<p>You should 1000% use a cron job for this.</p>
<p>The other, much uglier, option for this would be for your plugin to listen to an event, like onEndRequest.</p>
<p>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 &gt; 24 hours, you add the job to the queue. If it’s &lt; 24 hours, you bail.</p>
<p>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.</p>

CC BY-SA 4.0
2024-01-15T23:30:49.070Z
2024-01-15T23:30:49.070Z
Brad Bell
<p>I would like to add a daily job that updates data.</p>
<p>I have the following code:</p>
<pre class="lang-php prettyprint-override"><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
          }
       }
);

</code></pre>
<p>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.</p>
<p>I also don't want to use cronjobs or something similar because the whole thing should happen natively in the plugin.</p>

CC BY-SA 4.0
2024-01-09T18:12:08.440Z
I hate to tell you this, but a cronjob is the answer.
Lindsey D
<plugin-development><events><queue><cron>
What is the best way to automatically add a job (queue) every day?
# What is the best way to automatically add a job (queue) every day?

(Asked by: username_0 on 2024-01-09)

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.

**Tags:** plugin-development, events, queue, cron
> **Comments:**
> **username_1**: I hate to tell you this, but a cronjob is the answer.  

---

## 1 answers

### Answer by username_2 on 2024-01-15. Score: 2 | Accepted answer

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.


---

