知识分类
后端
前端
设计
开源组件/产品
APP
其他
广告招募

TP6下使用workerman定时备份数据库

管理员
发布于:2023-04-19 16:55:57

1.使用composer安装 think-worker

composer require topthink/think-worker

2.在app\command下创建定时任务文件Cron.php(文件名自己取)

<?php

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use Workerman\Lib\Timer;
use Workerman\Worker;

/**
 * 计划任务
 */

class Cron extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('Cron')
            ->setDescription('计划任务');
    }

    protected function execute(Input $input, Output $output)
    {
        $worker = new Worker();
        $worker->name = $this->getName();
        $worker->onWorkerStart = function () use ($output) {
            //每隔24小时备份数据库 
            $database = new Database();
            Timer::add((3600 * 24), [$database, 'backup'], [$output]);
        };
        Worker::runAll();
    }
}

这里引用了一个 备份MYSQL的类库 点击这里

3.在配置文件config/console.php 中添加指令

'Cron' => 'app\command\cron\Cron',

<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
    // 指令定义
    'commands' => [
        'Cron' => 'app\command\Cron',
    ],
];

4.控制台到TP目下执行以下命令

php think Cron
上一篇:分享一个PHP函数:将字节bytes转换成合适的大小单位
下一篇:没有了
搜索内容
最新文章
  • 连接WIFI出现无Internet
  • 免费AI SEO工具,SEO的AI生
  • OBS在银河麒麟或ubuntu等核心
  • 常见主板开机进入bios热键,一张图
  • 微软常用运行库VC++合集下载,解决
  • MSYS2中安装使用GCC的正确方法
  • 用了这些网站,多出来的时间让你摸鱼
  • 优麒麟操作系统(麒麟系统社区版)安装
  • 推荐一个Vscode插件,让你写正则
  • MinDoc文档管理系统linux环
精彩文章
  • 1 php将数字转成EXCEL列名,支持
  • 2 什么是PHP站群以及使用php开源框
  • 3 连接WIFI出现无Internet
  • 4 免费AI SEO工具,SEO的AI生
  • 5 FrameCoder非可,基于web
  • 6 PBOOTCMS安全加固方法解析与实
  • 7 常见主板开机进入bios热键,一张图
  • 8 MSYS2中安装使用GCC的正确方法
  • 9 Layui 怎么样更换主题颜色?
  • 10 小程序真机调试 报错 define
工信部备案:苏ICP备2023011827号-1
苏公网安备 32111202000338号