這次給大家整理了以實現任務定時自動化調度為主題的一波swoole教程,希望能對大家有用!
開發環境
環境:lnmp下進行試驗。
框架:laravel5
問題描述
這幾天做銀行對帳接口時,踩了一個坑,具體需求大致描述一下。
銀行每天凌晨后,會開始準備昨天的交易流水數據,需要我們這邊請求拿到。
因為他們給的是一個base64加密的zip壓縮流,解開以后可以得到txt文件,里面就是我們需要的數據了。
業務程序寫好以后,隨手丟了一個定時任務就去睡覺了。
哪知道第二天上班的時候,檢查。發現并沒有拿到數據,查詢一下日志的時候發現,凌晨服務端請求的時候,銀行接口返回了:系統錯誤信息。
咨詢銀行那邊后,銀行那邊相關人員建議我們多請求幾次,但是在多次請求中,我發現銀行那邊是有頻率限制的,最后得知,此接口只能半個小時才能請求一次。這就比較尷尬了,因為我不知道銀行那邊什么時候能返回數據給我。
于是這個問題怎么解決呢?理想的情況是,服務端請求數據,銀行那邊沒有返回。然后程序等半個小時后,再請求一次,這樣一直到銀行那邊返回正確的數據中止。
問題分析
這個功能換作別的語言也許不難,但是通過php實現的話,那就比較麻煩了。通常的話,我們可以搭配linux下的cron來實現,比如我們可以在凌晨到6:00之間做一個定時任務,每半個小時掃描一次php腳本,如果發現銀行那邊的狀態依舊為失敗的話,我們就執行一次php腳本去請求數據。直到請求到正確的數據,然后把狀態更新為成功。
這不失為一種方法,但太傻了。比如說銀行那邊比較正常,凌晨,也就是第一次請求的時候,就已經返回了正確的數據,那么我們的cron腳本還傻傻的每個半個小時執行一次,好蠢!~
或者我們可以嘗試使用linux下的at命令,但感覺還是不夠優雅。
解決問題
于是決定給laravel擴展一個swoole插件來解決此問題,swoole的定時任務很完美的解決了我們目前的問題。
首先我們需要把swoole擴展安裝好,具體過程略。
裝好以后,我們寫一個swoole簡易的服務端測試腳本,注意,此腳本是放在app/Console/Commands/下的,筆者是放在了app/Console/Commands/Swoole/swoole.php下,具體代碼為
<?php namespace App\Console\Commands\Swoole; use Illuminate\Console\Command; class swoole extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'swoole {action}'; /** * The console command description. * * @var string */ protected $description = "Let's use swoole !"; private $serv; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $arg = $this->argument('action'); switch ($arg) { case 'start': $this->info('swoole server started'); $this->start(); break; case 'stop': $this->info('stoped'); $this->stop(); break; case 'restart': $this->info('restarted'); break; } } private function start() { $this->serv = new \swoole_server("127.0.0.1", 9501); $this->serv->set(array( 'worker_num' => 8, 'daemonize' => false, 'max_request' => 10000, 'dispatch_mode' => 2, 'task_worker_num' => 8, 'task_ipc_mode' => 3, 'log_file' => storage_path('logs/taskqueue.log'), )); $this->serv->on('Receive', array($this, 'onReceive')); $this->serv->on('Task', array($this, 'onTask')); $this->serv->on('Finish', array($this, 'onFinish')); $this->serv->start(); } public function onReceive(\swoole_server $serv, $fd, $from_id, $data) { $serv->task($data); } public function onTask($serv, $task_id, $from_id, $data) { $timeon = (3) * 1000; if ($timeon > 0) { $serv->after($timeon, function () { //業務邏輯處 exec('php /path/to/root/artisan Test:Command'); }); } return date('Y-m-d H:i:s') . "第一次執行"; } public function onFinish($serv, $task_id, $data) { echo "Task finish\n"; } private function stop() { exec('/usr/bin/killall php'); } }
這是服務端,我們主要用到了after方法,模擬的話,是三秒一執行。實際應該是三十分鐘
然后我們隨便寫一個客戶端連接類
<?php /** * Created by PhpStorm. * User: nosay * Date: 4/13/18 * Time: 9:27 PM */ namespace App\Extension\php\Swoole; class swoole{ private $data; private $client; public function __construct($data){ $this->data = $data; $this->client = new \swoole_client(SWOOLE_SOCK_TCP); } public function connect(){ if( !$this->client->connect("127.0.0.1", 9501 , 1) ) { echo "Error"; } $this->client->send($this->data); } }于是我們在銀行腳本中就可以去執行了
<?php namespace App\Console\Commands\Test; use App\Extension\php\Swoole\swoole; use Illuminate\Console\Command; class TestCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'Test:Command'; /** * The console command description. * * @var string */ protected $description = 'Command Test'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { //這里是業務邏輯 //如果銀行那邊返回的為false的話,那么我們把他交給swoole的定時腳本 $status = false; if(!$status) { $swoole = new swoole("hehe"); $swoole->connect(); } } }以上是文章全部內容,有學習與經驗交流的歡迎關注編程學習網,一起學習,交流與進步!
掃碼二維碼 獲取免費視頻學習資料
- 本文固定鏈接: http://www.wangchenghua.com/post/7888/
- 轉載請注明:轉載必須在正文中標注并保留原文鏈接
- 掃碼: 掃上方二維碼獲取免費視頻資料