Skip to content
ultimate_cron.drush.inc 12.3 KiB
Newer Older
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
<?php
/**
 * @file
 * Drush commands for Ultimate Cron!
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
 */
use Drupal\ultimate_cron\CronPlugin;
use Drupal\ultimate_cron\Entity\CronJob;
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed

/**
Dane Powell's avatar
Dane Powell committed
 * Implements hook_drush_command().
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
 */
function ultimate_cron_drush_command() {
  $items = array();

Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  $items['cron-logs'] = array(
    'description' => 'Show a cron jobs logs',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    'arguments' => array(
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      'name' => 'Job to show logs for',
    ),
    'options' => array(
      'limit' => 'Number of log entries to show',
      'compact' => 'Only show the first line of each log entry',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    ),
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    'examples' => array(
      'drush cron-logs node_cron --limit=20' => 'Show 20 last logs for the node_cron job',
    ),
  );

  $items['cron-list'] = array(
    'description' => 'List cron jobs',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    'options' => array(
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      'module' => 'Comma separated list of modules to show jobs from',
      'enabled' => 'Show enabled jobs',
      'disabled' => 'Show enabled jobs',
      'behind' => 'Show jobs that are behind schedule',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      'status' => 'Comma separated list of statuses to show jobs from',
      'extended' => 'Show extended information',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      'name' => 'Show name instead of title',
      'scheduled' => 'Show scheduled jobs',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    ),
    'examples' => array(
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      'drush cron-list --status=running --module=node' => 'Show jobs from the node module that are currently running',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    ),
    'aliases' => array('cl'),
  );

  $items['cron-run'] = array(
    'description' => 'Run cron job',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    'arguments' => array(
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      'name' => 'Job to run',
      'force' => 'Skip the schedule check for each job. Locks are still respected.',
      'options' => 'Custom options for plugins, e.g. --options=thread=1 for serial launcher',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    'examples' => array(
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      'drush cron-run node_cron' => 'Run the node_cron job',
      'drush cron-run --options=thread=1' => 'Run all scheduled jobs and instruct serial launcher only to launch thread 1 jobs',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    ),
  $items['cron-is-running'] = array(
    'description' => 'Tell whether cron is running. Exit status is set in concordance with the cron running status.',
    'examples' => array(
      'drush cron-is-running' => 'Check if cron is running.',
      'drush cron-is-running --quiet' => 'Check if cron is running and don\'t show an informative message.',
      'while `drush cron-is-running --quiet`; do echo "Waiting cron to finish"; sleep 1; done' => 'Bash loop to wait until cron finishes.',
    ),
    'aliases' => array('cir'),
  );

Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  $items['cron-enable'] = array(
    'description' => 'Enable cron job',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    'arguments' => array(
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      'name' => 'Job to enable',
    ),
    'options' => array(
      'all' => 'Enabled all jobs',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    ),
    'examples' => array(
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      'drush cron-enable node_cron' => 'Enable the node_cron job',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    ),
    'aliases' => array('ce'),
  );

  $items['cron-disable'] = array(
    'description' => 'Disable cron job',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    'arguments' => array(
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      'name' => 'Job to disable',
    ),
    'options' => array(
      'all' => 'Enabled all jobs',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    ),
    'examples' => array(
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      'drush cron-disable node_cron' => 'Disable the node_cron job',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    ),
    'aliases' => array('cd'),
  );

  $items['cron-unlock'] = array(
    'description' => 'Unlock cron job',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    'arguments' => array(
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      'name' => 'Job to unlock',
    ),
    'options' => array(
      'all' => 'Enabled all jobs',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    ),
    'examples' => array(
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      'drush cron-unlock node_cron' => 'Unlock the node_cron job',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    ),
    'aliases' => array('cu'),
  );

  return $items;
}

/**
Dane Powell's avatar
Dane Powell committed
 * Implements hook_drush_help().
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
 */
function ultimate_cron_drush_help($section) {
  switch ($section) {
    case 'drush:cron-list':
      return dt('This command will list cron jobs');
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    case 'drush:cron-run':
      return dt('This command will run a cron job');
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    case 'drush:cron-enable':
      return dt('This command will enable a cron job');
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    case 'drush:cron-disable':
      return dt('This command will disable a cron job');
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    case 'drush:cron-unlock':
      return dt('This command will unlock a cron job');
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  }
}

/**
 * List cron jobs.
 */
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
function drush_ultimate_cron_cron_list() {
  $modules = drush_get_option('module');
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  $enabled = drush_get_option('enabled');
  $disabled = drush_get_option('disabled');
  $behind = drush_get_option('behind');
  $extended = drush_get_option('extended');
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  $statuses = drush_get_option('status');
  $scheduled = drush_get_option('scheduled');
  $showname = drush_get_option('name');

  $modules = $modules ? explode(',', $modules) : array();
  $statuses = $statuses ? explode(',', $statuses) : array();
  $title = $showname ? dt('Name') : dt('Title');
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed

  $table = array();
  $table[] = array(
    '',
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    dt('Module'),
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    dt('Scheduled'),
    dt('Started'),
    dt('Duration'),
  $print_legend = FALSE;

  /** @var \Drupal\ultimate_cron\Entity\CronJob $job */
  foreach (CronJob::loadMultiple() as $name => $job) {
    if ($modules && !in_array($job->getModule(), $modules)) {
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      continue;
    if ($enabled && FALSE === $job->status()) {
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      continue;
    }
    if ($disabled && TRUE === $job->status()) {
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      continue;
    }
    if ($scheduled && !$job->isScheduled()) {
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      continue;
    }
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    $legend = '';

    if (FALSE === $job->status()) {
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      $legend .= 'D';
      $print_legend = TRUE;
    $lock_id = $job->isLocked();
    $log_entry = $job->loadLogEntry($lock_id);
    if ($time = $job->isBehindSchedule()) {
      $legend .= 'B';
      $print_legend = TRUE;
    if ($behind && !$time) {
      continue;
    }

    if ($lock_id && $log_entry->lid == $lock_id) {
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      $legend .= 'R';
      list(, $status) = $job->getPlugin('launcher')->formatRunning($job);
      $print_legend = TRUE;
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    }
    elseif ($log_entry->start_time && !$log_entry->end_time) {
     list(, $status) = $job->getPlugin('launcher')->formatUnfinished($job);
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    }
    else {
      list(, $status) = $log_entry->formatSeverity();
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    }
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    if ($statuses && !in_array($status, $statuses)) {
      continue;
    }

    $progress = $lock_id ? $job->formatProgress() : '';
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed

    $table[$name][] = $legend;
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    $table[$name][] = $job->getModuleName();
    $table[$name][] = $showname ? $job->id() : $job->getTitle();
    $table[$name][] = $job->getPlugin('scheduler')->formatLabel($job);
    $table[$name][] = $log_entry->formatStartTime();
    $table[$name][] = $log_entry->formatDuration() . ' ' . $progress;
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    $table[$name][] = $status;

    if ($extended) {
      $table['extended:' . $name][] = '';
      $table['extended:' . $name][] = '';
ChrisjuhB's avatar
ChrisjuhB committed
      $table['extended:' . $name][] = $job->id();
      $table['extended:' . $name][] = $job->getPlugin('scheduler')->formatLabelVerbose($job);
      $table['extended:' . $name][] = $log_entry->init_message;
      $table['extended:' . $name][] = $log_entry->message;
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    }
  }
  drush_print_table($table);
  if ($print_legend) {
    drush_print("\n" . dt('Legend: D = Disabled, R = Running, B = Behind schedule'));
  }
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
 * List cron jobs.
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
 */
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
function drush_ultimate_cron_cron_logs($name = NULL) {
  if (!$name) {
    return drush_set_error(dt('No job specified?'));
  }

  /** @var CronJob $job */
  $job = Cronjob::load($name);

Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  if (!$job) {
    return drush_set_error(dt('@name not found', array('@name' => $name)));
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  $compact = drush_get_option('compact');
  $limit = drush_get_option('limit');
  $limit = $limit ? $limit : 10;
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  $table = array();
  $table[] = array(
    '',
    dt('Started'),
    dt('Duration'),
    dt('User'),
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    dt('Initial message'),
    dt('Message'),
    dt('Status'),
  );

  $lock_id = $job->isLocked();
  $log_entries = $job->getLogEntries(ULTIMATE_CRON_LOG_TYPE_ALL, $limit);
  /** @var \Drupal\ultimate_cron\Logger\LogEntry $log_entry */
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  foreach ($log_entries as $log_entry) {
    $progress = '';
    if ($log_entry->lid && $lock_id && $log_entry->lid === $lock_id) {
      $progress = $job->getProgress();
      $progress = is_numeric($progress) ? sprintf(' (%d%%)', round($progress * 100)) : '';
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    $legend = '';
    if ($lock_id && $log_entry->lid == $lock_id) {
      $legend .= 'R';
      list(, $status) = $job->getPlugin('launcher')->formatRunning($job);
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    }
    elseif ($log_entry->start_time && !$log_entry->end_time) {
      list(, $status) = $job->getPlugin('launcher')->formatUnfinished($job);
      list(, $status) = $log_entry->formatSeverity();
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    }

    $table[$log_entry->lid][] = $legend;
    $table[$log_entry->lid][] = $log_entry->formatStartTime();
    $table[$log_entry->lid][] = $log_entry->formatDuration() . $progress;
    $table[$log_entry->lid][] = $log_entry->formatUser();
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    if ($compact) {
      $table[$log_entry->lid][] = trim(reset(explode("\n", $log_entry->init_message)), "\n");
      $table[$log_entry->lid][] = trim(reset(explode("\n", $log_entry->message)), "\n");
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    else {
      $table[$log_entry->lid][] = trim($log_entry->init_message, "\n");
      $table[$log_entry->lid][] = trim($log_entry->message, "\n");
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    }
    $table[$log_entry->lid][] = $status;
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  drush_print_table($table);
}
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
/**
 * Run cron job(s).
 */
function drush_ultimate_cron_cron_run($name = NULL) {
  if ($options = drush_get_option('options')) {
    $pairs = explode(',', $options);
    foreach ($pairs as $pair) {
      list($key, $value) = explode('=', $pair);
      CronPlugin::setGlobalOption(trim($key), trim($value));
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  if (!$name) {
    // Run all jobs.
    $jobs = CronJob::loadMultiple();

    /** @var CronJob $job */
    foreach($jobs as $job) {
      if ($force || $job->isScheduled()) {
        $job->run(t('Launched by drush'));
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  else {
    // Run a specific job.
    $job = CronJob::load($name);

Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    if (!$job) {
      return drush_set_error(dt('@name not found', array('@name' => $name)));
    }

    if ($force || $job->isScheduled()) {
      $job->run(t('Launched by drush'));
/**
 * Tell whether cron is running.
 */
function drush_ultimate_cron_cron_is_running() {
  $locked = FALSE;
  foreach (CronJob::loadMultiple() as $name => $job) {
    if ($job->isLocked()) {
      $locked = TRUE;
      break;
    }
  }

  if ($locked) {
    $msg = dt('Cron is running.');
    drush_set_context('DRUSH_EXIT_CODE', 0);
  }
  else {
    $msg = dt('Cron is not running.');
    drush_set_context('DRUSH_EXIT_CODE', 1);
  }

  return $msg;
}

Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
/**
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
 * Enable a cron job.
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
 */
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
function drush_ultimate_cron_cron_enable($name = NULL) {
  if (!$name) {
    if (!drush_get_option('all')) {
      return drush_set_error(dt('No job specified?'));
    }
    /** @var CronJob $job */
    foreach (CronJob::loadMultiple() as $job) {
      $job->enable()->save();
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    }
    return;

  $job = CronJob::load($name);
  if ($job->enable()->save()) {
    drush_print(dt('@name enabled', array('@name' => $name)));
  }
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
 * Disable a cron job.
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
 */
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
function drush_ultimate_cron_cron_disable($name = NULL) {
  if (!$name) {
    if (!drush_get_option('all')) {
      return drush_set_error(dt('No job specified?'));
    }
    foreach (CronJob::loadMultiple() as $job) {
      $job->disable()->save();
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    }
    return;
  }

  $job = CronJob::load($name);
  if ($job->disable()->save()) {
    drush_print(dt('@name disabled', array('@name' => $name)));
  }
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
 * Unlock a cron job.
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
 */
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
function drush_ultimate_cron_cron_unlock($name = NULL) {
  if (!$name) {
    if (!drush_get_option('all')) {
      return drush_set_error(dt('No job specified?'));
    }
    /** @var CronJob $job */
    foreach (CronJob::loadMultiple() as $job) {
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
      if ($job->isLocked()) {

  /** @var CronJob $job */
  $job = CronJob::load($name);
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  if (!$job) {
    return drush_set_error(dt('@name not found', array('@name' => $name)));
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  $lock_id = $job->isLocked();
  if (!$lock_id) {
    return drush_set_error(dt('@name is not running', array('@name' => $name)));
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  // Unlock the process.
  if ($job->unlock($lock_id, TRUE)) {
    $log_entry = $job->resumeLog($lock_id);
    global $user;
    \Drupal::logger('ultimate_cron')->warning('@name manually unlocked by user @username (@uid)', array(
ChrisjuhB's avatar
ChrisjuhB committed
      '@name' => $job->id(),
      '@username' => $user->getDisplayName(),
      '@uid' => $user->id(),
    $log_entry->finish();
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    drush_print(dt('Cron job @name unlocked', array('@name' => $name)));
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
  }
  else {
Thomas Gielfeldt's avatar
Thomas Gielfeldt committed
    drush_set_error(dt('Could not unlock cron job @name', array('@name' => $name)));