本篇文章给大家带来的内容是关于基于Laravel框架下使用守护进程supervisor实现定时任务(毫秒),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

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

选型

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

代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

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->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);

    }

}

链接:https://pan.baidu.com/s/1v5gm7n0L7TGyejCmQrMh2g 提取码:x2p5

免费分享,但是X度限制严重,如若链接失效点击链接或搜索加群 群号518475424

进程守护

1

2

3

4

5

6

7

8

9

[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的配置

效果图

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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

输出日志

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

[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."}

基于Laravel框架下使用守护进程supervisor实现定时任务(毫秒)的更多相关文章

  1. asp.net core2.0 部署centos7/linux系统 --守护进程supervisor(二)

    原文:asp.net core2.0 部署centos7/linux系统 --守护进程supervisor(二) 续上一篇文章:asp.net core2.0 部署centos7/linux系统 -- ...

  2. 基于laravel框架构建最小内容管理系统

    校园失物招领平台开发 --基于laravel框架构建最小内容管理系统 摘要 ​ 针对目前大学校园人口密度大.人群活动频繁.师生学习生活等物品容易遗失的基本现状,在分析传统失物招领过程中的工作效率低下. ...

  3. 在C#/.NET应用程序开发中创建一个基于Topshelf的应用程序守护进程(服务)

    本文首发于:码友网--一个专注.NET/.NET Core开发的编程爱好者社区. 文章目录 C#/.NET基于Topshelf创建Windows服务的系列文章目录: C#/.NET基于Topshelf ...

  4. Laravel框架下容器Container 的依赖注入和反射应用

    依赖注入,简单说是把类里头依赖的对象,置于类外头,即客户端调用处.相当于把类与类解耦. 一个简单的例子: class A { public function __construct() { // 这种 ...

  5. Laravel框架下路由的使用(源码解析)

    本篇文章给大家带来的内容是关于Laravel框架下路由的使用(源码解析),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 前言 我的解析文章并非深层次多领域的解析攻略.但是参考着开发文 ...

  6. laravel 守护进程Supervisor的配置

    安装Supervisor Supervisor是Linux系统中常用的进程守护程序.如果队列进程queue:work意外关闭,它会自动重启启动队列进程.在Ubuntu安装Supervisor 非常简单 ...

  7. Ubuntu安装守护进程supervisor

    Supervisor安装与配置(Linux/Unix进程管理工具) asp.net core 负载均衡集群搭建(centos7+nginx+supervisor+kestrel) 为了保证服务能够稳定 ...

  8. 【Linux】- Ubuntu守护进程supervisor

    linux的守护进程类似于windows的服务.linux通过supervisor创建守护进程. 1.安装supervisor sudo apt-get install supervisor 安装成功 ...

  9. 基于dubbo框架下的RPC通讯协议性能测试

    一.前言 Dubbo RPC服务框架支持丰富的传输协议.序列化方式等通讯相关的配置和扩展.dubbo执行一次RPC请求的过程大致如下:消费者(Consumer)向注册中心(Registry)执行RPC ...

随机推荐

  1. mvc5 源码解析1:UrlRoutingModule

    注册在C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG \webconfig中 在该module源码中 我们可以看出注册了application ...

  2. java--set,Collections,map

    set 特点: 无序, 不允许重复 没有索引 Set<String> set = new HashSet<String>(); set.add("hello" ...

  3. 浅谈cookie 和 session

    一. cookie 定义:保存在浏览器本地上的一组组键值对 特点: 由服务器让浏览器进行设置的 浏览器保存在浏览器本地 下次访问时自动携带 应用: 登录 保存浏览习惯 简单的投票 使用cookie的原 ...

  4. c# 读取txt文件中文乱码解决方法

    之前做过一个项目,在程序运行目录下有个txt文件,文件内容是中文的时候会乱码, 后来用这个函数处理后,就不乱码了: private string GetPDA_Code()        {      ...

  5. 第3篇-超市管理系统Scrum冲刺博客

    一.站立式会议: 1.会议照片 2.昨天完成的工作 ①数据库方面:根据需求关系为在数据库中建立相关表的基本模型供后续参考. ②前端方面:完成了登录界面的设计:各个界面的草图:为各个界面选取合适的图片如 ...

  6. https://support.microsoft.com/zh-cn/help/2290714/error-message-when-you-install-office-2010-on-a-windows-7-based-comput

    Error message when you install Office 2010 on a Windows 7-based computer "The installation of M ...

  7. Factorization Machine算法

    参考: http://stackbox.cn/2018-12-factorization-machine/ https://baijiahao.baidu.com/s?id=1641085157432 ...

  8. LCD RGB 控制技术讲解 — 时钟篇(上)【转】

    1. 时序图 下面是LCD RGB 控制的典型时序图 天啊,一下就上这玩意,怎么看??? 其实要解释上面的时序图,我们还需要了解一些LCD的显示过程.所以现在只是有个印象,稍后我们详细讲解. 2. L ...

  9. xshell 远程登陆CentOS7 免密登陆

    首先说一下大体的思路: 1. 以密码登陆CentOS系统 2. 配置ssh 3. xshell 生成秘钥 4. 进行免密登陆 软件.设备: xshell(下载地址(免费版),也可以自行百度下载) Ce ...

  10. 远程唤醒、WOL、Magic_Packet【转】

    转自:https://www.cnblogs.com/zhuimengle/p/5898830.html 原文:http://blog.csdn.net/flyoxs/article/details/ ...