You are here

public function UltimateCronCommands::cronList in Ultimate Cron 8.2

List cron jobs.

@command cron:list @option module Comma separated list of modules to show jobs from @option enabled Show enabled jobs @option disabled Show enabled jobs @option behind Show jobs that are behind schedule @option status Comma separated list of statuses to show jobs from @option extended Show extended information @option name Show name instead of title @option scheduled Show scheduled jobs @usage drush cron-list --status=running --module=node Show jobs from the node module that are currently running @aliases crl cron-list @format table

Parameters

array $options: Options array.

File

src/Commands/UltimateCronCommands.php, line 140

Class

UltimateCronCommands
Class UltimateCronCommands.

Namespace

Drupal\ultimate_cron\Commands

Code

public function cronList(array $options = [
  'module' => NULL,
  'enabled' => NULL,
  'disabled' => NULL,
  'behind' => NULL,
  'status' => NULL,
  'extended' => NULL,
  'name' => NULL,
  'scheduled' => NULL,
]) {
  $modules = $options['module'];
  $enabled = $options['enabled'];
  $disabled = $options['disabled'];
  $behind = $options['behind'];
  $extended = $options['extended'];
  $statuses = $options['status'];
  $scheduled = $options['scheduled'];
  $showname = $options['name'];
  $modules = $modules ? explode(',', $modules) : [];
  $statuses = $statuses ? explode(',', $statuses) : [];
  $title = $showname ? dt('Name') : dt('Title');
  $table = [];
  $table[] = [
    '',
    dt('ID'),
    dt('Module'),
    $title,
    dt('Scheduled'),
    dt('Started'),
    dt('Duration'),
    dt('Status'),
  ];
  $print_legend = FALSE;

  /** @var \Drupal\ultimate_cron\Entity\CronJob $job */
  foreach (CronJob::loadMultiple() as $name => $job) {
    if ($modules && !in_array($job
      ->getModule(), $modules)) {
      continue;
    }
    if ($enabled && FALSE === $job
      ->status()) {
      continue;
    }
    if ($disabled && TRUE === $job
      ->status()) {
      continue;
    }
    if ($scheduled && !$job
      ->isScheduled()) {
      continue;
    }
    $legend = '';
    if (FALSE === $job
      ->status()) {
      $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) {
      $legend .= 'R';
      list(, $status) = $job
        ->getPlugin('launcher')
        ->formatRunning($job);
      $print_legend = TRUE;
    }
    elseif ($log_entry->start_time && !$log_entry->end_time) {
      list(, $status) = $job
        ->getPlugin('launcher')
        ->formatUnfinished($job);
    }
    else {
      list(, $status) = $log_entry
        ->formatSeverity();
    }
    if ($statuses && !in_array($status, $statuses)) {
      continue;
    }
    $progress = $lock_id ? $job
      ->formatProgress() : '';
    $table[$name][] = $legend;
    $table[$name][] = $job
      ->id();
    $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;
    $table[$name][] = $status;
    if ($extended) {
      $table['extended:' . $name][] = '';
      $table['extended:' . $name][] = '';
      $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;
    }
  }
  if ($print_legend) {
    $this->output
      ->writeln("\n" . dt('Legend: D = Disabled, R = Running, B = Behind schedule'));
  }
  return new RowsOfFields($table);
}