-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsms.php
More file actions
168 lines (138 loc) · 4.71 KB
/
Copy pathsms.php
File metadata and controls
168 lines (138 loc) · 4.71 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
class CALLWorker extends Worker {
protected static $client;
public function __construct() {
}
public function run(){
require(ROOTDIR.'/config/webservice.config.php');
$soap['location'] = 'http://api.hx9999.com/backend/Sms_queue';
self::$client = new SoapClient(null, $soap);
}
protected function getConnection(){
return self::$client;
}
}
/* the collectable class implements machinery for Pool::collect */
class Sendsms extends Stackable {
public function __construct($row) {
$this->data = $row;
}
public function run()
{
try{
$client = $this->worker->getConnection();
#暂时不开通
$result = $client->send($this->data['id']);
$str = sprintf("%s,%s,%s\n", date('Y-m-d H:i:s'), $this->data['id'],'flag:'.$result['flag'].',msg:'.$result['msg'].',line:'.$result['line']);
}catch(Exception $e){
$msg = "Caught exception:{$e->getMessage()}";
$str = sprintf("%s,【%s】,%s\n", date('Y-m-d H:i:s'), $this->data['id'],$msg);
}
file_put_contents('/www/hx9999.com/log.hx9999.com/sms.' . date("Y-m-d") . '.log', $str, FILE_APPEND);
}
}
class Client {
public static function main() {
$str = sprintf("%s,%s\n", date('Y-m-d H:i:s'), '自动发送短信多线程开始' );
file_put_contents('/www/hx9999.com/log.hx9999.com/sms.' . date("Y-m-d") . '.log', $str, FILE_APPEND);
require(DBCONFIG);
$pool = new Pool(MAX_CONCURRENCY_JOB, \CALLWorker::class, []);
try {
$mysqli = new mysqli($dbhost,$dbuser,$dbpw,$dbname);
if ($mysqli->connect_errno) {
$str = sprintf("%s,%s\n", date('Y-m-d H:i:s'), '数据库连接出错' );
file_put_contents('/www/hx9999.com/log.hx9999.com/sms.' . date("Y-m-d") . '.log', $str, FILE_APPEND);
}else{
$sql = "select id from sms_queue where `status`='New' order by id desc limit 500";
if ($query = $mysqli->query($sql)){
while($row=$query->fetch_assoc())
{
$pool->submit(new Sendsms($row));
}
$pool->shutdown();
$query->free();
}
$mysqli->close();
$str = sprintf("%s,%s\n", date('Y-m-d H:i:s'), '自动发送短信多线程结束,准备进入睡眠5秒中' );
file_put_contents('/www/hx9999.com/log.hx9999.com/sms.' . date("Y-m-d") . '.log', $str, FILE_APPEND);
}
} catch (Exception $e) {
$str = sprintf("%s,%s\n", date('Y-m-d H:i:s'), ',【系统错误,数据库操作出现故障,自动发送短信多线程结束,准备进入睡眠5秒中】' . $e->getMessage() );
file_put_contents('/www/hx9999.com/log.hx9999.com/sms.' . date("Y-m-d") . '.log', $str, FILE_APPEND);
}
//睡眠
sleep(5);
}
}
class Daemon {
/* config */
const LISTEN = "0.0.0.0";
const MAXCONN = 100;
const pidfile = 'sms';
const uid = 80;
const gid = 80;
protected $pool = NULL;
protected $zmq = NULL;
public function __construct() {
$this->pidfile = '/var/run/'.self::pidfile.'.pid';
}
private function daemon(){
if (file_exists($this->pidfile)) {
echo "The file $this->pidfile exists.\n";
exit();
}
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// we are the parent
//pcntl_wait($status); //Protect against Zombie children
exit($pid);
} else {
// we are the child
file_put_contents($this->pidfile, getmypid());
posix_setuid(self::uid);
posix_setgid(self::gid);
return(getmypid());
}
}
private function start(){
$pid = $this->daemon();
while( true ){
Client::main();
}
}
private function stop(){
if (file_exists($this->pidfile)) {
$pid = file_get_contents($this->pidfile);
posix_kill($pid, 9);
unlink($this->pidfile);
}
}
private function help($proc){
printf("%s start | stop | help \n", $proc);
}
public function main($argv){
if(count($argv) < 2){
printf("please input help parameter\n");
exit();
}
if($argv[1] === 'stop'){
$this->stop();
}else if($argv[1] === 'start'){
$this->start();
}else{
$this->help($argv[0]);
}
}
}
//项目根目录
define('ROOTDIR',realpath(dirname(dirname(__FILE__))));
//数据库配置文件
define('DBCONFIG',ROOTDIR.'/config/db.config.php');
// 定义可以同时执行的进程数量
define('MAX_CONCURRENCY_JOB', 20);
define('SMS_LOG','/www/hx9999.com/log.hx9999.com/sms.' . date("Y-m-d") . '.log');
$Daemon = new Daemon();
$Daemon->main($argv);
?>