fucongcong/framework

View on GitHub
core/Group/Cron/CronAdmin.php

Summary

Maintainability
A
2 hrs
Test Coverage
<?php
 
namespace Group\Cron;
 
use swoole_http_server;
use swoole_process;
 
The class CronAdmin has 11 public methods. Consider refactoring CronAdmin to keep number of public methods under 10.
class CronAdmin
Opening class brace must be on a line by itself
Whitespace found at end of line
{
protected $http;
 
protected $jobs;
 
protected $pidPath = __FILEROOT__.'runtime/cron-server/pid';
 
Method `__construct` has 57 lines of code (exceeds 25 allowed). Consider refactoring.
The method __construct() has an NPath complexity of 2048. The configured NPath complexity threshold is 200.
The method __construct() has a Cyclomatic Complexity of 13. The configured cyclomatic complexity threshold is 10.
The variable $work_id is not named in camelCase.
public function __construct()
Whitespace found at end of line
{
$this->mkDir(__FILEROOT__.'runtime/cron-server');
$http = new swoole_http_server('0.0.0.0', '10008');
$http->set(array(
'daemonize' => true,
'log_file' => __FILEROOT__.'runtime/cron-server/app.log',
'reactor_num' => 1,
'worker_num' => 2, //worker process num
'backlog' => 128, //listen backlog
'max_request' => 500,
'heartbeat_idle_time' => 30,
'heartbeat_check_interval' => 10,
Whitespace found at end of line
'dispatch_mode' => 3,
));
 
$http->on('Start', [$this, 'onStart']);
$http->on('WorkerStart', [$this, 'onWorkerStart']);
$http->on('shutdown', [$this, 'onShutdown']);
 
Expected 1 space before opening brace; found 0
$http->on('request', function ($request, $response){
$request->get = isset($request->get) ? $request->get : [];
$request->post = isset($request->post) ? $request->post : [];
$request->cookie = isset($request->cookie) ? $request->cookie : [];
$request->files = isset($request->files) ? $request->files : [];
$request->server = isset($request->server) ? $request->server : [];
if ($request->server['request_uri'] == '/favicon.ico') {
$response->end();
return;
}
if ($request->post) {
$post = $request->post;
$action = $post['action'];
$this->$action($post);
$response->status(200);
$response->end(json_encode(['status' => 'success', 'code' => 200]));
return;
}
 
$cacheDir = \Config::get('cron::log_dir') ? : 'runtime';
$pid = $this->get('pid', $cacheDir) ? : 0;
 
$works = [];
foreach ($this->jobs as $job) {
$cronAdmin = $this->get('cronAdmin', $cacheDir."/".$job['name']);
$work_id = $this->get('work_id', $cacheDir."/".$job['name']);
if (is_array($cronAdmin) && is_array($work_id)) {
$work = $cronAdmin[0];
$work['pid'] = $work_id[0];
$works[] = $work;
The method __construct uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
} else {
$works[] = ['job' => $job, 'pid' => $work_id[0]];
}
}
 
$output = $this->twigInit()->render('console.html.twig', [
'pid' => $pid,
'works' => $works,
]);
$response->status(200);
$response->end($output);
return;
});
$this->http = $http;
}
 
public function start()
{
$this->http->start();
}
 
/**
* 服务启动回调事件
* @param 【swoole_http_server】 $serv
*/
public function onStart($serv)
{
if (PHP_OS !== 'Darwin') {
swoole_set_process_name("php http server: master");
}
 
echo "HTTP Server Start...".PHP_EOL;
 
$pid = $serv->master_pid;
$this->mkDir($this->pidPath);
file_put_contents($this->pidPath, $pid);
}
 
Avoid unused parameters such as '$post'.
public function startMaster($post)
{
$path = __ROOT__;
exec("cd {$path} && app/cron start > /dev/null &");
\Log::info("cd {$path} && app/cron start > /dev/null &", ['startMaster']);
}
 
public function restartMaster($post)
{
$path = __ROOT__;
exec("cd {$path} && app/cron restart > /dev/null &");
\Log::info("cd {$path} && app/cron restart > /dev/null &", ['restartMaster']);
$this->reloadServer($post);
}
 
Avoid unused parameters such as '$post'.
public function stopMaster($post)
{
$path = __ROOT__;
exec("cd {$path} && app/cron stop > /dev/null &");
\Log::info("cd {$path} && app/cron stop > /dev/null &", ['stopMaster']);
}
 
public function execWorker($post)
{
$path = __ROOT__;
$jobName = $post['jobName'];
exec("cd {$path} && app/cron exec {$jobName} > /dev/null &");
\Log::info("cd {$path} && app/cron exec {$jobName} > /dev/null &", ['execWorker']);
}
 
public function rejobWorker($post)
{
$path = __ROOT__;
$jobName = $post['jobName'];
exec("cd {$path} && app/cron rejob {$jobName} > /dev/null &");
\Log::info("cd {$path} && app/cron rejob {$jobName} > /dev/null &", ['rejobWorker']);
}
 
Avoid unused parameters such as '$post'.
public function reloadServer($post)
{
$pid = file_get_contents($this->pidPath);
echo "当前进程".$pid.PHP_EOL;
echo "热重启中".PHP_EOL;
if ($pid) {
if (swoole_process::kill($pid, 0)) {
swoole_process::kill($pid, SIGUSR1);
}
}
echo "重启完成".PHP_EOL;
swoole_process::daemon(true);
}
 
/**
* 服务关闭回调事件
* @param 【swoole_http_server】 $serv
*/
Remove error control operator '@' on line 166.
Avoid unused parameters such as '$serv'.
public function onShutdown($serv)
Whitespace found at end of line
{
@unlink($this->pidPath);
echo "HTTP Server Shutdown...".PHP_EOL;
}
 
/**
* worker启动回调事件
* @param 【swoole_http_server】 $serv
* @param 【int】 $workerId
*/
Avoid unused parameters such as '$workerId'.
Avoid unused parameters such as '$serv'.
public function onWorkerStart($serv, $workerId)
Whitespace found at end of line
{
if (function_exists('opcache_reset')) {
opcache_reset();
}
 
//设置不同进程名字,方便grep管理
if (PHP_OS !== 'Darwin') {
swoole_set_process_name("php http server: worker");
}
 
\Config::clear();
$this->jobs = \Config::get('cron::job');
 
echo "HTTP Worker Start...".PHP_EOL;
}
 
private function get($cacheName, $cacheDir)
{
$dir = __FILEROOT__.$cacheDir."/".$cacheName;
 
if (file_exists($dir)) {
$data = file_get_contents($dir);
if ($data) {
return json_decode($data, true);
}
}
return null;
}
 
private function twigInit()
{
Missing class import via use statement (line '207', column '23').
$loader = new \Twig_Loader_Filesystem(dirname(__FILE__)."/View");
Avoid using undefined variables such as '$env' which will lead to PHP notices.
Missing class import via use statement (line '208', column '21').
$twig = new \Twig_Environment($loader, isset($env) ? $env : array());
 
return $twig;
}
 
/**
* 新建目录
* @param [string] $dir
*/
private function mkDir($dir)
{
$parts = explode('/', $dir);
Avoid unused local variables such as '$file'.
$file = array_pop($parts);
$dir = '';
foreach ($parts as $part) {
if (!is_dir($dir .= "$part/")) {
mkdir($dir);
}
}
}
}