-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SwController.php
78 lines (70 loc) · 2.06 KB
/
SwController.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
<?php
namespace parker714\yii2s;
use Yii;
use yii\console\Controller;
use parker714\yii2s\servers\Server;
use yii\helpers\Console;
/**
* Class SwController
*
* @package parker714\yii2s
*/
class SwController extends Controller
{
/**
* Sw server instance
*
* @var Server
*/
public $server;
/**
* Init sw server instance
*
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
parent::init();
$this->server = Yii::createObject($this->server);
$this->server->swEvents = array_merge($this->server->coreEvents(), $this->server->swEvents);
$this->server->set = array_merge($this->server->coreSets(), $this->server->set);
$this->server->set['pid_file'] = Yii::getAlias($this->server->set['pid_file']);
$this->server->set['log_file'] = Yii::getAlias($this->server->set['log_file']);
}
/**
* Sw server dispatcher
*
* @param $command
*
* @return bool|int
* @throws \Exception
*/
public function actionServer($command)
{
$pidFile = $this->server->getPidFile();
$pid = file_exists($pidFile) ? file_get_contents($pidFile) : null;
switch ($command) {
case 'start':
if (!is_null($pid) && posix_kill($pid, 0)) {
return $this->stdout("server(pid: {$pid}) already start\n", Console::FG_RED);
}
$this->server->start();
break;
case 'stop':
if (!is_null($pid)) {
posix_kill($pid, SIGTERM);
}
break;
case 'reload':
if (!is_null($pid)) {
// reload all worker
posix_kill($pid, SIGUSR1);
// reload all task worker
//posix_kill($pid, SIGUSR2);
}
break;
default:
$this->stdout("Unknown command, usage: start|stop|reload\n", Console::FG_RED);
}
}
}