-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.php
116 lines (94 loc) · 2.66 KB
/
demo.php
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
107
108
109
110
111
112
113
114
115
116
<?php
//namespace Shop\Crontab;
/**
* @version 1.0 2014-11-13
* @author shaogx
*/
define('VERSION', '1.0.0');
if (!defined('DEBUG')) {
define("DEBUG", false);
}
if (!defined("ROOT_CRON")) {
define("ROOT_CRON", realpath(__DIR__));
}
try {
$line = "start pid:[" . posix_getppid() . "->" . getmypid() . "] memory:[" . _memory() . "] at " . date('Y-m-d H:i:s', time()) . "\n";
echo ($line); //echo or file
// load class
include_once ROOT_CRON . '/library/Command.php';
include_once ROOT_CRON . '/library/Worker.php';
include_once ROOT_CRON . '/library/Process.php';
include_once ROOT_CRON . '/library/Stack.php';
include_once ROOT_CRON . '/library/Pool.php';
include_once ROOT_CRON . '/library/Queue.php';
include_once ROOT_CRON . '/library/Chan.php';
function demoWorker() {
//code goes here...
}
/**
* worker master - Pool
*/
function workerPool() {
$start = microtime(true);
echo "\nworker start at {$start}";
$jobs = array();
if ($jobs) {
$wNum = 5; //进程数
$jobCnt = count($jobs);
$w = ceil($jobCnt / $wNum); //
$pool = new Pool("demoWorker", $w);
$pool->addJobs($jobs);
$pool->waiting();
$stop = microtime(true);
$cost = $stop - $start;
echo "\nworker {$jobCnt} jobs done at {$stop}";
echo "\nworker cost time = {$cost}";
} else {
//for daemon mode
echo "\nworker no job and sleep 5s";
sleep(5);
}
$end = microtime(true);
echo "\nworker end at {$end}";
}
/**
* worker master - Chan
*/
function workerChan() {
$start = microtime(true);
echo "\nworkerChan start at {$start}";
$jobs = array(
'0' => array('1', '2', '3'), //first chan
'1' => array('1', '2', '3'), //second chan
'2' => array('1', '2', '3'), //third chan
//....
);
if ($jobs) {
$chanCnt = count($jobs);
if ($chanCnt && $jobs) {
$jobCnt = count($jobs, COUNT_RECURSIVE);
$chan = new Chan("demoWorker", $jobs);
$chan->waiting();
$stop = microtime(true);
$cost = $stop - $start;
echo "\nworkerChan {$jobCnt} jobs done at {$stop}";
echo "\nworkerChan {$jobCnt} cost time = {$cost}";
}
} else {
//for daemon mode
echo "\nworkerChan no job and sleep 5s";
sleep(5);
}
$end = microtime(true);
echo "\nworkerChan end at {$end}";
}
$daemon = new Worker('demo', $daemon = true);
$daemon->setup('workerPool');
$daemon->setup('workerChan');
$daemon->bootstrap($argv);
$line = "end pid:[" . posix_getppid() . "->" . getmypid() . "] memory:[" . _memory() . "] at " . date('Y-m-d H:i:s', time()) . "\n";
echo ($line); //echo or file
} catch (\Phalcon\Exception $e) {
$line = 'error:time:' . date('Y-m-d H:i:s', time()) . ';' . $e->getMessage() . "\n";
echo ($line); //echo or file
}