1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace crmeb\basic;
- use crmeb\interfaces\JobInterface;
- use think\facade\Log;
- use think\queue\Job;
- class BaseJobs implements JobInterface
- {
-
- public function __call($name, $arguments)
- {
- $this->fire(...$arguments);
- }
-
- 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();
- }
- }
-
- protected function runJob(string $action, Job $job, array $infoData, int $errorCount = 3)
- {
- $action = method_exists($this, $action) ? $action : 'handle';
- if (!method_exists($this, $action)) {
- $job->delete();
- }
- if ($this->{$action}(...$infoData)) {
-
- $job->delete();
- } else {
- if ($job->attempts() >= $errorCount && $errorCount) {
-
- $job->delete();
- } else {
-
- $job->release();
- }
- }
- }
- }
|