-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.php
More file actions
104 lines (88 loc) · 1.84 KB
/
Copy pathtimer.php
File metadata and controls
104 lines (88 loc) · 1.84 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
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
<?php
//定时器
class Timer{
//任务队列
private static $task = array();
private static function installSignal(){
pcntl_signal(SIGALRM,array('Timer','signalHandler'),false);
}
private static function signalHandler($sig){
self::doTask();
}
/**
* 添加事件队列
* @param [type] $obj [description]
*/
public static function add(Task $obj){
self::$task[$obj->getTaskId()] = $obj;
}
/**
* 删除队列
* @param [type] $obj [description]
* @return [type] [description]
*/
public static function del(Task $obj){
if(array_key_exists($obj->getTaskId(), self::$task)){
self::$task[$obj->getTaskId()] = null;
}
}
/**
* 执行队列
* @return [type] [description]
*/
public static function doTask(){
foreach (self::$task as &$value) {
if(($value->getLastTime() + $value->getInterval()) < time()){
$value->setLastTime(time());
require $value->getObj();
}
}
pcntl_alarm(1); //继续
}
public static function run(){
self::installSignal();
pcntl_alarm(1);
while(1){
pcntl_signal_dispatch();
}
}
}
class Task{
private $taskId;
private $obj; //执行路径
private $interval; //秒
private $lastTime;
public function setTaskId($taskId){
$this->taskId = $taskId;
}
public function getTaskId(){
if(empty($this->taskId)){
return md5($this->obj);
}
return $this->taskId;
}
public function setObj($obj){
$this->obj = $obj;
}
public function getObj(){
return $this->obj;
}
public function setInterval($interval){
$this->interval = $interval;
}
public function getInterval(){
if($this->interval <=0 ){
exit("时间间隔未设置\n");
}
return $this->interval;
}
public function setLastTime($time){
$this->lastTime = $time;
}
public function getLastTime(){
if(empty($this->lastTime)){
$this->lastTime = time();
}
return $this->lastTime;
}
}