日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php swoole实现定时任务,Swoole实现任务定时自动化调度详解,来学习下

發(fā)布時(shí)間:2025/3/21 php 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php swoole实现定时任务,Swoole实现任务定时自动化调度详解,来学习下 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問題描述

這幾天做銀行對(duì)帳接口時(shí),踩了一個(gè)坑,具體需求大致描述一下。

銀行每天凌晨后,會(huì)開始準(zhǔn)備昨天的交易流水?dāng)?shù)據(jù),需要我們這邊請(qǐng)求拿到。

因?yàn)樗麄兘o的是一個(gè)base64加密的zip壓縮流,解開以后可以得到txt文件,里面就是我們需要的數(shù)據(jù)了。

業(yè)務(wù)程序?qū)懞靡院?#xff0c;隨手丟了一個(gè)定時(shí)任務(wù)就去睡覺了。

哪知道第二天上班的時(shí)候,檢查。發(fā)現(xiàn)并沒有拿到數(shù)據(jù),查詢一下日志的時(shí)候發(fā)現(xiàn),凌晨服務(wù)端請(qǐng)求的時(shí)候,銀行接口返回了:系統(tǒng)錯(cuò)誤信息。

咨詢銀行那邊后,銀行那邊相關(guān)人員建議我們多請(qǐng)求幾次,但是在多次請(qǐng)求中,我發(fā)現(xiàn)銀行那邊是有頻率限制的,最后得知,此接口只能半個(gè)小時(shí)才能請(qǐng)求一次。這就比較尷尬了,因?yàn)槲也恢楞y行那邊什么時(shí)候能返回?cái)?shù)據(jù)給我。

于是這個(gè)問題怎么解決呢?理想的情況是,服務(wù)端請(qǐng)求數(shù)據(jù),銀行那邊沒有返回。然后程序等半個(gè)小時(shí)后,再請(qǐng)求一次,這樣一直到銀行那邊返回正確的數(shù)據(jù)中止。

問題分析

這個(gè)功能換作別的語言也許不難,但是通過php實(shí)現(xiàn)的話,那就比較麻煩了。通常的話,我們可以搭配linux下的cron來實(shí)現(xiàn),比如我們可以在凌晨到6:00之間做一個(gè)定時(shí)任務(wù),每半個(gè)小時(shí)掃描一次php腳本,如果發(fā)現(xiàn)銀行那邊的狀態(tài)依舊為失敗的話,我們就執(zhí)行一次php腳本去請(qǐng)求數(shù)據(jù)。直到請(qǐng)求到正確的數(shù)據(jù),然后把狀態(tài)更新為成功。

這不失為一種方法,但太傻了。比如說銀行那邊比較正常,凌晨,也就是第一次請(qǐng)求的時(shí)候,就已經(jīng)返回了正確的數(shù)據(jù),那么我們的cron腳本還傻傻的每個(gè)半個(gè)小時(shí)執(zhí)行一次,好蠢!~

或者我們可以嘗試使用linux下的at命令,但感覺還是不夠優(yōu)雅。

解決問題

于是決定給laravel擴(kuò)展一個(gè)swoole插件來解決此問題,swoole的定時(shí)任務(wù)很完美的解決了我們目前的問題。

首先我們需要把swoole擴(kuò)展安裝好,具體過程略。

裝好以后,我們寫一個(gè)swoole簡(jiǎn)易的服務(wù)端測(cè)試腳本,注意,此腳本是放在app/Console/Commands/下的,筆者是放在了app/Console/Commands/Swoole/swoole.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 () {

//業(yè)務(wù)邏輯處

exec('php /path/to/root/artisan Test:Command');

});

}

return date('Y-m-d H:i:s') . "第一次執(zhí)行";

}

public function onFinish($serv, $task_id, $data)

{

echo "Task finish\n";

}

private function stop()

{

exec('/usr/bin/killall php');

}

}

這是服務(wù)端,我們主要用到了after方法,模擬的話,是三秒一執(zhí)行。實(shí)際應(yīng)該是三十分鐘

然后我們隨便寫一個(gè)客戶端連接類

/**

* 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);

}

}

于是我們?cè)阢y行腳本中就可以去執(zhí)行了

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()

{

//這里是業(yè)務(wù)邏輯

//如果銀行那邊返回的為false的話,那么我們把他交給swoole的定時(shí)腳本

$status = false;

if(!$status)

{

$swoole = new swoole("hehe");

$swoole->connect();

}

}

}

以上內(nèi)容希望幫助到大家,很多PHPer在進(jìn)階的時(shí)候總會(huì)遇到一些問題和瓶頸,業(yè)務(wù)代碼寫多了沒有方向感,不知道該從那里入手去提升,對(duì)此我整理了一些資料,包括但不限于:分布式架構(gòu)、高可擴(kuò)展、高性能、高并發(fā)、服務(wù)器性能調(diào)優(yōu)、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優(yōu)化、shell腳本、Docker、微服務(wù)、Nginx等多個(gè)知識(shí)點(diǎn)高級(jí)進(jìn)階干貨需要的可以免費(fèi)分享給大家

總結(jié)

以上是生活随笔為你收集整理的php swoole实现定时任务,Swoole实现任务定时自动化调度详解,来学习下的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。