-
Notifications
You must be signed in to change notification settings - Fork 26
/
class-job.php
118 lines (96 loc) · 2.87 KB
/
class-job.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace HM\Cavalcade\Runner;
use DateInterval;
use DateTime;
use DateTimeZone;
use PDO;
const MYSQL_DATE_FORMAT = 'Y-m-d H:i:s';
class Job {
public $id;
public $site;
public $hook;
public $args;
public $start;
public $nextrun;
public $interval;
public $status;
protected $db;
protected $table_prefix;
public function __construct( $db, $table_prefix ) {
$this->db = $db;
$this->table_prefix = $table_prefix;
}
public function get_site_url() {
$query = "SHOW TABLES LIKE '{$this->table_prefix}blogs'";
$statement = $this->db->prepare( $query );
$statement->execute();
if ( 0 === $statement->rowCount() ) {
return false;
}
$query = "SELECT domain, path FROM {$this->table_prefix}blogs";
$query .= ' WHERE blog_id = :site';
$statement = $this->db->prepare( $query );
$statement->bindValue( ':site', $this->site );
$statement->execute();
$data = $statement->fetch( PDO::FETCH_ASSOC );
$url = $data['domain'] . $data['path'];
return $url;
}
/**
* Acquire a "running" lock on this job
*
* Ensures that only one supervisor can run the job at once.
*
* @return bool True if we acquired the lock, false if we couldn't.
*/
public function acquire_lock() {
$query = "UPDATE {$this->table_prefix}cavalcade_jobs";
$query .= ' SET status = "running"';
$query .= ' WHERE status = "waiting" AND id = :id';
$statement = $this->db->prepare( $query );
$statement->bindValue( ':id', $this->id );
$statement->execute();
$rows = $statement->rowCount();
return ( $rows === 1 );
}
public function mark_completed() {
$data = [];
if ( $this->interval ) {
$this->reschedule();
} else {
$query = "UPDATE {$this->table_prefix}cavalcade_jobs";
$query .= ' SET status = "completed"';
$query .= ' WHERE id = :id';
$statement = $this->db->prepare( $query );
$statement->bindValue( ':id', $this->id );
$statement->execute();
}
}
public function reschedule() {
$date = new DateTime( $this->nextrun, new DateTimeZone( 'UTC' ) );
$date->add( new DateInterval( "PT{$this->interval}S" ) );
$this->nextrun = $date->format( MYSQL_DATE_FORMAT );
$this->status = 'waiting';
$query = "UPDATE {$this->table_prefix}cavalcade_jobs";
$query .= ' SET status = :status, nextrun = :nextrun';
$query .= ' WHERE id = :id';
$statement = $this->db->prepare( $query );
$statement->bindValue( ':id', $this->id );
$statement->bindValue( ':status', $this->status );
$statement->bindValue( ':nextrun', $this->nextrun );
$statement->execute();
}
/**
* Mark the job as failed.
*
* @param string $message failure detail message
*/
public function mark_failed( $message = '' ) {
$query = "UPDATE {$this->table_prefix}cavalcade_jobs";
$query .= ' SET status = "failed"';
$query .= ' WHERE id = :id';
$statement = $this->db->prepare( $query );
$statement->bindValue( ':id', $this->id );
$statement->execute();
}
}