2020-09-03 00:15:57 +08:00
|
|
|
<?php
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-09-03 00:15:57 +08:00
|
|
|
|
|
|
|
|
namespace HttpServer;
|
|
|
|
|
|
|
|
|
|
|
2020-09-07 18:11:36 +08:00
|
|
|
use Exception;
|
2021-02-22 17:44:24 +08:00
|
|
|
use ReflectionException;
|
2020-09-07 18:11:36 +08:00
|
|
|
use Snowflake\Abstracts\Input;
|
2021-02-22 17:44:24 +08:00
|
|
|
use Snowflake\Event;
|
|
|
|
|
use Snowflake\Exception\ComponentException;
|
2020-09-07 15:19:13 +08:00
|
|
|
use Snowflake\Exception\ConfigException;
|
2021-02-22 17:44:24 +08:00
|
|
|
use Snowflake\Exception\NotFindPropertyException;
|
2021-03-09 20:04:49 +08:00
|
|
|
use Snowflake\Process\Process;
|
2020-09-03 00:15:57 +08:00
|
|
|
use Snowflake\Snowflake;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Command
|
|
|
|
|
* @package HttpServer
|
|
|
|
|
*/
|
|
|
|
|
class Command extends \Console\Command
|
|
|
|
|
{
|
|
|
|
|
|
2020-10-29 11:19:02 +08:00
|
|
|
public string $command = 'sw:server';
|
2020-09-03 00:15:57 +08:00
|
|
|
|
|
|
|
|
|
2020-10-29 11:19:02 +08:00
|
|
|
public string $description = 'server start|stop|reload|restart';
|
2020-09-03 00:15:57 +08:00
|
|
|
|
|
|
|
|
|
2020-09-11 14:27:09 +08:00
|
|
|
const ACTIONS = ['start', 'stop', 'restart'];
|
|
|
|
|
|
|
|
|
|
|
2020-09-03 00:15:57 +08:00
|
|
|
/**
|
2020-09-07 18:11:36 +08:00
|
|
|
* @param Input $dtl
|
2020-12-17 14:09:14 +08:00
|
|
|
* @return string
|
2020-09-11 14:27:09 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
* @throws ConfigException
|
2020-09-03 00:15:57 +08:00
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function onHandler(Input $dtl): string
|
2020-09-03 00:15:57 +08:00
|
|
|
{
|
2021-02-20 13:08:54 +08:00
|
|
|
$manager = Snowflake::app()->getServer();
|
2020-09-07 18:11:36 +08:00
|
|
|
$manager->setDaemon($dtl->get('daemon', 0));
|
2020-09-11 14:27:09 +08:00
|
|
|
|
|
|
|
|
if (!in_array($dtl->get('action'), self::ACTIONS)) {
|
2020-09-11 14:42:52 +08:00
|
|
|
return 'I don\'t know what I want to do.';
|
2020-09-11 14:27:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($manager->isRunner() && $dtl->get('action') == 'start') {
|
2020-09-11 14:42:52 +08:00
|
|
|
return 'Service is running. Please use restart.';
|
2020-09-07 15:19:13 +08:00
|
|
|
}
|
2020-09-11 14:27:09 +08:00
|
|
|
|
2021-03-09 20:04:49 +08:00
|
|
|
Process::kill(Snowflake::getMasterPid(), SIGTERM);
|
2020-09-11 14:22:44 +08:00
|
|
|
if ($dtl->get('action') == 'stop') {
|
2020-09-11 14:42:52 +08:00
|
|
|
return 'shutdown success.';
|
2020-09-11 14:22:44 +08:00
|
|
|
}
|
2021-02-22 17:44:24 +08:00
|
|
|
|
2020-12-17 14:09:14 +08:00
|
|
|
return $manager->start();
|
2020-09-03 00:15:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|