Completed
Push — master ( 08ce7b...011452 )
by Stefano
02:35
created

Job   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 15
lcom 2
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
B queue() 0 9 5
A register() 0 3 1
A cleanQueue() 0 4 2
A execute() 0 9 3
A run() 0 9 2
A error() 0 4 1
A retry() 0 4 1
1
<?php
2
3
/**
4
 * Job
5
 *
6
 * Simple, database based job queue.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015-2016 - http://caffeina.com
11
 */
12
13
/*
14
15
Here is the table needed :
16
17
CREATE TABLE `queue` (
18
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
19
  `type` varchar(128) DEFAULT NULL,
20
  `status` enum('PENDING','ACTIVE','DONE','ERROR') NOT NULL DEFAULT 'PENDING',
21
  `tries` int(5) unsigned DEFAULT '0',
22
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
23
  `scheduled_at` timestamp NULL,
24
  `activated_at` timestamp NULL DEFAULT NULL,
25
  `payload` text,
26
  `error` text,
27
  PRIMARY KEY (`id`),
28
  KEY `type` (`type`),
29
  KEY `status` (`status`),
30
  KEY `scheduled_at` (`scheduled_at`)
31
) CHARSET=utf8;
32
33
34
*/
35
36
class Job extends Model {
37
  const _PRIMARY_KEY_ = "queue.id";
38
39
  public $id,
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
40
         $type,
41
         $status = 'PENDING',
42
         $tries = 0,
43
         $created_at,
44
         $scheduled_at,
45
         $activated_at,
46
         $payload,
47
         $error;
48
49
  public static function queue($type, $payload=null, $when=null){
50
    $now = gmdate("Y-m-d H:i:s");
51
    $job = new static;
52
    $job->type         = $type;
53
    $job->created_at   = $now;
54
    $job->scheduled_at = $when ? gmdate("Y-m-d H:i:s",(is_int($when) ? $when : (strtotime($when)?:time()))) : $now;
55
    $job->payload      = $payload !== null ? serialize($payload) : null;
56
    $job->save();
57
  }
58
59
  public static function register($type, $callback){
60
    self::on("worker[{$type}]", $callback);
61
  }
62
63
  public static function cleanQueue($all=false){
64
    $statuses = $all ? "'DONE','ERROR'" : "'DONE'";
65
    return SQL::exec("DELETE FROM `".static::persistenceOptions('table')."` WHERE `status` IN ($statuses)");
66
  }
67
68
  public static function execute(){
69
    $condition = "status = 'PENDING' and `scheduled_at` <= NOW() ORDER BY `scheduled_at` ASC LIMIT 1";
70
    if (($job = static::where($condition)) && ($job = current($job))){
0 ignored issues
show
Documentation introduced by
$condition is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
      // Lock chosen job rapidly
72
      SQL::update(static::persistenceOptions('table'),['id' => $job->id, 'status' => 'ACTIVE']);
73
      $job->run();
74
      return $job;
75
    } else return false;
76
  }
77
78
  public function run(){
79
    $this->status = 'ACTIVE';
80
    $this->activated_at = gmdate("Y-m-d H:i:s");
81
    $this->tries++;
82
    $this->save();
83
    $this->status = 'DONE';
84
    self::trigger("worker[{$this->type}]", $this, $this->payload ? unserialize($this->payload) : null);
85
    $this->save();
86
  }
87
88
  public function error($message=null){
89
    $this->status = 'ERROR';
90
    $this->error  = $message;
91
  }
92
93
  public function retry($message=null){
94
    $this->status = 'PENDING';
95
    $this->error  = $message;
96
  }
97
98
}
99