-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBluePrint.php
More file actions
71 lines (60 loc) · 1.27 KB
/
BluePrint.php
File metadata and controls
71 lines (60 loc) · 1.27 KB
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
<?php
/**
* Created by PhpStorm.
* User: yzw
* Date: 2018/4/1 0001
* Time: 下午 21:33
*/
include "JobExample.php";
class BluePrint
{
protected $isDaemon;
protected $pid;
public function __construct()
{
$this->setPid();
}
//todo
public function daemon()
{
}
protected function setPid()
{
$this->pid = posix_getpid();
file_put_contents("worker_pid",$this->pid);
}
public function run()
{
while(true){
$taskList = $this->getTaskList();
if(empty($taskList)){
$this->stop();
}
$this->consume($taskList);
}
}
protected function consume($taskList)
{
while(!empty($taskList)){
$job = array_pop($taskList);
$job = unserialize($job);
$job->handle();
}
}
protected function stop()
{
posix_kill($this->pid,SIGSTOP);
}
protected function getTaskList()
{
//get tasklist from something like redis or database
$redis = new Redis();
$redis->connect("127.0.0.1",6379);
$result = [];
if($result = $redis->lrange("task_list",0,-1)){
$redis->delete("task_list");
}
return $result;
}
}
(new BluePrint())->run();