Table of Contents
framework, so there is no choice. The daemon process uses
The above is the configuration of the supervisor
Rendering
Related recommendations:
Home PHP Framework Laravel Using the daemon supervisor to implement scheduled tasks (milliseconds) based on the Laravel framework

Using the daemon supervisor to implement scheduled tasks (milliseconds) based on the Laravel framework

Aug 16, 2018 am 10:14 AM
laravel php redis supervisor

The content of this article is about using the daemon supervisor to implement scheduled tasks (milliseconds) based on the Laravel framework. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The company needs to implement rotation training of an interface every Y seconds within , but it does not meet the needs. SelectionThe company's projects are all based on the

Laravel

framework, so there is no choice. The daemon process uses

supervisor

, let’s see if this guy can meet our needsCode<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">namespace App\Console\Commands; use Illuminate\Console\Command; use Cache; use Carbon\Carbon; class TaskCommand extends Command {     /**      * The name and signature of the console command.      *      * @var string      */     protected $signature = 'ue:task         {--id=      : 当前编号}         {--max=     : 最大线程}         {--sleep=   : 休眠多少毫秒}         {--debug=   : 是否调试模式}         ';     /**      * The console command description.      *      * @var string      */     protected $description = 'Command description';     /**      * Create a new command instance.      *      * @return void      */     public function __construct() {         parent::__construct();     }     /**      * Execute the console command.      *      * @return mixed      */     public function handle() {         $this-&gt;id       = $this-&gt;option('id') ?? '00';         $this-&gt;max      = $this-&gt;option('max') ?? 32;         $this-&gt;sleep    = $this-&gt;option('sleep') ?? 700;         $this-&gt;debug    = $this-&gt;option('debug') ?? false;         if ($this-&gt;id &gt; $this-&gt;max) {             return true;         }         while (true) {             $this-&gt;doRun();         }     }     /**      *       * @param int $taskId      * @return boolean      */     protected function doRun() {         $lock = sprintf('task:%03d:%s', $this-&gt;id, time());         $data = [             'id' =&gt; $this-&gt;id,             'max' =&gt; $this-&gt;max,             'time'  =&gt; (new Carbon)-&gt;format('Y-m-d H:i:s.u'),             'key' =&gt; $lock,         ];         try {             $result = cache()-&gt;get($lock);             if ($result) {                 $data['message'] = 'Task Has been executed.';                 $this-&gt;wait($this-&gt;sleep);                 return true;             }             cache()-&gt;put($lock, true, 2);             $data['message'] = 'Task Executed.';             $this-&gt;logger($data);             $this-&gt;wait($this-&gt;sleep);         } catch (\Exception $ex) {             $data['message'] = $ex-&gt;getMessage();             cache()-&gt;put($data, true, 2);             $this-&gt;wait($this-&gt;sleep);         }     }     /**      * 毫秒      * @param string $time      */     protected function wait($time) {         $wait = $time * 1000;         usleep($wait);     }     protected function logger($message) {         if($this-&gt;debug){             $time   = (new Carbon)-&gt;format('Y-m-d H:i:s.u');             $this-&gt;line(array_get($message, 'message') .' - '. $time);         }         logger()-&gt;stack(['task'])-&gt;debug(null, $message);     } }</pre><div class="contentsignin">Copy after login</div></div>Process Guardian

[program:task-worker]
process_name=%(program_name)s_%(process_num)02d
command=/usr/bin/php /home/wwwroot/demo/artisan ue:task --id=%(process_num)02d --max=8
autostart=true
autorestart=true
user=www
numprocs=8
redirect_stderr=true
stdout_logfile=/home/wwwroot/demo/storage/logs/worker.log
Copy after login

The above is the configuration of the supervisor

Rendering

Task Executed. - 2018-08-14 22:17:18.985094
Task Executed. - 2018-08-14 22:17:19.336115
Task Executed. - 2018-08-14 22:17:20.038236
Task Executed. - 2018-08-14 22:17:21.090470
Task Executed. - 2018-08-14 22:17:22.142716
Task Executed. - 2018-08-14 22:17:23.195126
Task Executed. - 2018-08-14 22:17:24.247698
Task Executed. - 2018-08-14 22:17:25.300066
Task Executed. - 2018-08-14 22:17:26.352638
Task Executed. - 2018-08-14 22:17:27.054124
Task Executed. - 2018-08-14 22:17:28.106420
Task Executed. - 2018-08-14 22:17:29.158906
Task Executed. - 2018-08-14 22:17:30.211438
Task Executed. - 2018-08-14 22:17:31.263542
Task Executed. - 2018-08-14 22:17:32.315923
Task Executed. - 2018-08-14 22:17:33.017096
Task Executed. - 2018-08-14 22:17:34.068963
Task Executed. - 2018-08-14 22:17:35.121267
Task Executed. - 2018-08-14 22:17:36.173600
Task Executed. - 2018-08-14 22:17:37.226165
Copy after login
Output log
[2018-08-14 22:12:24] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:24.389224","key":"task:001:1534255944","message":"Task Executed."} 
[2018-08-14 22:12:25] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:25.390158","key":"task:001:1534255945","message":"Task Executed."} 
[2018-08-14 22:12:26] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:26.391594","key":"task:001:1534255946","message":"Task Executed."} 
[2018-08-14 22:12:27] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:27.393196","key":"task:001:1534255947","message":"Task Executed."} 
[2018-08-14 22:12:28] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:28.395124","key":"task:001:1534255948","message":"Task Executed."} 
[2018-08-14 22:12:29] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:29.396796","key":"task:001:1534255949","message":"Task Executed."} 
[2018-08-14 22:12:30] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:30.398666","key":"task:001:1534255950","message":"Task Executed."} 
[2018-08-14 22:12:31] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:31.400561","key":"task:001:1534255951","message":"Task Executed."} 
[2018-08-14 22:12:32] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:32.402462","key":"task:001:1534255952","message":"Task Executed."} 
[2018-08-14 22:12:33] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:33.404092","key":"task:001:1534255953","message":"Task Executed."} 
[2018-08-14 22:12:34] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:34.405550","key":"task:001:1534255954","message":"Task Executed."} 
[2018-08-14 22:12:35] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:35.407197","key":"task:001:1534255955","message":"Task Executed."} 
[2018-08-14 22:12:36] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:36.408920","key":"task:001:1534255956","message":"Task Executed."} 
[2018-08-14 22:12:37] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:37.410841","key":"task:001:1534255957","message":"Task Executed."} 
[2018-08-14 22:12:38] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:38.412764","key":"task:001:1534255958","message":"Task Executed."} 
[2018-08-14 22:12:39] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:39.414518","key":"task:001:1534255959","message":"Task Executed."} 
[2018-08-14 22:12:40] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:40.416229","key":"task:001:1534255960","message":"Task Executed."} 
[2018-08-14 22:12:41] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:41.418001","key":"task:001:1534255961","message":"Task Executed."} 
[2018-08-14 22:12:42] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:42.419476","key":"task:001:1534255962","message":"Task Executed."} 
[2018-08-14 22:12:43] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:43.421388","key":"task:001:1534255963","message":"Task Executed."} 
[2018-08-14 22:12:44] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:44.423164","key":"task:001:1534255964","message":"Task Executed."} 
[2018-08-14 22:12:45] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:45.424798","key":"task:001:1534255965","message":"Task Executed."} 
[2018-08-14 22:12:46] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:46.426667","key":"task:001:1534255966","message":"Task Executed."} 
[2018-08-14 22:12:47] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:47.428553","key":"task:001:1534255967","message":"Task Executed."} 
[2018-08-14 22:12:48] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:48.430427","key":"task:001:1534255968","message":"Task Executed."} 
[2018-08-14 22:12:49] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:49.432118","key":"task:001:1534255969","message":"Task Executed."} 
[2018-08-14 22:12:50] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:50.433893","key":"task:001:1534255970","message":"Task Executed."} 
[2018-08-14 22:12:51] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:51.435711","key":"task:001:1534255971","message":"Task Executed."} 
[2018-08-14 22:12:52] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:52.437015","key":"task:001:1534255972","message":"Task Executed."} 
[2018-08-14 22:12:53] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:53.438352","key":"task:001:1534255973","message":"Task Executed."} 
[2018-08-14 22:12:54] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:54.439989","key":"task:001:1534255974","message":"Task Executed."} 
[2018-08-14 22:12:55] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:55.441580","key":"task:001:1534255975","message":"Task Executed."} 
[2018-08-14 22:12:56] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:56.443116","key":"task:001:1534255976","message":"Task Executed."} 
[2018-08-14 22:12:57] local.DEBUG:  {"id":"1","max":"32","time":"2018-08-14 22:12:57.445006","key":"task:001:1534255977","message":"Task Executed."}
Copy after login

Use PHP to implement daemon task background running and multi-threading (php- resque usage instructions)

php scheduled task framework sharing

implementation of supervisor execution asynchronous process in php framework Laravel

The above is the detailed content of Using the daemon supervisor to implement scheduled tasks (milliseconds) based on the Laravel framework. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

What versions of laravel are there? How to choose the version of laravel for beginners What versions of laravel are there? How to choose the version of laravel for beginners Apr 18, 2025 pm 01:03 PM

In the Laravel framework version selection guide for beginners, this article dives into the version differences of Laravel, designed to assist beginners in making informed choices among many versions. We will focus on the key features of each release, compare their pros and cons, and provide useful advice to help beginners choose the most suitable version of Laravel based on their skill level and project requirements. For beginners, choosing a suitable version of Laravel is crucial because it can significantly impact their learning curve and overall development experience.

The difference between laravel and thinkphp The difference between laravel and thinkphp Apr 18, 2025 pm 01:09 PM

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

Laravel framework skills sharing Laravel framework skills sharing Apr 18, 2025 pm 01:12 PM

In this era of continuous technological advancement, mastering advanced frameworks is crucial for modern programmers. This article will help you improve your development skills by sharing little-known techniques in the Laravel framework. Known for its elegant syntax and a wide range of features, this article will dig into its powerful features and provide practical tips and tricks to help you create efficient and maintainable web applications.

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Redis's Role: Exploring the Data Storage and Management Capabilities Redis's Role: Exploring the Data Storage and Management Capabilities Apr 22, 2025 am 12:10 AM

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Laravel user login function list Laravel user login function list Apr 18, 2025 pm 01:06 PM

Building user login capabilities in Laravel is a crucial task and this article will provide a comprehensive overview covering every critical step from user registration to login verification. We will dive into the power of Laravel’s built-in verification capabilities and guide you through customizing and extending the login process to suit specific needs. By following these step-by-step instructions, you can create a secure and reliable login system that provides a seamless access experience for users of your Laravel application.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

See all articles