Run ID: job identifier a found in database (Snowflake ID)
- Class: class of the job
- Started at: start time of the job
- Server ID: server ID as defined in config.php> (see `serverid`). Highlighted if it’s running on current server.
- PID: PID of process executing the job
- Running since: human readable elapsed time since job started
EOF;
$this
->setName('background-job:running')
->setDescription('Show currently running jobs')
->setHelp($help)
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Maximum number of results returned by the command', 200);
}
#[Override]
protected function execute(InputInterface $input, OutputInterface $output): int {
$limit = (int)$input->getOption('limit');
$jobs = $this->jobRuns->runningJobs($limit);
$this->writeStreamingTableInOutputFormat($input, $output, $this->formatLine($jobs), 20);
return Base::SUCCESS;
}
private function formatLine(iterable $jobs): \Generator {
$now = time();
$currentServerId = $this->config->getSystemValueInt('serverid', -1);
foreach ($jobs as $job) {
yield [
'Run ID' => $job->runId,
'Class' => $job->className,
'Started at' => $job->startedAt->format('Y-m-d H:i:s'),
'Server ID' => $job->serverId === $currentServerId ? '' . $job->serverId . '' : $job->serverId,
'PID' => $job->pid,
'Running since' => $this->formatDuration($now - $job->startedAt->format('U')),
];
}
}
/**
* TODO Move this function to utils class with better formatting (plural, i18n…)
*/
private function formatDuration(int $seconds): string {
if ($seconds < 60) {
return sprintf('%d seconds', $seconds);
}
if ($seconds < 3600) {
return sprintf('%d minutes', $seconds / 60);
}
if ($seconds < (3600 * 24)) {
return sprintf('> %d hours', $seconds / 3600);
}
return sprintf('> %d days', $seconds / (3600 * 24));
}
}