Posted over 3 years ago. Visible to the public.

Add a Cron Job and Command

There is no config to register a cron job in OroPlatform. There are 2 steps to add a cron job:

  1. add a command class in the bundle
  2. run a CLI to add the job in table oro_cron_schedule

The added class can be run as a command in CLI when it is defined as a Service.

Add a Command Class

src\Stars\Bundle\LoggerEventBundle\Command\InsertActionDataCommand.php

Copy
<?php namespace Stars\Bundle\LoggerEventBundle\Command; use Oro\Bundle\CronBundle\Command\CronCommandInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class InsertActionDataCommand extends Command implements CronCommandInterface { /** * Command name for cron must start with the namesapce "oro:cron"; * see vendor\oro\platform\src\Oro\Bundle\CronBundle\Command\CronDefinitionsLoadCommand.php */ const COMMAND_NAME = 'oro:cron:sc-logger-event:insert-action-data'; /** * The max number of events to read from STARS */ const LIMIT_EVENTS = 80; /** * {@inheritDoc} */ public function isActive() { return true; } /** * {@inheritDoc} */ public function getDefaultDefinition() { return '0 1 * * *'; } /** * {@inheritdoc} */ public function configure() { $this ->setName(static::COMMAND_NAME) ->setDescription('Insert unserialized action_data from STARS to table sc_stars_logger_event_action_data') ->addOption( 'limit', 'l', InputOption::VALUE_OPTIONAL, 'The max number of events to read from STARS.', self::LIMIT_EVENTS ); } /** * {@inheritDoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $cnt = $this->insertByEntity((int) $input->getOption('limit')); $output->writeln(sprintf('<comment>Rows Inserted:</comment> %d', $cnt)); } }

Add Job to DB

Copy
[kiat@reporting misoro]$ sudo -u nginx php bin/console oro:cron:definitions:load Removing all previously loaded commands... Processing command "oro:cron:sc-logger-event:insert-action-data": setting up schedule.. Processing command "oro:cron:message-queue:cleanup": setting up schedule.. Processing command "oro:cron:message-queue:consumer_heartbeat_check": setting up schedule.. Processing command "oro:cron:email-body-sync": setting up schedule.. Processing command "oro:cron:definitions:load": Skipping, the command does not implement CronCommandInterface Processing command "oro:cron:integration:cleanup": setting up schedule.. Processing command "oro:cron:integration:sync": setting up schedule.. Processing command "oro:cron:import-clean-up-storage": setting up schedule.. Processing command "oro:cron:batch:cleanup": setting up schedule.. Processing command "oro:cron:imap-sync": setting up schedule.. Processing command "oro:cron:imap-credential-notifications": setting up schedule.. Processing command "oro:cron:calendar:date": setting up schedule.. Processing command "oro:cron:send-reminders": setting up schedule..

Define the Command as a Service

I need to test and execute the command in bash (and controller if necessary). So, I define the command as a service.

Configure the Service

Following OroPlatform convention, command services are configured in the file commands.yml.

src\Stars\Bundle\LoggerEventBundle\Resources\config\commands.yml

Copy
services: Stars\Bundle\LoggerEventBundle\Command\InsertActionDataCommand: autowire: true tags: - { name: console.command } #- { name: console.command, command: 'oro:cron:sc-logger-event:insert-action-data' } # it's unnecessary to add the command tag

I configured it for autowiring and lazy loading Archive . ~I also configured the command name to run in CLI as oro:cron:sc-logger-event:insert-action-data. Note: it is important to use the same command name as defined in the command class. Otherwise, it will not be loaded as a cron job.~

Load the Configuration

src\Stars\Bundle\LoggerEventBundle\DependencyInjection\StarsLoggerEventExtension.php

Copy
<?php namespace Stars\Bundle\LoggerEventBundle\DependencyInjection; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\HttpKernel\DependencyInjection\Extension; class StarsLoggerEventExtension extends Extension { /** * {@inheritDoc} */ public function load(array $configs, ContainerBuilder $container) { $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('commands.yml'); } }

Clear Cache and Check

Copy
[kiat@reporting misoro]$ sudo -u nginx php bin/console debug:container stars Select one of the following services to display its information: [0] Stars\Bundle\EmgsiheDbBundle\Dbal\Connection [1] Stars\Bundle\LoggerEventBundle\Command\InsertActionDataCommand > 1 Information for Service "Stars\Bundle\LoggerEventBundle\Command\InsertActionDataCommand" ======================================================================================== ---------------- ------------------------------------------------------------------ Option Value ---------------- ------------------------------------------------------------------ Service ID Stars\Bundle\LoggerEventBundle\Command\InsertActionDataCommand Class Stars\Bundle\LoggerEventBundle\Command\InsertActionDataCommand Tags console.command (command: stars:logger_event:insert_action_data) Calls setName Public no Synthetic no Lazy no Shared yes Abstract no Autowired yes Autoconfigured no ---------------- ------------------------------------------------------------------

Run the Command in Bash

Copy
[kiat@reporting misoro]$ sudo -u nginx php bin/console stars:logger_event:insert_action_data --help Usage: stars:logger_event:insert_action_data [options] Options: -l, --limit[=LIMIT] The max number of events to read from STARS. [default: 80] -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -e, --env=ENV The Environment name. [default: "dev"] --no-debug Switches off debug mode. --disabled-listeners=DISABLED-LISTENERS Disable optional listeners, "all" to disable all listeners, command "oro:platform:optional-listeners" shows all listeners (multiple values allowed) --current-user=CURRENT-USER ID, username or email of the user that should be used as current user --current-organization=CURRENT-ORGANIZATION ID or name of the organization that should be used as current organization -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Help: Insert unserialized action_data from STARS to table sc_stars_logger_event_action_data [kiat@reporting misoro]$ sudo -u nginx php bin/console stars:logger_event:insert_action_data Rows Inserted: 445

List Cron Commands

There is no specific CLI command to list all the oro:cron. But there is a hack:

Copy
[kiat@reporting misoro]$ sudo -u nginx php bin/console oro:cro Command "oro:cro" is ambiguous. Did you mean one of these? oro:cron:message-queue:cleanup Clear successes and failed jobs from message_queue_job table oro:cron:message-queue:consumer_heartbeat_check Checks if there is alive consumers oro:cron:email-body-sync Synchronize email body oro:cron Cron commands launcher oro:cron:definitions:load Loads cron commands definitions from application to database. oro:cron:integration:cleanup Clean up integration statuses history oro:cron:integration:sync Runs synchronization for integration oro:cron:import-clean-up-storage Clear old files from import storage. oro:cron:batch:cleanup Clean up batch history oro:cron:imap-sync Synchronization emails via IMAP oro:cron:imap-credential-notifications Send wrong email credentials notifications oro:cron:calendar:date Generate calendar dates oro:cron:sc-logger-event:insert-action-data Insert unserialized action_data from STARS to table sc_stars_logger_event_action_data oro:cron:send-reminders Send reminders

If the cron isn't added to the Job List, clear the prod cache

If the cron is not added to the Job List in backend and the table oro_message_queue_job:

Copy
[kiat@reporting misoro]$ sudo -u nginx php bin/console oro:cron --env prod The command "oro:cron:sc-logger-event:insert-action-data" does not exist. oro:cron [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--disabled-listeners DISABLED-LISTENERS] [--current-user CURRENT-USER] [--current-organization CURRENT-ORGANIZATION] [--] <command> [kiat@reporting misoro]$ sudo -u nginx php bin/console cache:clear --env prod // Clearing the cache for the prod environment with debug false [OK] Cache for the "prod" environment (debug=false) was successfully cleared. [kiat@reporting misoro]$ sudo -u nginx php bin/console oro:cron --env prod [kiat@reporting misoro]$

Owner of this card:

Avatar
kiatng
Last edit:
over 3 years ago
by kiatng
Posted by kiatng to Oro
This website uses short-lived cookies to improve usability.
Accept or learn more