摘要:背景介紹公司需要實現(xiàn)分鐘內每隔秒輪訓某個接口,自帶的貌似只精確到分鐘,雖然可以到精確到秒,但是并不滿足需求。選型公司項目都是基于框架,所以這個沒得選。
背景介紹
公司需要實現(xiàn)X分鐘內每隔Y秒輪訓某個接口,Linux自帶的crontab貌似只精確到分鐘,雖然可以到精確到秒,但是并不滿足需求。
選型公司項目都是 基于 Laravel 框架,所以這個沒得選。守護進程用的 supervisor,看看這個家伙能不能滿足我們的需求
代碼namespace AppConsoleCommands; use IlluminateConsoleCommand; use Cache; use CarbonCarbon; 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->id = $this->option("id") ?? "00"; $this->max = $this->option("max") ?? 32; $this->sleep = $this->option("sleep") ?? 700; $this->debug = $this->option("debug") ?? false; if ($this->id > $this->max) { return true; } while (true) { $this->doRun(); } } /** * * @param int $taskId * @return boolean */ protected function doRun() { $lock = sprintf("task:%03d:%s", $this->id, time()); $data = [ "id" => $this->id, "max" => $this->max, "time" => (new Carbon)->format("Y-m-d H:i:s.u"), "key" => $lock, ]; try { $result = cache()->get($lock); if ($result) { $data["message"] = "Task Has been executed."; $this->wait($this->sleep); return true; } cache()->put($lock, true, 2); $data["message"] = "Task Executed."; $this->logger($data); $this->wait($this->sleep); } catch (Exception $ex) { $data["message"] = $ex->getMessage(); cache()->put($data, true, 2); $this->wait($this->sleep); } } /** * 毫秒 * @param string $time */ protected function wait($time) { $wait = $time * 1000; usleep($wait); } protected function logger($message) { if($this->debug){ $time = (new Carbon)->format("Y-m-d H:i:s.u"); $this->line(array_get($message, "message") ." - ". $time); } logger()->stack(["task"])->debug(null, $message); } }進程守護
[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
上面是supervisor的配置效果圖
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輸出日志
[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."}
文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/29247.html
摘要:之前在內使用的隊列服務做了一個異步,處理一些內容審核的相關操作。但是每次重啟容器之后都需要進入內部啟動的隊列進程雖然文檔內有寫使用管理進程,但是并沒有那么做。。,最近需求上需要使用,所以決定使用來管理這些進程。所以使用了一個腳本,在執(zhí)行它。 之前在docker內使用lumen的隊列服務做了一個異步,處理一些內容審核的相關操作。但是每次重啟容器之后都需要進入docker內部啟動lumen...
摘要:之前在內使用的隊列服務做了一個異步,處理一些內容審核的相關操作。但是每次重啟容器之后都需要進入內部啟動的隊列進程雖然文檔內有寫使用管理進程,但是并沒有那么做。。,最近需求上需要使用,所以決定使用來管理這些進程。所以使用了一個腳本,在執(zhí)行它。 之前在docker內使用lumen的隊列服務做了一個異步,處理一些內容審核的相關操作。但是每次重啟容器之后都需要進入docker內部啟動lumen...
摘要:前言是一個客戶端服務器系統(tǒng),允許其用戶在類操作系統(tǒng)上控制許多進程。這將打印一個示例的配置文件到您的終端。不會自動生成配置文件。具體可見官方文檔后臺提供的后臺管理比較簡單大致功能有重啟啟動停止進程,打印日志,清除日志等。 showImg(https://segmentfault.com/img/bVbgXm1?w=242&h=45); 前言 Supervisor是一個客戶端/服務器系統(tǒng),...
閱讀 748·2021-10-14 09:43
閱讀 2072·2021-09-30 09:48
閱讀 3440·2021-09-08 09:45
閱讀 1089·2021-09-02 15:41
閱讀 1878·2021-08-26 14:15
閱讀 770·2021-08-03 14:04
閱讀 2972·2019-08-30 15:56
閱讀 3071·2019-08-30 15:52