詳解swoole實現任務定時自動化調度


問題描述

這幾天做銀行對帳接口時,踩了一個坑,具體需求大致描述一下。

銀行每天凌晨后,會開始准備昨天的交易流水數據,需要我們這邊請求拿到。

因為他們給的是一個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(); } } } 

以上內容希望幫助到大家,很多PHPer在進階的時候總會遇到一些問題和瓶頸,業務代碼寫多了沒有方向感,不知道該從那里入手去提升,對此我整理了一些資料,包括但不限於:分布式架構、高可擴展、高性能、高並發、服務器性能調優、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點高級進階干貨需要的可以免費分享給大家,需要的可以加入我的PHP官方群免費獲取,點擊此處

或者戳下面鏈接

PHP進階學習思維導圖、面試;文檔、視頻資源免費獲取​kdocs.cn

部分資料截圖如下


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM