Files
huangjingfen/pro_v3.5.1/crmeb/basic/BaseJobs.php
panchengyong c1e74d8e68 chore(php): 统一 ScottPan 文件头与注释域名替换
- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明

Made-with: Cursor
2026-03-29 11:22:58 +08:00

72 lines
1.6 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
namespace crmeb\basic;
use crmeb\interfaces\JobInterface;
use think\queue\Job;
/**
* 消息队列基类
* Class BaseJobs
* @package crmeb\basic
*/
class BaseJobs implements JobInterface
{
/**
* 运行消息队列
* @param Job $job
* @param $data
*/
public function fire(Job $job, $data): void
{
try {
$action = $data['do'] ?? 'doJob';//任务名
$infoData = $data['data'] ?? [];//执行数据
$errorCount = $data['errorCount'] ?? 0;//最大错误次数
$this->runJob($action, $job, $infoData, $errorCount);
} catch (\Throwable $e) {
$job->delete();
}
}
/**
* 执行队列
* @param string $action
* @param Job $job
* @param array $infoData
* @param int $errorCount
*/
protected function runJob(string $action, Job $job, array $infoData, int $errorCount = 3)
{
if (!class_exists(get_class($this))) {
$job->delete();
} else {
$action = method_exists($this, $action) ? $action : 'handle';
if (!method_exists($this, $action)) {
$job->delete();
} else {
if ($this->{$action}(...$infoData)) {
//删除任务
$job->delete();
} else {
if ($job->attempts() >= $errorCount && $errorCount) {
//删除任务
$job->delete();
} else {
//从新放入队列
$job->release();
}
}
}
}
}
public function failed($data)
{
}
}