laravel怎样在指定的时间执行任务?

微信小程序的项目,后端使用的是laravel ,需求是用户预约上课后,在开课的前一个小时给该用户发送订阅消息进行提醒,比如约的是10:30的课,要在9:30发送订阅消息,laravel端怎样实现这样的定时任务?

当我尝试使用延时队列时,http请求的参数怎样传递给队列job呢?总是报错!

TypeError: Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null …

队列job

namespace App\Jobs;
class sendPrebookTip implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $access_token;
    protected $template_id;
    protected $touser;
    protected $params;

    public function __construct($access_token,$template_id,$touser,$params)
    {
        $this -> acess_token = $access_token;
        $this-> template_id = $template_id;
        $this-> touser = $touser;
        $this-> params = $params;
    }
    public function handle( )
    {

        Http::post('https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$this->acess_token,[
            'template_id' => $this->template_id,
            'touser' => $this->touser,
            "page" => "/pages/index/index",
            "miniprogram_state" => "developer",
            "lang" => "zh_CN",
            "data" => $this-> params
        ]);
    }
}

控制器

namespace App\Http\Controllers;
use App\Jobs\sendPrebookTip;        //队列任务
public function wxPrebook(Request $request)
{
    // 发送服务消息
        $APPID=$request->appId;
        $SECRET=$request->appSecret;
        $touser=$request->openId;
        $template_id= 'H9eIpHa6XiqLS5tsaEe23N4YhSXrfhVHPqZHRuAicog';
        $access_token = Http::get('api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$APPID.'&secret='.$SECRET)['access_token'];
        $params = [
            'time4' => ['value' => '09:00'],
            'thing3' => ['value' => '609教室'],
            'thing2' => ['value' => '王教练'],
            'thing1' => ['value' => '001'],
        ];
    // 定时发送队列消息
        sendPrebookTip::dispatch($access_token,$template_id,$touser,$params)
            ->delay(now()->addMinutes(2));
}
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
slowlyo
最佳答案
通常使用以下两种思路
  • 把待提醒的记录放到 延时队列
  • 使用 任务调度 定时查询待提醒的记录
    • 查询 未提醒 的记录 (注意一次不要查询太多, 查询加上 limit , 按照提醒时间升序排序)
    • 如果符合发送提醒的条件则发送提醒, 并标记为 已提醒

哪种都行, 结合实际场景考虑

1个月前 评论
liuhoupan (楼主) 1个月前
liuhoupan (楼主) 1个月前
sanders 1个月前
讨论数量: 10
slowlyo
通常使用以下两种思路
  • 把待提醒的记录放到 延时队列
  • 使用 任务调度 定时查询待提醒的记录
    • 查询 未提醒 的记录 (注意一次不要查询太多, 查询加上 limit , 按照提醒时间升序排序)
    • 如果符合发送提醒的条件则发送提醒, 并标记为 已提醒

哪种都行, 结合实际场景考虑

1个月前 评论
liuhoupan (楼主) 1个月前
liuhoupan (楼主) 1个月前
sanders 1个月前
slowlyo
通常使用以下两种思路
  • 把待提醒的记录放到 延时队列
  • 使用 任务调度 定时查询待提醒的记录
    • 查询 未提醒 的记录 (注意一次不要查询太多, 查询加上 limit , 按照提醒时间升序排序)
    • 如果符合发送提醒的条件则发送提醒, 并标记为 已提醒

哪种都行, 结合实际场景考虑

1个月前 评论
liuhoupan (楼主) 1个月前
liuhoupan (楼主) 1个月前
sanders 1个月前

按天/小时查询当天/1小时内需要提醒的,加人延时队列(如半小时前提醒)提醒或直接提醒

按天、小时查询可以避免查询数据太多造成的卡顿

1个月前 评论
liuhoupan (楼主) 1个月前

延时队列

1个月前 评论

肯定是延时队列更合适

1个月前 评论
liuhoupan (楼主) 1个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!