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));
}
通常使用以下两种思路
limit
, 按照提醒时间升序排序)哪种都行, 结合实际场景考虑