Hi there, I have an application that reads a digital input signal from a sensor and stores this in a database. I need to perform some actions at a certain on-delay of this signal. What's the best way to implement this? I've already developed a working solution by using delayed jobs but I think that these are only meant to be used for time consuming tasks. It would be nice that off-delays can also be applied.
Consider designing your own queue, something that involves two parts:
- A database table representing the queue items
- A scheduled task to process the queue items
The database table should be a simple model that can support the delay coming from the sensor, stored as a timestamp in the database.
The scheduled task is simply a custom artisan command
myplugin:work that looks up the latest queue entry, processes it and marks it as complete or rejected. The queue should also lock the record so it doesn't process the same entry twice.
You can then use the cronjob to run the artisan command every 1-5 minutes.
I hope this advice helps.
Hi daftspunk, Thank you for your answer. My solution already seems to implement parts of your advice: The queue worker is scheduled each 10 seconds and the datatable of the inputs has a boolean value for the "on_delay_active" status and a timestamp for the start of the active signal.
The only step I'm missing is the locking of the database. I guess that could explain the reason that the database is locking unexpectedly with my current solution. How can I achieve this locking? I can't find it in the October or Laravel Docs. The only similar thing that I found was the ShouldBeUniqueUntilProcessing Interface in Laravel 8.
Thanks for your help.
Any suggestions are very much appreciated
Look up the concept of a Mutex. There is a good example in the Laravel code with the
Illuminate\Console\Scheduling\CacheEventMutex class. This is essentially a method of locking a process so others cannot overlap. I'm not too familiar with the Laravel queue system in this regard, we use our own custom queue and mutex implementation.
1-5 of 5