You are here

function drush_services_client_sync_nodes in Services Client 7

Sync all nodes to specific connection

File

./services_client.drush.inc, line 192
Services client drush integration

Code

function drush_services_client_sync_nodes() {

  // Disable failure notifications for drush run
  global $conf;
  $conf['services_client_notify'] = FALSE;
  $min = drush_get_option('min');
  $max = drush_get_option('max');
  $hook = drush_get_option('hook', NULL);
  $type = drush_get_option('type', NULL);
  $query = db_select('node', 'n', array(
    'fetch' => PDO::FETCH_ASSOC,
  ))
    ->fields('n', array(
    'nid',
  ));
  if ($min) {
    $query
      ->condition('nid', $min, '>=');
  }
  if ($max) {
    $query
      ->condition('nid', $max, '<');
  }
  if ($type) {
    $query
      ->condition('type', $type);
  }
  $result = $query
    ->execute();
  foreach ($result as $row) {
    $attempts = 0;
    $sucessful = FALSE;
    $node = node_load($row['nid']);

    // Make 3 attempts to sync node
    while (!$sucessful && $attempts < 3) {
      unset($node->_services_client);
      $attempts++;
      $sc_result = services_client_data_process($node, 'node_save', $hook);
      if (!count($sc_result['errors'])) {
        $sucessful = TRUE;
      }
      elseif ($attempts < 3) {
        sleep($attempts);
      }
    }
    if (!$sucessful) {
      drush_print(dt("ERROR: !nid|!title", array(
        '!nid' => $node->nid,
        '!title' => $node->title,
      )));
    }

    // Reset static caches
    drupal_static_reset();
  }
}