Skip to content
Advertisement

Laravel Repeating Task

I need a task:

  • which can be turned on or off (only for admins/authorized users)
  • which runs in a constant loop
  • which should display a timer to every user each loop, 60 seconds for example (with pusher)
  • if the user connects to this task 20 seconds in, it should continue from the remaining 40 seconds

How would I achieve something like that? I was thinking about a combination of events, queues and tasks, but I’m not sure how I should use them to create my task.

Advertisement

Answer

It sounds like you can use the scheduler to control the frequency of your task

https://laravel.com/docs/7.x/scheduling

You can then use a truth constraint to check whether it should run or not like so:

$schedule->command('your:task')->when(function () {
    // Do a config check / DB check / Cache check here
    return true;
});

I would suggest setting a value in something like the caching layer rather than accessing a file or using a relational database. This is a task which will be run every second so you don’t want anything which could be too resource intensive!

You can then have a command to set the value in the cache that you are checking. This command should only be run by those users you give permission.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement