0

We are developing one Extension in which we provide the facility to the user to set his custom time for run corn job.

for this functionality, we provide fields in-store configuration like:

Custom Schedule: {*/5 * * * *}

Or

Frequency: {Monthly/Weekly/Daily}

Start On: {00:00:59}(time to start)

and set this dynamic value into crontab => config -> group -> job -> shedule

How can we set this functionality in our Extension?

Which is the Better way for implementing this functionality?

1
0

Existing cronjobs can be overridden by setting a new schedule in the core_config_data table.

Let's say your user wants to update the schedule for the catalogrule_apply_all job (defaults to: 0 1 * * *) to 0 5 * * *. You'll need to do something like this:

public function __construct(
   \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
) {
    $this->_configWriter = $configWriter;
}

public function doStuff() {
    $scopeId = 0; // Change this to the scope of your user.
    $path = 'crontab/default/jobs/catalogrule_apply_all/schedule/cron_expr';
    $value = '0 5 * * *';

    $this->_configWriter->save($path, $value, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId);
}

Using raw SQL, that would look like:

INSERT INTO `core_config_data` (`config_id`, `scope`, `scope_id`, `path`, `value`, `updated_at`) VALUES (NULL, 'default', '0', 'crontab/default/jobs/catalogrule_apply_all/schedule/cron_expr', '0 5 * * *', CURRENT_TIME());

Clear the cache for the config to be updated:

bin/magento c:c

You can check that the job schedule has been taken into account by using n98-magerun2 :

n98-magerun2 sys:cron:list | grep catalogrule_apply_all

Output:

+-----------------------------------------------------+---------------+------+------+---+---+----+
| Job                                                 | Group         | m    | h    | D | M | WD |
+-----------------------------------------------------+---------------+------+------+---+---+----+
| catalogrule_apply_all                               | default       | 0    | 5    | * | * | *  |
+-----------------------------------------------------+---------------+------+------+---+---+----+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.