You are here

function rec_transfer_handle_commands in Recommender API 7.4

Same name and namespace in other branches
  1. 6.3 rec_transfer/rec_transfer.module \rec_transfer_handle_commands()
  2. 7.5 rec_transfer/rec_transfer.module \rec_transfer_handle_commands()
1 call to rec_transfer_handle_commands()
rec_transfer_cron in rec_transfer/rec_transfer.module
Implements hook_cron().

File

rec_transfer/rec_transfer.module, line 88
This is the module file for Recommender Data Transfer

Code

function rec_transfer_handle_commands($cron = FALSE) {
  if (is_null(variable_get('rec_transfer_apikey')) || is_null(variable_get('rec_transfer_endpoint'))) {
    watchdog('rec_transfer', 'You must specify service endpoint and apikey.');
    return;
  }

  // retrieve the ping server commands
  $rows = db_query("SELECT id FROM {async_command} WHERE status IS NULL AND app='recommender' AND command='PingMe'");
  foreach ($rows as $row) {
    $ready_check = rec_transfer_check_service('ready');
    if (!$ready_check['success']) {
      async_command_update_command($row->id, array(
        'status' => 'FAIL',
        'message' => 'Remote service is not ready. Additional message: ' . $ready_check['message'],
        'end' => time(),
      ));
    }
    else {
      async_command_update_command($row->id, array(
        'status' => 'OKOK',
        'message' => 'Pong from remote service: ' . $ready_check['message'],
        'end' => time(),
      ));
    }
  }

  // retrieve a list of commands waiting to be executed.
  $rows = db_query("SELECT id, id1 FROM {async_command} WHERE status IS NULL AND app='recommender' AND command='RunRecommender'");
  $upload_queue = DrupalQueue::get('rec_transfer_upload');
  foreach ($rows as $row) {
    $command = array(
      'command_id' => $row->id,
      'recommender_id' => $row->id1,
    );
    if (!$cron) {
      rec_transfer_upload($command);
    }
    else {
      $upload_queue
        ->createItem($command);
    }
  }

  // retrieve a list of commands pending or running, check status and download results if necessary
  $rows = db_query("SELECT id, id1 FROM {async_command} WHERE status IN ('PEND', 'RUNN') AND app='recommender' AND command='RunRecommender'");
  $download_queue = DrupalQueue::get('rec_transfer_download');
  foreach ($rows as $row) {
    $command = array(
      'command_id' => $row->id,
      'recommender_id' => $row->id1,
    );
    if (!$cron) {
      rec_transfer_download($command);
    }
    else {
      $download_queue
        ->createItem($command);
    }
  }
}