本篇文章给大家带来的内容是关于基于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;
u
illuminate\console\command;
u
cache;
u
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'
) ?? fal;
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
);
}
}
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
ur=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.39866孟桥6"
,
"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"
,
"k手机短片视频下载ey"
:
"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."
}
本文发布于:2023-04-07 20:04:57,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/ab51106af4c672fb039261dbc482f59c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:基于Laravel框架下使用守护进程supervisor实现定时任务(毫秒).doc
本文 PDF 下载地址:基于Laravel框架下使用守护进程supervisor实现定时任务(毫秒).pdf
留言与评论(共有 0 条评论) |