Job Execution¶
Time-Based Command Execution¶
You’ll often have tasks that should be executed at fixed times (for example, sending mailings to customers or generating reports). Usually, these tasks wouldn’t be performed by visiting a particular URL using your browser. Instead, you’ll configure your operating system to execute some script which performs the desired tasks.
With OroPlatform, you can use the OroCronBundle which makes it easy to run Symfony Console commands through cronjobs (on UNIX-based operating systems) or through the Windows task scheduler.
Configuring the Entry Point Command¶
All you have to do to regularly run a set of commands from your application
is to configure your system to run the oro:cron
command every minute.
On UNIX-based systems, you can simply set up a crontab
entry for this:
*/1 * * * * /path/to/php /path/to/app/console oro:cron --env=prod > /dev/null
Note: Some OS flavors will require the user name (usually root) in the crontab entry, like this:
*/1 * * * * root /path/to/php /path/to/app/console oro:cron --env=prod > /dev/null
On Windows, use the Control Panel to configure the Task Scheduler to do the same.
Note
This entry in the crontab doesn’t mean that your cron commands are executed
every minute. The oro:cron
command only makes sure that the actual
commands are added to the scheduler which makes sure that they are only
executed at the desired times (see How Does it Work
for some insights into the actual process).
Creating the Command¶
The oro:cron
command will automatically execute all commands previously
loaded with oro:cron:definitions:load
command. The command loads commands
that implement the CronCommandInterface
if they are registered in the
oro:cron
namespace. Implementing the CronCommandInterface
requires
you to implement two methods –
getDefaultDefinition()
.
It returns the crontab compatible description of when the command should
be executed. For example, if a command should be run every day five minutes
after midnight, the appropriate value is 5 0 * * *
. Your command will
then look like this:
isCronEnabled()
.
It checks some pre-conditions and returns true or false. If it returns false the
command will not be added to the Message Queue. For example for the integrations
sync command it can check that there’re more than 0 active integrations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | // src/Acme/DemoBundle/Command/DemoCommand.php
namespace Acme\DemoBundle\Command;
use Oro\Bundle\CronBundle\Command\CronCommandInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class DemoCommand implements CronCommandInterface
{
public function getDefaultDefinition()
{
return '5 0 * * *';
}
public function isCronEnabled()
{
// check some pre-conditions
return $condition ? true : false;
}
protected function configure()
{
$this->setName('oro:cron:demo');
// ...
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// ...
}
}
|