I have a task in the schedule that pings other services to check for responses, and if the response is the same as before, nothing changes. However, if it’s different, I update the previous data in the database. But I want to make sure to check if the schedule even ran, because if it didn’t, the previous data could be corrupted.
Now, the question is how can I check when the previous task was run? I don’t think Laravel has a built-in option for that. What’s the best approach for this? Should I create a special table for this single record that I would update every time the task runs, or is it better to use the log file (but then how does the performance look like, especially with date conversion to compare with the current one?), or keep the values in cache (but then there’s also a risk of memory clearing)? Maybe you have a better approach to this solution?
I wouldn’t like to always update the dates in every record in this schedule because currently there are 40 records running every 30 seconds, and this number may grow.
2
Answers
One approach to solving this problem is to create a separate table in your database to store the last run time of the scheduled task. This table would have a single row with a timestamp column indicating the last time the task was run. You can then update this row every time the task runs.
Using a separate table to store the last run time, you can avoid the performance overhead of updating the dates in every record in the schedule. Additionally, this approach allows you to easily track the last run time of the scheduled task and take appropriate action if it fails to run within the expected interval.
Try Laravel Telescope on top of the list of the commands and when they last happened, it has a lot of very helpful insights.