123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace app\admin\service;
- use think\Db;
- class QueueService
- {
-
- const STATUS_PEND = 1;
-
- const STATUS_PROC = 2;
-
- const STATUS_COMP = 3;
-
- const STATUS_FAIL = 4;
-
- public static function add($title, $uri, $later, array $data, $double = 1, $desc = '')
- {
- if (empty($double) && self::exists($title)) {
- throw new \think\Exception('该任务已经创建,请耐心等待处理完成!');
- }
- $jobId = Db::name('SystemJobsLog')->insertGetId([
- 'title' => $title, 'later' => $later, 'uri' => $uri, 'double' => intval($double),
- 'data' => json_encode($data, 256), 'desc' => $desc, 'status_at' => date('Y-m-d H:i:s'),
- ]);
- $data['_job_id_'] = $jobId;
- $data['_job_title_'] = $title;
- \think\Queue::later($later, $uri, $data);
- }
-
- public static function status($jobId, $status = self::STATUS_PEND, $statusDesc = '')
- {
- $result = Db::name('SystemJobsLog')->where(['id' => $jobId])->update([
- 'status' => $status, 'status_desc' => $statusDesc, 'status_at' => date('Y-m-d H:i:s'),
- ]);
- return $result !== false;
- }
-
- public static function exists($title)
- {
- $where = [['title', 'eq', $title], ['status', 'in', [1, 2]]];
- return Db::name('SystemJobsLog')->where($where)->count() > 0;
- }
-
- public static function get($jobId)
- {
- return Db::name('SystemJobsLog')->where(['id' => $jobId])->find();
- }
-
- public static function del($jobId)
- {
- $where = [['id', 'eq', $jobId], ['status', 'in', [1, 3, 4]]];
- if (Db::name('SystemJobsLog')->where($where)->delete() > 0) {
- Db::name('SystemJobs')->whereLike('payload', '%"_job_id_":"' . $jobId . '"%')->delete();
- return true;
- } else {
- return false;
- }
- }
- }
|