资讯专栏INFORMATION COLUMN

基于supervisor秒级Laravel定时任务

leeon / 1980人阅读

摘要:背景介绍公司需要实现分钟内每隔秒轮训某个接口,自带的貌似只精确到分钟,虽然可以到精确到秒,但是并不满足需求。选型公司项目都是基于框架,所以这个没得选。

背景介绍

公司需要实现X分钟内每隔Y秒轮训某个接口,Linux自带的crontab貌似只精确到分钟,虽然可以到精确到秒,但是并不满足需求。

选型

公司项目都是 基于 Laravel 框架,所以这个没得选。守护进程用的 supervisor,看看这个家伙能不能满足我们的需求

代码

</>复制代码

  1. namespace AppConsoleCommands;
  2. use IlluminateConsoleCommand;
  3. use Cache;
  4. use CarbonCarbon;
  5. class TaskCommand extends Command {
  6. /**
  7. * The name and signature of the console command.
  8. *
  9. * @var string
  10. */
  11. protected $signature = "ue:task
  12. {--id= : 当前编号}
  13. {--max= : 最大线程}
  14. {--sleep= : 休眠多少毫秒}
  15. {--debug= : 是否调试模式}
  16. ";
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = "Command description";
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct() {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle() {
  37. $this->id = $this->option("id") ?? "00";
  38. $this->max = $this->option("max") ?? 32;
  39. $this->sleep = $this->option("sleep") ?? 700;
  40. $this->debug = $this->option("debug") ?? false;
  41. if ($this->id > $this->max) {
  42. return true;
  43. }
  44. while (true) {
  45. $this->doRun();
  46. }
  47. }
  48. /**
  49. *
  50. * @param int $taskId
  51. * @return boolean
  52. */
  53. protected function doRun() {
  54. $lock = sprintf("task:%03d:%s", $this->id, time());
  55. $data = [
  56. "id" => $this->id,
  57. "max" => $this->max,
  58. "time" => (new Carbon)->format("Y-m-d H:i:s.u"),
  59. "key" => $lock,
  60. ];
  61. try {
  62. $result = cache()->get($lock);
  63. if ($result) {
  64. $data["message"] = "Task Has been executed.";
  65. $this->wait($this->sleep);
  66. return true;
  67. }
  68. cache()->put($lock, true, 2);
  69. $data["message"] = "Task Executed.";
  70. $this->logger($data);
  71. $this->wait($this->sleep);
  72. } catch (Exception $ex) {
  73. $data["message"] = $ex->getMessage();
  74. cache()->put($data, true, 2);
  75. $this->wait($this->sleep);
  76. }
  77. }
  78. /**
  79. * 毫秒
  80. * @param string $time
  81. */
  82. protected function wait($time) {
  83. $wait = $time * 1000;
  84. usleep($wait);
  85. }
  86. protected function logger($message) {
  87. if($this->debug){
  88. $time = (new Carbon)->format("Y-m-d H:i:s.u");
  89. $this->line(array_get($message, "message") ." - ". $time);
  90. }
  91. logger()->stack(["task"])->debug(null, $message);
  92. }
  93. }
进程守护

</>复制代码

  1. [program:task-worker]
  2. process_name=%(program_name)s_%(process_num)02d
  3. command=/usr/bin/php /home/wwwroot/demo/artisan ue:task --id=%(process_num)02d --max=8
  4. autostart=true
  5. autorestart=true
  6. user=www
  7. numprocs=8
  8. redirect_stderr=true
  9. stdout_logfile=/home/wwwroot/demo/storage/logs/worker.log

</>复制代码

  1. 上面是supervisor的配置
效果图

</>复制代码

  1. Task Executed. - 2018-08-14 22:17:18.985094
  2. Task Executed. - 2018-08-14 22:17:19.336115
  3. Task Executed. - 2018-08-14 22:17:20.038236
  4. Task Executed. - 2018-08-14 22:17:21.090470
  5. Task Executed. - 2018-08-14 22:17:22.142716
  6. Task Executed. - 2018-08-14 22:17:23.195126
  7. Task Executed. - 2018-08-14 22:17:24.247698
  8. Task Executed. - 2018-08-14 22:17:25.300066
  9. Task Executed. - 2018-08-14 22:17:26.352638
  10. Task Executed. - 2018-08-14 22:17:27.054124
  11. Task Executed. - 2018-08-14 22:17:28.106420
  12. Task Executed. - 2018-08-14 22:17:29.158906
  13. Task Executed. - 2018-08-14 22:17:30.211438
  14. Task Executed. - 2018-08-14 22:17:31.263542
  15. Task Executed. - 2018-08-14 22:17:32.315923
  16. Task Executed. - 2018-08-14 22:17:33.017096
  17. Task Executed. - 2018-08-14 22:17:34.068963
  18. Task Executed. - 2018-08-14 22:17:35.121267
  19. Task Executed. - 2018-08-14 22:17:36.173600
  20. Task Executed. - 2018-08-14 22:17:37.226165
输出日志

</>复制代码

  1. [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."}
  2. [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."}
  3. [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."}
  4. [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."}
  5. [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."}
  6. [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."}
  7. [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."}
  8. [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."}
  9. [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."}
  10. [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."}
  11. [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."}
  12. [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."}
  13. [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."}
  14. [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."}
  15. [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."}
  16. [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."}
  17. [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."}
  18. [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."}
  19. [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."}
  20. [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."}
  21. [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."}
  22. [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."}
  23. [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."}
  24. [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."}
  25. [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."}
  26. [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."}
  27. [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."}
  28. [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."}
  29. [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."}
  30. [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."}
  31. [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."}
  32. [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."}
  33. [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."}
  34. [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."}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/29247.html

相关文章

  • Docker 使用 supervisord 管理 lumen队列与crontab

    摘要:之前在内使用的队列服务做了一个异步,处理一些内容审核的相关操作。但是每次重启容器之后都需要进入内部启动的队列进程虽然文档内有写使用管理进程,但是并没有那么做。。,最近需求上需要使用,所以决定使用来管理这些进程。所以使用了一个脚本,在执行它。 之前在docker内使用lumen的队列服务做了一个异步,处理一些内容审核的相关操作。但是每次重启容器之后都需要进入docker内部启动lumen...

    cyqian 评论0 收藏0
  • Docker 使用 supervisord 管理 lumen队列与crontab

    摘要:之前在内使用的队列服务做了一个异步,处理一些内容审核的相关操作。但是每次重启容器之后都需要进入内部启动的队列进程虽然文档内有写使用管理进程,但是并没有那么做。。,最近需求上需要使用,所以决定使用来管理这些进程。所以使用了一个脚本,在执行它。 之前在docker内使用lumen的队列服务做了一个异步,处理一些内容审核的相关操作。但是每次重启容器之后都需要进入docker内部启动lumen...

    hlcc 评论0 收藏0
  • Supervisor 从入门到放弃

    摘要:前言是一个客户端服务器系统,允许其用户在类操作系统上控制许多进程。这将打印一个示例的配置文件到您的终端。不会自动生成配置文件。具体可见官方文档后台提供的后台管理比较简单大致功能有重启启动停止进程,打印日志,清除日志等。 showImg(https://segmentfault.com/img/bVbgXm1?w=242&h=45); 前言 Supervisor是一个客户端/服务器系统,...

    whjin 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<