美文网首页
hf3.0 转载自如出 定时任务用法

hf3.0 转载自如出 定时任务用法

作者: geeooooz | 来源:发表于2023-07-23 13:49 被阅读0次

转载自:https://www.ziruchu.com/art/652

Hyperf 3 快速使用 - Hyperf 3 通过配置和注解两种方式使用定时任务

Hypref 通过 hyperf/crontab 组件提供了一个 秒级 定时任务的功能。执行定时任务会通过 Linux 中的 crontab 命令实现。

配置定时任务
第一步:安装组件包

composer require hyperf/crontab
第二步:发布配置文件

php bin/hyperf.php vendor:publish hyperf/crontab
该命令会在 config/autoload 目录下生成 crontab.php 配置文件,生成内容如下:

<?php

declare(strict_types=1);

return [
   // 开启定时任务
   'enable' => true,
   'crontab' => [
   ],
];
第三步:启用任务调度器进程 

在使用定时任务组件之前,需要先在 config/autoload/processes.php 内注册一下 Hyperf\Crontab\Process\CrontabDispatcherProcess 自定义进程,如下:

<?php
// config/autoload/processes.php

declare(strict_types=1);

return [
   // 分发定时任务
   \Hyperf\Crontab\Process\CrontabDispatcherProcess::class,
];
到这里定时任务基础配置已经完成,接下来只要编写定时任务脚本即可。

高能提示:
1、服务启动,定时任务也就启动了,但是定时任务不会立即执行;
2、服务启动后,定时任务不会立即执行,而是等到下一分才开始执行。 举例:比如定时启动时间是 23:05:05 ,那么定时任务会在 23:06:00 才正式执行。

两种方式实现定时任务

方式一:使用配置文件方式实现定时任务

第一步:定义定时任务类

<?php

namespace App\Task;

// 使用配置文件的方式执行定时任务
use Hyperf\Logger\LoggerFactory;
use Psr\Log\LoggerInterface;

class TestTask
{
   protected LoggerInterface $logger;

   public function __construct(LoggerFactory $loggerFactory)
   {
       $this->logger = $loggerFactory->get('log', 'default');
   }
   
   public function demoTask()
   {
       // 执行定时任务
       $this->logger->info(date('Y-m-d H:id') . ' 我是王美丽呀');
   }
}
第二步:配置定时任务

<?php
// config/autoload/crontab.php
declare(strict_types=1);

return [
   'enable' => true,
   'crontab' => [
       (new \Hyperf\Crontab\Crontab())->setName('TestTask')->setRule('* * * * *')->setCallback([\App\Task\TestTask::class, 'demoTask'])->setMemo('我是备注:测试定时任务'),
   ],
];
第三步:启动服务

php bin/hyperf start

方式二:使用注解方式实现定时任务

第一步:定义定时任务类

<?php

namespace App\Task;

use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Logger\LoggerFactory;
use Psr\Log\LoggerInterface;

#[Crontab(name: "DemoTask", rule: "*\/1 * * * * *", callback: "demoTask", memo: "注解定时任务说明'")]
class DemoTask
{
   protected LoggerInterface $logger;

   public function __construct(protected LoggerFactory $loggerFactory)
   {
       $this->logger = $this->loggerFactory->get('log', 'default');
   }

   public function demoTask()
   {
       $this->logger->info(date('Y-m-d H:is') . ' DemoTask  使用注解实现定时任务');
   }
}
第二步:启动服务

php bin/hyperf start

了解一下定时任务的参数

public function __construct(
   public ?string $rule = null,
   public ?string $name = null,
   public string $type = 'callback',
   public ?bool $singleton = null,
   public ?string $mutexPool = null,
   public ?int $mutexExpires = null,
   public ?bool $onOneServer = null,
   public array|string|null $callback = null,
   public ?string $memo = null,
   public array|string|bool $enable = true
) {
}
失败任务
当定时任务执行失败时,会触发 FailToExecute 事件。因此,可以编写一个监听器类监听该事件,拿到对应的定时任务和异常。
第一步:定义异常任务类,用于模拟定时任务失败

<?php

namespace App\Task;

use _PHPStan_1292afebc\Nette\Neon\Exception;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Logger\LoggerFactory;
use Psr\Log\LoggerInterface;

#[Crontab(name: "PostTask", rule: "*\/1 * * * * *", callback: "posts", memo: "post任务'")]
class PostTask
{
   protected LoggerInterface $logger;

   public function __construct(protected LoggerFactory $loggerFactory)
   {
       $this->logger = $this->loggerFactory->get('log', 'default');
   }

   public function posts()
   {
       // 此处报了异常
       throw new Exception('测试定时任务');

       // 正常业务逻辑
       $this->logger->info('文章定时任务');
   }
}
第二步:定义异常监听器

<?php

namespace App\Listener;

use Hyperf\Crontab\Event\FailToExecute;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Logger\LoggerFactory;
use Psr\Log\LoggerInterface;

#[Listener]
class FailToExecuteCrontabPostListener implements ListenerInterface
{
   protected LoggerInterface $logger;

   public function __construct(protected LoggerFactory $loggerFactory)
   {
       $this->logger = $this->loggerFactory->get('log', 'default');
   }

   public function listen(): array
   {
       return [
           FailToExecute::class
       ];
   }

   public function process(object $event): void
   {
       $msg =  '定时任务执行失败' .  $event->crontab->getName() . '----' . $event->throwable->getMessage();

       $this->logger->info($msg);
   }
}
$event->crontab->getName() 用于获取出现异常的定时任务名称;

$event->throwable->getMessage() 用于获取定时任务出现异常消息。

第三步:测试

php bin/hyperf.php start
   
tail -f runtime/logs/hyperf.log
# 输出内容如下
[2023-03-18 15:33:12] log.INFO: 定时任务执行失败PostTask----测试定时任务 [] []
[2023-03-18 15:33:13] log.INFO: 定时任务执行失败PostTask----测试定时任务 [] []

调度分发策略

定时任务在设计上允许通过不同的策略来调度分发执行任务,目前仅提供了 多进程执行策略、协程执行策略 两种策略,默认为 多进程执行策略。

案例:修改为协程风格时,只需要修改config/autoload/dependencies.php文件,配置如下:

<?php

declare(strict_types=1);
return [
   'HttpServer' => Hyperf\HttpServer\Server::class,
   // 修改为协程风格
   \Hyperf\Crontab\Strategy\StrategyInterface::class =>  \Hyperf\Crontab\Strategy\CoroutineStrategy::class
];

相关文章

  • laravel 定时任务秒级执行

    定时任务的用法请参考:laravel 定时任务通过队列发送邮件 只需在自定义的someCmd.php文件的hand...

  • SpringCloud学习笔记

    Quartz定时任务用法 1.在 build.gradle 里面添加依赖 compilegroup:'org.qu...

  • 2018-07-09 python中的轻量级定时任务调度库:sc

    转载自: python中的轻量级定时任务调度库:schedule 提到定时任务调度的时候,相信很多人会想到芹菜ce...

  • AlarmManager实现定时任务

    AlarmManager用于创建定时任务,可以在约定的时间发送广播,开启服务等。 用法: 创建AlarmManag...

  • Linux利用Crontab定时访问某个路由地址

    文章出自虎书博客,转载请注明出处。 首先利用linux的Crontab命令创建定时任务,然后结合curl实现定时访...

  • 2019-07-31定时任务

    定时任务 定时任务实现方法 系统默认定时任务 用户自定义设置定时任务 定时任务配置文件 定时任务启动 定时任务样例...

  • crontab命令总结

    基本用法 crontab命令用于在Linux服务器上设置定时任务,通过crontab --help可以查看命令使用...

  • 常用的几种Linux功能介绍

    1、Ubuntu下定时任务的实现 Linux中的crond Ubuntu下crontab命令的用法 如何利用cro...

  • 分布式定时调度-xxl-job

    一、定时任务概述 1.1 定时任务认识 1.1.1 什么是定时任务 定时任务是按照指定时间周期运行任务。使用场景为...

  • day 22 操作系统定时任务

    系统定时任务概念==生活中闹钟 系统定时任务实现方法: 实现定时任务配置: 定时任务如何进行设置 定时任务编写常见...

网友评论

      本文标题:hf3.0 转载自如出 定时任务用法

      本文链接:https://www.haomeiwen.com/subject/zvmfpdtx.html