Home » Blog » Dev notes » Scheduling cron jobs in Magento

Scheduling cron jobs in Magento

thumbnail

In this topic we will show how to create cron tasks with example. We can use this to trigger functions in the declared time. This is very helpful if you have to trigger functions that are consuming resources and needs time. 

Notice: Creating the module tutorial is here.

STEP 1. Create crontab.xml

In this step we will create a file that is responsible for the corrected crontab configuration. Cron file should always be in /etc folder. There is no split into areas. 

The file should have pattern like this:

app/code/[your_namespace]/[your_module]/etc/crontab.xml

in our case it’s:

app/code/PandaGroup/CronExample/etc/crontab.xml

Content of crontab.xml is like this:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"-xsi
:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/
crontab.xsd">
<group id="default">
  <job name="pandagroup_send_mails"
       instance="PandaGroup\CronExample\Cron" method="execute">
    <schedule>*/1 * * * *</schedule>
  </job>
</group>
</config>

The explanation for each node:

  • group id – here you have to type to what group the cron jobs belongs e.g. index, default, custom
  • job name – cronjob name (defined by you)
  • instance – class with the function to start
  • method – name of the function that you want to start
  • schedule – here type when the cron should start – it’s standard cron pattern

STEP 2. Create cron group

In this step we will create a custom cron group, but it is optional. You can use default groups like default or index.

The file should have pattern like this:

app/code/[your_namespace]/[your_module]/etc/cron_groups.xml

in our case it’s:

app/code/PandaGroup/CronExample/etc/cron_groups.xml

Content of this file should be with this pattern:

<config>
    <group id="<group_name> e.g. mails">
        <schedule_generate_every>1</schedule_generate_every>
        <schedule_ahead_for>10</schedule_ahead_for>
        <schedule_lifetime>3</schedule_lifetime>
        <history_cleanup_every>20</history_cleanup_every>
        <history_success_lifetime>60</history_success_lifetime>
        <history_failure_lifetime>600</history_failure_lifetime>
        <use_separate_process>1</use_separate_process>
    </group>
</config>

The explanation for each node:

  • schedule_generate_every – frequency in minutes that schedules are written to the table in database named cron_schedule
  • schedule_ahead_for – time in minutes in advance that schedules are written to the database table named cron_schedule
  • schedule_lifetime – window of time in minutes that cron job must start or it will be considered as missed
  • history_cleanup_every – time in minutes that cron history is kept in the database
  • history_success_lifetime – time in minutes that the record of successfully completed cron jobs are kept in the database
  • history_failure_lifetime – time in minutes that the record of failed cron jobs are kept in the database
  • use_separate_process – flag that says if run this crongroup’s jobs in a separate php process

STEP 3. Create file with function to start

The file should have pattern like this:

app/code/[your_namespace]/[your_module]/Cron/YourNameClass.php

in our case it’s:

app/code/PandaGroup/CronExample/Cron/Test.php

<?php

  namespace PandaGroup/CronExample/Cron;

  class Test
  {
    public function execute()
  {

    //trigger your function here

    return $this;
  }
  }

That’s all. Now compile and clean cache and go to the next step – commands. 

Compiling:

bin/magento setup:di:compile

Cache clean:

bin/magento cache:clean

STEP 4. Commands

Running all cron jobs:

bin/magento cron:run

Running jobs with cron group e.g. default group:

bin/magento cron:run --group="default"
+1 (No Ratings Yet)
Loading...

Leave a Reply

Your email address will not be published. Required fields are marked *

*