Log output of executed cron job to table `oro_message_queue_job`

There is a data column in oro_message_queue_job which can be used to log the output of cron job.

References

vendor\oro\platform\src\Oro\Component\MessageQueue\Job\JobRunner.php

In private function callbackResult($runCallback, $job), it does the callback to execute the cron:

$jobRunner = $this->getJobRunnerForChildJob($job->getRootJob());
try {
    $result = call_user_func($runCallback, $jobRunner, $job);

So it passed 2 parameters to the anonymous function. The $job param is the entity for the table oro_message_queue_job.

vendor\oro\platform\src\Oro\Bundle\CronBundle\Async\CommandRunnerMessageProcessor.php

In protected function runRootJob($ownerId, array $body, array $commandArguments), it creates an anonymous function to run the command, which executes the cron, when it's called back in callbackResult($runCallback, $job):

return $this->jobRunner->runUnique($ownerId, $jobName, function () use ($commandName, $commandArguments) {
    $output = $this->commandRunner->run($commandName, $commandArguments);
    //...
});

Note that the anonymous function has no parameter.

Implementation

vendor\oro\platform\src\Oro\Bundle\CronBundle\Async\CommandRunnerMessageProcessor.php

protected function runRootJob($ownerId, array $body, array $commandArguments)
{
    $commandName = $body['command'];

    $jobName = sprintf('oro:cron:run_command:%s', $commandName);
    if ($commandArguments) {
        array_walk($commandArguments, function ($item, $key) use (&$jobName) {
            if (is_array($item)) {
                $item = implode(',', $item);
            }
            $jobName .= sprintf('-%s=%s', $key, $item);
        });
    }
    return $this->jobRunner->runUnique($ownerId, $jobName, function (JobRunner $jobRunner, Job $job) use ($commandName, $commandArguments) {
        $output = $this->commandRunner->run($commandName, $commandArguments);
        $job->setData([
            'output' => $output, 
            'arguments' => $commandArguments
        ]);
        if (!$job->isRoot()) { // only child jobs are run, added check just to be safe
            $job->getRootJob()->setData($job->getRootJob()->getData() + [$job->getId() => $job->getData()]);
        }
        $this->logger->info(sprintf('Command %s was executed. Output: %s', $commandName, $output), [
            'command' => $commandName,
            'arguments' => $commandArguments,
        ]);
        return true;
    });
}

vendor\oro\platform\src\Oro\Bundle\MessageQueueBundle\Resources\config\oro\datagrids.yml

Show the column data in the grid.

datagrids:
    ...
        columns:
        ....
            data:
                label: 'Data'
                frontend_type: array
        sorters:
   ...

Reference vendor\oro\platform\src\Oro\Bundle\DataGridBundle\Extension\Formatter\Property\PropertyInterface.php

kiatng About 5 years ago