Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement timing Scheduler for laravel Asynchronous Monitoring

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "laravel asynchronous monitoring timing scheduler how to achieve", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "laravel asynchronous monitoring timing scheduler how to achieve" it!

What is the timing scheduler?

Laravel provides a command timed task function by default. Without this scheduled task under other php frameworks, we can only rely on the crontab provided by our system to run some asynchronous scripts. This causes us to go to the server to change the crontab code to update this configuration every time we release a new timed task.

If the command is executed by php artisan schedule:run, where should it be executed? yes, this call still needs to be executed by our crontab, but it only needs to be configured once, and all subsequent scheduled tasks are controlled by our business code.

Scene

We have a scheduled task to import data

/ / Import database data $schedule- > command (self::SIGNATURE)-> withoutOverlapping ()-> everyMinute ()-> runInBackground () per minute

Here, it is best to use runInBackground () for a long time to indicate asynchronous execution, which is actually a & symbol at the end of the shell script, which is completely dependent on the system on the linux.

WithoutOverlapping () is used here to indicate that only one task can be executed at a time. The main logic uses an exclusive lock implementation, which depends on our cache's driver. I use redis here, and then do it as the expiration of the lock directly provided by the redis.

There is a problem

This task is perfect under normal circumstances, because only one can run again and finish at the same time, but a scene appears.

One day, after our qa classmate had just deployed the environment, our server was silently importing the library, because when using withoutOverlapping ($expire_at=1440), a lock was generated in redis. This default parameter is the expiration time of the lock, and the default is one day. Then, because our docker environment needs to change the parameters and restart the back-end server service, it is more violent for us to restart, that is, to send a kill signal directly. Cause all the processes running inside the instant kill, and at this time our redis lock still exists, and it is about 1440 minutes, then when we restart server, we find that the lock has been there all the time, there is no way to carry out the follow-up operation, we can only wait.

Solve

Can I reduce the lock time? the original one day, I changed it to 30 minutes, no problem. We do the same at the beginning of the first version of the plan, and the authorities can do the same.

Later, we wondered whether we could achieve a monitoring program. After the process exited, it would be monitored to expire immediately, so that it would not have to be fixed for a fixed time. Of course, this is the ideal state of all software developers: if you want to open it, you will open it. I hung up the lock and removed it, whether normal or not.

Solution

Description:

When the command starts, get the pid of the process, and then fork the child process, and you can pass the pid to the child process.

The child process performs a probe every 10 seconds to get whether the id of the parent process is consistent with the incoming pid. Here is a common knowledge point: if the parent process exits abnormally, the child process will be taken over by the init process (pid=1) if it does not exit, so this is an orphan process.

At the same time, the child process will change the expiration time of the redis lock every time it is alive. If the probing interval is 10s, then our expiration time setting is 14s, a little more redundant time.

Thank you for your reading. the above is the content of "how to realize laravel Asynchronous Monitoring timing Scheduler". After the study of this article, I believe you have a deeper understanding of how to realize laravel Asynchronous Monitoring timing Scheduler, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report