1

I would like to add a daily job that updates data.

I have the following code:

Event::on(
    Controller::class,
    Controller::EVENT_BEFORE_ACTION,
       static function (ActionEvent $event) {
          //Check if is older than 24h
          if($check){
             //Add a job to queue
          }
       }
);

The problem with the code is that it only triggers when you are logged in and I would like to find a more efficient way because it is executed with every controller action, it would be enough if it is only executed once a day.

I also don't want to use cronjobs or something similar because the whole thing should happen natively in the plugin.

1
  • 4
    I hate to tell you this, but a cronjob is the answer.
    – Lindsey D
    Commented Jan 9 at 18:12

1 Answer 1

2

You should 1000% use a cron job for this.

The other, much uglier, option for this would be for your plugin to listen to an event, like onEndRequest.

That plugin keeps track of the last time it added this job to the queue in a database table. If, at the end of the request, you check that table and see it’s been > 24 hours, you add the job to the queue. If it’s < 24 hours, you bail.

Of course, this is all dependent on actually receiving front-end traffic. If the site doesn’t get any requests for 48+ hours, none of that logic will execute, which is why you should use a cron job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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