tp6使用workerman發送定時任務,定時執行某個邏輯

在tp6中操作workerman發送定時任務是與linux中的crontab定時任務效果相同,運行php think worker就可以啓動workerman,然後用js的方式去啓動它,只需要啓動一次就可以了,以後不管頁面或瀏覽器關閉沒有,這個定時任務都會一直不停的運行着。

在後臺運行

在這裏插入圖片描述

在瀏覽器的console中運行:

ws = new WebSocket("ws://127.0.0.1:2347");
        ws.onopen = function() {
        alert("連接成功");
        };
        ws.onmessage = function(e) {
        alert("收到服務端的消息:" + e.data);
        };

在這裏插入圖片描述

上代碼:

<?php
declare(strict_types=1);

namespace app\socketio\controller;

use think\Request;
use Workerman\Lib\Timer;
use think\worker\Server;

class Settings extends Server
{
    protected $socket = 'websocket://0.0.0.0:2347';

    public function __construct()
    {
        parent::__construct();
        $this->onWorkerStart();

    }

    public function onWorkerStart()
    {

        $this->add_timer();
    }
    public function add_timer(){
        #設置每60秒執行一次定時任務
        /**
         *前端啓動方式:
        // 假設服務端ip爲127.0.0.1
        ws = new WebSocket("ws://127.0.0.1:2347");
        ws.onopen = function() {
        alert("連接成功");
        };
        ws.onmessage = function(e) {
        alert("收到服務端的消息:" + e.data);
        };
         */
        Timer::add(60, array($this, 'index'), array(), true);
    }
    /**
     * @param $connection
     * @param $data 142842567084ds
     */
    public function onMessage($connection,$data)
    {

        $message = [
            'application_ids.require' => '應用類型必須選定',
            'upgrade_start.require' => '升級開始時間不能爲空',
            'upgrade_start.dateFormat' => '時間格式不正確',
            'upgrade_end.dateFormat' => '時間格式不正確',
            'upgrade_end.gt' => '結束時間必須大於開始時間',
            'upgrade_end.require' => '升級結束時間不能爲空',
            'tips.require' => '升級備註不能爲空',
            'users_ids.require' => '請選擇升級用戶或類型',
            'close_type.require' => '必須選擇關閉某個端',
        ];

        $connection->send(json_encode($message));

    }




    /**
     * 顯示資源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        file_put_contents('a.log',date('Y-m-d H:i:s',time()).'執行了一次定時任務。'.PHP_EOL,FILE_APPEND);
//        $to = "[email protected]";         // 郵件接收者
//        $subject = "參數郵件";                // 郵件標題
//        $message = "Hello! 這是郵件的內容。";  // 郵件正文
//        $from = "[email protected]";   // 郵件發送者
//        $headers = "From:" . $from;         // 頭部信息設置
//        mail($to,$subject,$message,$headers);




        echo "每天83點執行任務";
        //每天0點執行任務
        if(time()/86400 === 0){
            echo date("Y-m-d H:i:s");
            echo "每天0點執行任務";
        }
        //每天8點執行任務
        if(time()/86400 === 39600){
            echo date("Y-m-d H:i:s");
            echo "每天8點執行任務";
        }
//        return view();
    }

    /**
     * 顯示創建資源表單頁.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的資源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 顯示指定的資源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 顯示編輯資源表單頁.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的資源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 刪除指定資源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}

最終效果:a.log

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章