• TP8 定时任务yzh52521/http-crontab 教程


    官方文档:

    https://www.thinkphp.cn/ext/36

    源码仓:

    http-crontab: 接口化秒级定时任务管理

    GitHub - yuanzhihai/http-crontab: 接口化秒级定时任务管理

    一、composer 安装

    composer require yzh52521/http-crontab

    二、启动yzh52521/http-crontab

    一定要用composer来打来命令窗口

    php think crontab start

    三、在TP控制器中发起请求

    发送GET、POST、PUT、DELETE等HTTP请求,在TP8的控制器中实现这些功能
    HTTP客户端库,如Guzzle。如果之前没有安装,可以通过Composer安装:

    要引入:

    use GuzzleHttp\Client;

    use think\facade\Config;

    发起ping请求:

    composer require guzzlehttp/guzzle
    1. <?php
    2. namespace app\admin\controller;
    3. use think\facade\Config;
    4. use GuzzleHttp\Client;
    5. /**
    6. * @name 定时任务
    7. * @author 峰神
    8. * @date 2024-6-1
    9. */
    10. class Crontab
    11. {
    12. public function index()
    13. {
    14. // 使用配置中的参数初始化Guzzle客户端
    15. $client = new Client([
    16. 'base_uri' => config('crontab.base_url'),
    17. 'timeout' => 2.0,
    18. 'headers' => [
    19. 'key' => config('crontab.safe_key'), // 添加安全密钥到请求头部
    20. ],
    21. ]);
    22. // 定义任务详情
    23. $taskData = [
    24. 'name' => 'My New Cron Task',
    25. 'schedule' => '* * * * *', // 每分钟执行一次
    26. 'command' => '/path/to/your/command', // 替换为实际的命令或控制器路由
    27. 'arguments' => [], // 可选参数
    28. 'times' => 0, // 无限次执行
    29. ];
    30. $method = 'GET';
    31. // 根据请求方法构造请求
    32. $uri = '/crontab/ping';
    33. $options = [];
    34. $taskData = [];
    35. if ('POST' === $method) {
    36. // 对于POST请求,我们需要在请求体中发送数据
    37. $options['json'] = $taskData;
    38. } elseif ('GET' === $method && !empty($taskData)) {
    39. // 对于GET请求,我们将数据附加到URL作为查询字符串
    40. $query = http_build_query($taskData);
    41. $uri .= '?' . $query;
    42. }
    43. try {
    44. // 发起请求
    45. $response = $client->request($method, $uri, $options);
    46. // 处理响应
    47. $responseBody = (string)$response->getBody();
    48. $arrayData = json_decode($responseBody, true);
    49. dump($arrayData);die;
    50. echo "Request response: " . $responseBody;
    51. } catch (\Exception $e) {
    52. echo "Error sending request: " . $e->getMessage();
    53. }
    54. }
    55. }

    打开/admin/crontab/index

    返回:Request response: {"code":200,"data":"pong","msg":"\u4fe1\u606f\u8c03\u7528\u6210\u529f\uff01"}

    成功

    添加任务例子:

    1. $client = new Client();
    2. // 发起POST请求
    3. $response = $client->post(config('crontab.base_url').''/crontab/add'', [
    4. 'headers' => [
    5. 'key' => Config::get('crontab.safe_key'),
    6. 'Content-Type' => 'application/x-www-form-urlencoded',
    7. ],
    8. 'form_params' => $postData,
    9. //'timeout' => 10.0, // 设置请求超时时间(秒)
    10. ]);
    11. // 处理响应
    12. $responseBody = (string)$response->getBody();
    13. $arrayData = json_decode($responseBody, true);

    重点:'form_params' => $postData,  $postData=数组

    日志

    1. //日志列表
    2. if(!empty($postData['id']) && $url=='/crontab/flow'){
    3. // 发起请求
    4. $response = $client->request('GET', config('crontab.base_url').$url,[
    5. 'query' => [
    6. 'page' => $page,
    7. 'limit' => $limit,
    8. 'filter' => json_encode(['crontab_id' => $postData['id']]), // 注意:这里假设服务器端能正确解析JSON格式的查询参数
    9. ],
    10. 'headers' => [
    11. 'key' => Config::get('crontab.safe_key'),
    12. ]
    13. ]);
    14. }else{//其它
    15. $response = $client->request('GET', config('crontab.base_url').$url,[
    16. 'headers' => [
    17. 'key' => Config::get('crontab.safe_key'),
    18. ]
    19. ]);
    20. }

    三、让定时任务关闭命令窗口也能运行,同时服务器重启后也能同时启动(宝塔)

    安装:

    添加成功后,是停止状态的,重新启用这个管理器就行

    参考案例:

    cshaptx4869/http-crontab - 码云 - 开源中国 (gitee.com)

    yuanzh/http-crontab - 码云 - 开源中国 (gitee.com)

    TP6代码安全

    http-crontab:Workerman + ThinkPHP6 实现后台可视化定时任务管理

    https://download.csdn.net/download/weixin_42157556/20696280

    如果本地安装测试请注意:

    1.环境:php7.3

    2.

    3.下载代码中缺少了vendor文件夹,需要重新生成,

    用composer+PHP打开命名窗口

    composer install

    ,启用服务:执行命令 php crontab.php (windows) 或 php crontab.php start (linux)

    浏览正常

  • 相关阅读:
    Elasticsearch基础篇(四):Elasticsearch7.x的官方文档学习(Set up Elasticsearch)
    大功率光伏应用不同多电平变换器拓扑的比较研究(Simulink)
    常见树种(贵州省):009楠木、樟木、桂木种类
    Flutter 3.3 正式发布啦
    XMLHttpRequest和Fetch API
    【黄色手套22】13番外:第一个C语言程序
    windows10中docker与vm不兼容
    解决ssh远程登录不上的问题
    使用amp训练出现Detected call of lr_scheduler.step() before optimizer.step().
    【图论】差分约束算法详解
  • 原文地址:https://blog.csdn.net/haibo0668/article/details/139373996