-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.m
More file actions
128 lines (116 loc) · 4.99 KB
/
main.m
File metadata and controls
128 lines (116 loc) · 4.99 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/*
+----------------------------------------------------------------------+
| Name: main.m |
+----------------------------------------------------------------------+
| Comment: 主程序 |
+----------------------------------------------------------------------+
| Author:Odin |
+----------------------------------------------------------------------+
| Created:2012-05-09 23:20:45 |
+----------------------------------------------------------------------+
| Last-Modified:2013-07-29 22:15:08 |
+----------------------------------------------------------------------+
*/
error_reporting(0);
define('_DAEMON_ROOT', dirname(__FILE__).'/');
//const
include_once(_DAEMON_ROOT.'inc/const.m');
//functions
include_once(_DAEMON_ROOT.'fun/log.m');
include_once(_DAEMON_ROOT.'fun/base.m');
include_once(_DAEMON_ROOT.'fun/daemon.m');
include_once(_DAEMON_ROOT.'fun/file.m');
include_once(_DAEMON_ROOT.'fun/time.m');
include_once(_DAEMON_ROOT.'fun/utils.m');
include_once(_DAEMON_ROOT.'fun/omq.m');
include_once(_DAEMON_ROOT.'fun/omqpoll.m');
#include_once(_DAEMON_ROOT.'fun/ip.m');
/* {{{ 初始化(读取配置项等)
*/
include_once(_DAEMON_ROOT.'modules/initDaemon.m');
/* }}} */
// Daemonize
include_once(_DAEMON_ROOT.'modules/daemonize.m');
/* {{{ 这时确定model
*/
if (isset($GLOBALS['DATA'])) { //如果没有配置数据层,可以不连
include_once(_DAEMON_ROOT._SUBDIR_DATA.'/IData.m');
include_once($GLOBALS['DATA']['file']);
}
/* }}} */
/* {{{ 后加载
* 这些文件需要放在ROOT目录下,并且以path=file形式出现,如果一个目录下有多个文件,用','分隔
*/
if (!empty($GLOBALS['OPTIONS']['postload'])) {
foreach ($GLOBALS['OPTIONS']['postload'] as $postPath=>$postBases) {
$bases=explode(',',$postBases);
foreach ($bases as $base) {
$postFile=$GLOBALS['_daemon']['_WORKERROOT_'].'/'.$postPath.'/'.$base;
if (@include_once($postFile)) {
_debug("[post:$postFile][loaded]",_DLV_NOTICE);
} else {
_debug("[post:$postFile][load_fail]",_DLV_NOTICE);
}
}
}
}
/* }}} */
$loop=0;
while($GLOBALS['_daemon']['masterRun']===true) {
$loop++;
// In a real world scenario we would do some sort of conditional launch.
// Maybe a condition in a DB is met, or whatever, here we're going to
// cap the number of concurrent grandchildren
$workerRun=false;
foreach ($GLOBALS['_daemon']['runningWorkers'] as $workerTitle=>$workerStatus) {
if ($workerStatus['wcount']>0) {
//可能一个脚本需要fork多个worker
for ($wSN=1;$wSN<=$workerStatus['wcount'];$wSN++) {
if ($workerStatus["#{$wSN}"]['stat']===_PSTAT_STANDBY) { //说明这个worker等着被启动...
$workerRun=true; //只要有一个worker还在跑,就标记workerRun为true
//spawn worker
_debug("[{$workerStatus["#{$wSN}"]['title']}][spawn_it]",_DLV_NOTICE);
$wPid=_spawnWorker($workerStatus["#{$wSN}"]);
if (-1 === $wPid) {
_debug("[{$workerTitle}][#{$wSN}][spawn_failed]",_DLV_EMERG);
} else {
_debug("[{$workerTitle}][#{$wSN}][pid:$wPid][spawn_successful]",_DLV_NOTICE);
$GLOBALS['_daemon']['runningWorkers'][$workerTitle]["#{$wSN}"]['stat']=_PSTAT_RUNNING;
$GLOBALS['_daemon']['runningWorkers'][$workerTitle]["#{$wSN}"]['pid']=$wPid;
$GLOBALS['_daemon']['runningWorkers']['_pids'][$wPid]="{$workerTitle}#{$wSN}";
}
} elseif ($workerStatus["#{$wSN}"]['stat']===_PSTAT_RUNNING) {
$workerRun=true;
}
}
}
}
_iterate(0.5);
pcntl_signal_dispatch();
if ($workerRun===false) { //没有worker在运行了,完成历史使命,退出
_debug("[no_worker_running]",_DLV_CRIT);
$GLOBALS['_daemon']['masterRun']=false;
}
// show status, once per 5000
if ($loop%5000==0) {
$pidArr=array();
$titleArr=array();
foreach ($GLOBALS['_daemon']['runningWorkers'] as $key=>$value) {
if ($key=='_pids') {
foreach ($value as $workerPid=>$workerInfo) {
$pidArr[]="{$workerPid}:{$workerInfo}";
}
$pidStr=implode(',',$pidArr);
} else {
$wTitle=$key;
foreach ($value as $snStr=>$snDetail) {
$titleArr[]="{$wTitle}:{$snStr}:{$snDetail['pid']}:{$snDetail['stat']}";
}
}
}
$titleStr=implode(',',$titleArr);
_debug("[MAIN][{$pidStr}][{$titleStr}]",_DLV_NOTICE);
}
}
_shutdown(0);