-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
363 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
return array( | ||
'run_conf' => storage_path('run') .DIRECTORY_SEPARATOR. 'workerman.conf', | ||
'log_file' => storage_path('logs'), // default: storage/logs/<appname>.log | ||
'pid_file' => storage_path('run'), // default: storage/run/hostname.pid | ||
'app_path' => app_path('Workerman'), // default: app/Workerman/ | ||
'apps' => array( | ||
// appname => options | ||
'timer' => array(), | ||
|
||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
namespace Skidu\LaraWorker\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
|
||
class BaseCommand extends Command | ||
{ | ||
protected $logFile; | ||
protected $pidFile; | ||
protected $runConfFile; | ||
|
||
protected function loadRuntimeConfig() | ||
{ | ||
if(!$this->runConfFile) { | ||
$this->initRunConfFile(); | ||
} | ||
$config = json_decode(file_get_contents($this->runConfFile)); | ||
if(json_last_error() !== JSON_ERROR_NONE) { | ||
throw new \Exception('parse runtime config failed: ' . $this->runConfFile); | ||
} | ||
|
||
$this->logFile = $config->log; | ||
$this->pidFile = $config->pid; | ||
return true; | ||
} | ||
|
||
protected function initLogFile($logFile=null) { | ||
if (!$logFile) { | ||
$logFile = config('workerman.log_file') .DIRECTORY_SEPARATOR. 'workerman.log'; | ||
} | ||
$this->createRunFile($logFile, 'log file'); | ||
$this->logFile = $logFile; | ||
return true; | ||
} | ||
|
||
protected function initPidFile($pidFile=null) | ||
{ | ||
if (!$pidFile) { | ||
$pidFile = config('workerman.pid_file') .DIRECTORY_SEPARATOR. 'workerman.pid'; | ||
} | ||
$this->createRunFile($pidFile, 'pid file'); | ||
$this->pidFile = $pidFile; | ||
return true; | ||
} | ||
|
||
protected function initRunConfFile() | ||
{ | ||
$runConfFile = config('workerman.run_conf'); | ||
$this->createRunFile($runConfFile, 'runtime config file'); | ||
$this->runConfFile = $runConfFile; | ||
return true; | ||
} | ||
|
||
private function createRunFile($file, $type) | ||
{ | ||
$directory = dirname($file); | ||
if (!is_dir($directory) && !mkdir($directory)) { | ||
throw new \Exception("can not create directory: {$directory}"); | ||
} | ||
if (!is_file($file) && ! touch($file)) { | ||
throw new \Exception("create {$type} failed: {$file}"); | ||
} | ||
if (!is_writable($file)) { | ||
throw new \Exception("{$type} can not been written: {$file}"); | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
namespace Skidu\LaraWorker\Commands; | ||
|
||
use Illuminate\Support\Facades\Config; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
class CreateCommand extends BaseCommand | ||
{ | ||
protected $signature = 'worker:create {name}'; | ||
protected $description = 'create a workerman apps'; | ||
|
||
public function fire() | ||
{ | ||
$name = $this->argument('name'); | ||
$path = config('workerman.app_path'); | ||
if (!is_dir($path) && !mkdir($path)) { | ||
return $this->error("\ncreate directory failed: {$path}\n"); | ||
} | ||
|
||
$appPath = $path .DIRECTORY_SEPARATOR. ucfirst($name); | ||
if (is_dir($appPath)) { | ||
return $this->error("\nFile exists: {$appPath}\n"); | ||
} | ||
|
||
if (!mkdir($appPath)) { | ||
return $this->error("\nfail to create directory at {$appPath}\n"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
namespace Skidu\LaraWorker\Commands; | ||
|
||
use Illuminate\Support\Facades\Config; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Workerman\Worker; | ||
|
||
class ReloadCommand extends BaseCommand | ||
{ | ||
protected $signature = 'worker:reload'; | ||
protected $description = 'reload all workerman apps'; | ||
|
||
public function fire() | ||
{ | ||
$this->loadRuntimeConfig(); | ||
|
||
global $argv; | ||
$argv = array('artisan', 'reload'); | ||
|
||
Worker::$pidFile = $this->pidFile; | ||
Worker::$logFile = $this->logFile; | ||
Worker::runAll(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
namespace Skidu\LaraWorker\Commands; | ||
|
||
use Illuminate\Support\Facades\Config; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Workerman\Worker; | ||
|
||
class RestartCommand extends BaseCommand | ||
{ | ||
protected $signature = 'worker:restart {--daemon}'; | ||
protected $description = 'restart all workerman apps'; | ||
|
||
public function fire() | ||
{ | ||
$this->loadRuntimeConfig(); | ||
global $argv; | ||
$argv = array( | ||
'artisan', | ||
'restart', | ||
$this->option('daemon') ? '-d' : '' | ||
); | ||
|
||
define('GLOBAL_START', 1); | ||
|
||
Worker::$logFile = $this->logFile; | ||
Worker::$pidFile = $this->pidFile; | ||
Worker::runAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
namespace Skidu\LaraWorker\Commands; | ||
|
||
use Illuminate\Support\Facades\Config; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Workerman\Worker; | ||
|
||
class StartCommand extends BaseCommand | ||
{ | ||
protected $signature = 'worker:start {--daemon} {--logfile=} {--pidfile=}'; | ||
protected $description = 'start all workerman apps'; | ||
|
||
public function fire() | ||
{ | ||
$this->checkException(); | ||
$this->initLogFile($this->option('logfile')); | ||
$this->initPidFile($this->option('pidfile')); | ||
$this->initRunConfFile(); | ||
$this->initWorkspace(); | ||
$this->startWorker(); | ||
} | ||
|
||
protected function checkException() | ||
{ | ||
if (!extension_loaded('pcntl')) { | ||
throw new \Exception("Please install pcntl extension"); | ||
} | ||
if (!extension_loaded('posix')) { | ||
throw new \Exception("Please install posix extension"); | ||
} | ||
return true; | ||
} | ||
|
||
protected function initWorkspace() | ||
{ | ||
$daemonOption = $this->option('daemon') ? '-d' : ''; | ||
global $argv; | ||
$argv = array( | ||
'artisan', | ||
'start', | ||
$daemonOption | ||
); | ||
|
||
define('GLOBAL_START', 1); | ||
Worker::$pidFile = $this->pidFile; | ||
Worker::$logFile = $this->logFile; | ||
return true; | ||
} | ||
|
||
protected function startWorker() | ||
{ | ||
$appPath = config('workerman.app_path'); | ||
$apps = config('workerman.apps'); | ||
foreach ($apps as $appName => $appConfig) { | ||
$file = $appPath .DIRECTORY_SEPARATOR. ucfirst($appName) .DIRECTORY_SEPARATOR. 'start.php'; | ||
if (!is_file($file)) { | ||
return $this->error("can not start app: {$appName}"); | ||
} | ||
require_once $file; | ||
} | ||
$config = array( | ||
'pid' => $this->pidFile, | ||
'log' => $this->logFile | ||
); | ||
file_put_contents($this->runConfFile, json_encode($config)); | ||
Worker::runAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
namespace Skidu\LaraWorker\Commands; | ||
|
||
use Illuminate\Support\Facades\Config; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Workerman\Worker; | ||
|
||
class StatusCommand extends BaseCommand | ||
{ | ||
protected $signature = 'worker:status'; | ||
protected $description = 'show all workerman app status'; | ||
|
||
public function fire() | ||
{ | ||
$this->loadRuntimeConfig(); | ||
|
||
global $argv; | ||
$argv = array( | ||
'artisan', 'status' | ||
); | ||
Worker::$logFile = $this->logFile; | ||
Worker::$pidFile = $this->pidFile; | ||
Worker::runAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
namespace Skidu\LaraWorker\Commands; | ||
|
||
use Workerman\Worker; | ||
|
||
class StopCommand extends BaseCommand | ||
{ | ||
protected $signature = 'worker:stop'; | ||
protected $description = 'stop all workerman apps'; | ||
|
||
public function fire() | ||
{ | ||
$this->loadRuntimeConfig(); | ||
|
||
global $argv; | ||
$argv = array( | ||
'artisan', | ||
'stop', | ||
); | ||
Worker::$logFile = $this->logFile; | ||
Worker::$pidFile = $this->pidFile; | ||
Worker::runAll(); | ||
|
||
unlink($this->runConfFile); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
namespace Skidu\LaraWorker; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class LaraWorkerProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
} | ||
|
||
/** | ||
* Register the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->registerCmdCreate(); | ||
$this->registerCmdStart(); | ||
$this->registerCmdRestart(); | ||
$this->registerCmdStop(); | ||
$this->registerCmdReload(); | ||
$this->registerCmdStatus(); | ||
} | ||
|
||
protected function registerCmdCreate() | ||
{ | ||
$this->app->singleton('command.worker.create', function($app){ | ||
return $app['\Skidu\LaraWorker\Commands\CreateCommand']; | ||
}); | ||
$this->commands('command.worker.create'); | ||
} | ||
|
||
protected function registerCmdStart() | ||
{ | ||
$this->app->singleton('command.worker.start', function($app){ | ||
return $app['\Skidu\LaraWorker\Commands\StartCommand']; | ||
}); | ||
$this->commands('command.worker.start'); | ||
} | ||
|
||
protected function registerCmdRestart() | ||
{ | ||
$this->app->singleton('command.worker.restart', function($app){ | ||
return $app['\Skidu\LaraWorker\Commands\RestartCommand']; | ||
}); | ||
$this->commands('command.worker.restart'); | ||
} | ||
|
||
protected function registerCmdStop() | ||
{ | ||
$this->app->singleton('command.worker.stop', function($app){ | ||
return $app['\Skidu\LaraWorker\Commands\StopCommand']; | ||
}); | ||
$this->commands('command.worker.stop'); | ||
} | ||
|
||
protected function registerCmdReload() | ||
{ | ||
$this->app->singleton('command.worker.reload', function($app){ | ||
return $app['\Skidu\LaraWorker\Commands\ReloadCommand']; | ||
}); | ||
$this->commands('command.worker.reload'); | ||
} | ||
|
||
protected function registerCmdStatus() | ||
{ | ||
$this->app->singleton('command.worker.status', function($app){ | ||
return $app['\Skidu\LaraWorker\Commands\StatusCommand']; | ||
}); | ||
$this->commands('command.worker.status'); | ||
} | ||
} |