Skip to content
Advertisement

Laravel schedular: execute a command every second

I have a project that needs to send notifications via WebSockets continuously. It should connect to a device that returns the overall status in string format. The system processes it and then sends notifications based on various conditions.

Since the scheduler can repeat a task as early as a minute, I need to find a way to execute the function every second.

Here is my app/Console/Kernel.php:

JavaScript

PS: If you have a better idea to handle the situation, please share your thoughts.

Advertisement

Answer

Usually, when you want more granularity than 1 minute, you have to write a daemon.

I advise you to try, now it’s not so hard as it was some years ago. Just start with a simple loop inside a CLI command:

JavaScript

One important thing: run the daemon via supervisord. You can take a look at articles about Laravel’s queue listener setup, it uses the same approach (a daemon + supervisord). A config section can look like this:

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