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

在这里插入图片描述

发布了124 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42433970/article/details/103436173