~~~
<?php
/**
* Created by PhpStorm.
* User: Mikkle
* QQ:776329498
* Date: 2017/06/19
* Time: 10:14
*/
namespace app\worker\controller;
use app\base\controller\Redis;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Exception;
use think\Log;
/**
* Created by PhpStorm.
* Power by Mikkle
* QQ:776329498
* Date: 2017/6/12
* Time: 15:07
*/
abstract class WorkerCommandBase extends Command
{
protected $sleep = 5;
protected $redis;
protected $listName;
protected $pcntl;
public function __construct($name = null)
{
$this->redis = $this->redis();
$this->listName = "worker_list";
$this->pcntl = true;
parent::__construct($name);
}
protected function configure()
{
$this->setName('mikkle')->setDescription('Here is the mikkle\'s command ');
}
protected function execute(Input $input,Output $output)
{
while (true) {
//標記后端服務運行中
$this->signWorking();
echo "==================================================" . PHP_EOL;
$this->autoClass();
echo "==================================================" . PHP_EOL;
$this->sleep();
}
}
/**
* 自動執行
* Power: Mikkle
* Email:776329498@qq.com
* @return bool
*/
protected function autoClass()
{
$works = $this->getWorks();
if ($works) {
foreach ($works as $work => $item) {
if ($this->pcntl) {
$this->pcntlWorker($work, $item);
} else {
$this->runWorker($work, $item);
}
}
} else {
return false;
}
}
public function getWorks()
{
try {
return $this->redis->hget($this->listName);
} catch (Exception $e) {
return false;
}
}
/**
* 檢測執行方法是否存在
* Power: Mikkle
* Email:776329498@qq.com
* @param $work
* @param $item
* @return bool
*/
protected function checkWorkerExists($work, $item)
{
if (class_exists($work)) {
if (method_exists($work, $item)) {
return true;
} else {
return false;
}
}
}
/**
* 運行任務
* Power: Mikkle
* Email:776329498@qq.com
* @param $work
* @param $item
*/
protected function runWorker($work, $item)
{
try {
if ($this->checkWorkerExists($work, $item)) {
echo "執行[{$work}]任務" . PHP_EOL;
$work::$item();
Log::notice("執行[{$work}::{$item}]任務");
} elseif ($this->checkWorkerExists($work, "run")) {
echo "執行[{$work}]任務" . PHP_EOL;
$work::run();
Log::notice("執行[{$work}::run]任務");
} else {
echo "執行[{$work}::{$item}]任務的默認和指定方法都不存在" . PHP_EOL;
$this->redis->hdel($this->listName, $item);
}
} catch (Exception $e) {
echo "執行[{$work}]任務失敗" . PHP_EOL;
Log::notice($e->getMessage());
if ($this->pcntl) {
$this->pcntlKill();
}
}
}
/**
* 分進程
* Power: Mikkle
* Email:776329498@qq.com
* @param $work
* @param $item
*/
protected function pcntlWorker($work, $item)
{
try {
// 通過pcntl得到一個子進程的PID
$pid = pcntl_fork();
if ($pid == -1) {
// 錯誤處理:創建子進程失敗時返回-1.
die ('could not fork');
} else if ($pid) {
// 父進程邏輯
// 等待子進程中斷,防止子進程成為僵尸進程。
// WNOHANG為非阻塞進程,具體請查閱pcntl_wait PHP官方文檔
pcntl_wait($status, WNOHANG);
} else {
// 子進程邏輯
$pid_2 = pcntl_fork();
if ($pid_2 == -1) {
// 錯誤處理:創建子進程失敗時返回-1.
die ('could not fork');
} else if ($pid_2) {
// 父進程邏輯
echo "父進程邏輯開始" . PHP_EOL;
// 等待子進程中斷,防止子進程成為僵尸進程。
// WNOHANG為非阻塞進程,具體請查閱pcntl_wait PHP官方文檔
pcntl_wait($status, WNOHANG);
echo "父進程邏輯結束" . PHP_EOL;
} else {
// 子進程邏輯
echo "子進程邏輯開始" . PHP_EOL;
$this->runWorker($work, $item);
echo "子進程邏輯結束" . PHP_EOL;
$this->pcntlKill();
}
$this->pcntlKill();
}
} catch (Exception $e) {
Log::error($e->getMessage());
}
}
/**
* Kill子進程
* Power: Mikkle
* Email:776329498@qq.com
*/
protected function pcntlKill()
{
// 為避免僵尸進程,當子進程結束后,手動殺死進程
if (function_exists("posix_kill")) {
posix_kill(getmypid(), SIGTERM);
}
system('kill -9 ' . getmypid());
exit ();
}
public function signWorking()
{
$this->redis()->set("command", "true");
$this->redis()->setExpire("command", 30);
}
public function sleep($second = "")
{
$second = $second ? $second : $this->sleep;
// sleep(sleep($second)); //TP5的命令行 sleep($second) 不生效
sleep($second);
echo "睡眠{$second}秒成功!當前時間:" . date('h:i:s') . PHP_EOL;
}
/**
* @return int
*/
public function getSleep()
{
return $this->sleep;
}
/**
* @param int $sleep
* @return void
*/
public function setSleep($sleep)
{
$this->sleep = $sleep;
}
/**
* @title redis
* @description redis加載自定義Redis類
* User: Mikkle
* QQ:776329498
*/
protected static function redis()
{
$options = empty($options) ? $redis = Config::get("redis") : $options;
return Redis::instance($options);
}
}
~~~
- PHP7新特性
- 優雅的寫代碼
- 常見的代碼優化
- 常用的工具類
- PHP原生生成EXCEL
- PHP地理位置計算
- PHP獲取服務器狀態
- 駝峰轉下劃線
- 百度地圖兩點坐標距離計算
- 判斷是否是url
- PHP常見header頭
- 郵件發送類
- 阿拉伯數字轉化為大寫
- 獲取漢字首個拼音
- 根據身份證號獲取星座
- 生成驗證碼類
- 生成唯一ID
- 身份證驗證類
- PHP中文轉拼音
- Nginx配置文件
- curl獲取網頁內容
- 快遞查詢api
- 上傳圖片類
- 股票類
- 找回密碼類
- 字符串助手函數
- 校驗數據規則
- PHP獲取收集相關信息
- 字符串截取助手函數
- 網頁中提取關鍵字
- 檢測瀏覽器語言
- 微信相關類
- 微信獲取access_token
- 獲取用戶基本信息
- 代碼規范
- 編程規范(psr-1,2)
- 編程規范(原作者的建議)
- 經驗
- 常用函數地址
- 函數集合
- 一些常識
- MYSQL相關知識
- 常用sql
- mysql事務隔離級別
- Read uncommitted
- Read committed
- Repeatable read
- Serializable
- 高性能MYSQL讀書筆記
- 第一章MYSQL的架構
- mysql邏輯架構
- redis相關知識
- 1.安裝redis
- 3.php操作redis
- 隊列
- 悲觀鎖
- 樂觀鎖
- 發布
- 訂閱
- redis實戰-文章投票
- 設計模式
- 創建模型實例
- 單例模式
- 工廠模式
- AnimalInterface.php
- Chicken.php
- Factory.php
- Farm.php
- Pig
- SampleFactory.php
- Zoo
- 抽象工廠模式
- AnimalFactory
- Factory
- FarmInterface
- Income
- PandaZoo
- PeonyZoo
- PigFarm
- PlantFactory
- RiceFarm
- ZooInterface
- 原型模式
- 建造者模式
- 結構型模式實例
- 橋接模式
- 享元模式
- 外觀模式
- 適配器模式
- 裝飾器模式
- 組合模式
- 代理模式哦
- 過濾器模式
- 行為型模式實例
- 模板模式
- 策略模式
- 狀態模式
- 觀察者模式
- 責任鏈模式
- 訪問者模式
- 解釋器模式
- 空對象模式
- 中介者模式
- 迭代器模式
- 命令模式
- 備忘錄模式
- 網絡知識
- 互聯網協議概述
- nginx簡易交互過程
- HTTP知識
- LINUX相關知識
- swoole學習
- 1.初識swoole
- 2.WebSocket PHP 即時通訊開發
- 3.異步多進程的 CURL
- 4.異步非阻塞多進程的 Http 服務器
- 5.TCP 服務器
- 5.1同步 TCP 客戶端
- 5.2異步 TCP 客戶端
- 6.UDP 服務器
- 7.異步多任務處理
- 8.毫秒定時器
- 9.高并發投票
- ThinkPHP5學習
- 命令行操作
- 所有命令行中用到的基類
- 1.base
- 2.WorkerBase
- 3.TimeWorkerBase
- 4.CycleWorkerBase
- 5.WorkerCommandBase
- 6.WorkerHookBase
- 1.基礎命令實現
- 2.建立Linux上的守護源碼
- 3.發送模板消息
- 4.基于命令行實現自己的隊列模式
- 5.發送定時短信
- thinkphp5使用sentry
- sentry通知,記錄日志
- 高級查詢
- Kafka相關
- 1.安裝
- 2.為php打擴展
- 3.kafka實現
- 一些工具搭建
- sentry日志收集系統搭建
- walle搭建
- php實現定時任務
- 檢測文件變化