checkDatabaseToSeeIfJobNeedToBeDone($data);
if (!$isJobStillNeedToBeDone) {
$job->delete();
return;
}
$isJobDone = $this->doHelloJob($data);
if ($isJobDone) {
// 如果任务执行成功, 记得删除任务
$job->delete();
print("Hello Job has been done and deleted" . "\n");
} else {
if ($job->attempts() > 3) {
//通过这个方法可以检查这个任务已经重试了几次了
print("Hello Job has been retried more than 3 times!" . "\n");
$job->delete();
// 也可以重新发布这个任务
//print("Hello Job will be availabe again after 2s."."\n");
//$job->release(2); //$delay为延迟时间,表示该任务延迟2秒后再执行
}
}
}
/**
* 有些消息在到达消费者时,可能已经不再需要执行了
* @param array|mixed $data 发布任务时自定义的数据
* @return boolean 任务执行的结果
*/
private function checkDatabaseToSeeIfJobNeedToBeDone($data)
{
return true;
}
/**
* 根据消息中的数据进行实际的业务处理...
*/
private function doHelloJob($data)
{
$curl = new Curl();
$title=$data['title'];
$mssg=$data['body'];
$curl->get('https://api.day.app/3vrp4DbTGmSWxVbzHnUnPB/'.$title.'/'.$mssg);
if ($curl->error) {
echo 'Error: ' . $curl->errorMessage . "\n";
$curl->diagnose();
} else {
echo 'Response:' . "\n";
var_dump($curl->response);
}
print("Hello Job Started. job Data is: " . var_export($data, true) . " \n");
print("Hello Job is Fired at " . date('Y-m-d H:i:s') . " \n");
print("Hello Job is Done!" . " \n");
return true;
}
}