|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kop\kue\resources; |
|
4
|
|
|
|
|
5
|
|
|
use kop\kue\Client; |
|
6
|
|
|
use kop\kue\exceptions\ClientException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class `Job` |
|
10
|
|
|
* =========== |
|
11
|
|
|
* |
|
12
|
|
|
* This class represents a `Job` resource of the Kue JSON API. |
|
13
|
|
|
* |
|
14
|
|
|
* |
|
15
|
|
|
* @link https://kop.github.io/php-kue-client/ Project page. |
|
16
|
|
|
* @license https://github.com/kop/php-kue-client/blob/master/LICENSE.md MIT |
|
17
|
|
|
* |
|
18
|
|
|
* @author Ivan Koptiev <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class Job |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Client $client Kue API client instance. |
|
24
|
|
|
*/ |
|
25
|
|
|
private $client; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param Client $client Kue API client instance. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Client $client) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->client = $client; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Search jobs. |
|
39
|
|
|
* |
|
40
|
|
|
* Jobs can be searched with a full text search (when search query is a string), |
|
41
|
|
|
* or with an extended search (when search query is an array). |
|
42
|
|
|
* |
|
43
|
|
|
* Full text search feature is turned off by default since Kue >=0.9.0. |
|
44
|
|
|
* Please {@link https://github.com/Automattic/kue#get-jobsearchq follow the Kue documentation} if you want to enable it. |
|
45
|
|
|
* |
|
46
|
|
|
* In case of extended search, the following array keys are recognized: |
|
47
|
|
|
* |
|
48
|
|
|
* - `from` _(required)_ - filters jobs with the specified ID range, starting with `from`; |
|
49
|
|
|
* - `to` _(required)_ - filters jobs with the specified ID range, ending with `to`; |
|
50
|
|
|
* - `state` - filters jobs by state, this field is required if `type` is also set; |
|
51
|
|
|
* - `type` - filters jobs by type; |
|
52
|
|
|
* - `order` - orders results by `asc` or `desc`. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string|array $query Search query. |
|
55
|
|
|
* |
|
56
|
|
|
* @return bool|mixed API response or `false` if API request fails. |
|
57
|
|
|
* @throws \kop\kue\exceptions\ApiException If API request fails and {@link Client::$_throwExceptions} is enabled. |
|
58
|
|
|
* @throws \kop\kue\exceptions\ClientException If search request is composed wrong and {@link Client::$_throwExceptions} is enabled. |
|
59
|
|
|
* |
|
60
|
|
|
* @see https://github.com/Automattic/kue#get-jobsearchq |
|
61
|
|
|
* @see https://github.com/Automattic/kue#get-jobsfromtoorder |
|
62
|
|
|
* @see https://github.com/Automattic/kue#get-jobsstatefromtoorder |
|
63
|
|
|
* @see https://github.com/Automattic/kue#get-jobstypestatefromtoorder |
|
64
|
|
|
*/ |
|
65
|
|
|
public function search($query) |
|
66
|
|
|
{ |
|
67
|
|
|
// Search by query string |
|
68
|
|
|
if (is_string($query)) { |
|
69
|
|
|
return $this->client->request('GET', 'job/search', [ |
|
70
|
|
|
'query' => [ |
|
71
|
|
|
'q' => $query, |
|
72
|
|
|
] |
|
73
|
|
|
]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Extended search |
|
77
|
|
|
if (is_array($query) && isset($query['from'], $query['to'])) { |
|
78
|
|
|
$requestPath = ''; |
|
79
|
|
|
if (in_array('order', $query)) { |
|
80
|
|
|
$requestPath = "/{$query['order']}" . $requestPath; |
|
81
|
|
|
} |
|
82
|
|
|
$requestPath = "/{$query['from']}..{$query['to']}" . $requestPath; |
|
83
|
|
|
if (in_array('state', $query)) { |
|
84
|
|
|
$requestPath = "/{$query['state']}" . $requestPath; |
|
85
|
|
|
} |
|
86
|
|
|
if (in_array('type', $query)) { |
|
87
|
|
|
$requestPath = "/{$query['type']}" . $requestPath; |
|
88
|
|
|
} |
|
89
|
|
|
return $this->client->request('GET', "jobs{$requestPath}"); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->client->getLogger()->error('Jobs search request is composed wrong!'); |
|
93
|
|
|
if ($this->client->getThrowExceptions()) { |
|
94
|
|
|
throw new ClientException('Jobs search request is composed wrong!'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Creates a new job. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $type Job type. |
|
104
|
|
|
* @param array $data Job data. |
|
105
|
|
|
* @param array $options Kue job options (e.g. attempts, priority, etc). |
|
106
|
|
|
* |
|
107
|
|
|
* @return bool|mixed API response or `false` if API request fails. |
|
108
|
|
|
* @throws \kop\kue\exceptions\ApiException If API request fails and {@link Client::$_throwExceptions} is enabled. |
|
109
|
|
|
* |
|
110
|
|
|
* @see https://github.com/Automattic/kue#post-job |
|
111
|
|
|
*/ |
|
112
|
|
|
public function create($type, $data, $options = []) |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->client->request('POST', 'job', [ |
|
115
|
|
|
'json' => [ |
|
116
|
|
|
'type' => $type, |
|
117
|
|
|
'data' => $data, |
|
118
|
|
|
'options' => $options, |
|
119
|
|
|
] |
|
120
|
|
|
]); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns a job by it's ID. |
|
125
|
|
|
* |
|
126
|
|
|
* @param integer $id Job ID. |
|
127
|
|
|
* |
|
128
|
|
|
* @return bool|mixed API response or `false` if API request fails. |
|
129
|
|
|
* @throws \kop\kue\exceptions\ApiException If API request fails and {@link Client::$_throwExceptions} is enabled. |
|
130
|
|
|
* |
|
131
|
|
|
* @see https://github.com/Automattic/kue#get-jobid |
|
132
|
|
|
*/ |
|
133
|
|
|
public function get($id) |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->client->request('GET', "job/{$id}"); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Returns job logs by job ID. |
|
140
|
|
|
* |
|
141
|
|
|
* @param integer $id Job ID. |
|
142
|
|
|
* |
|
143
|
|
|
* @return bool|mixed API response or `false` if API request fails. |
|
144
|
|
|
* @throws \kop\kue\exceptions\ApiException If API request fails and {@link Client::$_throwExceptions} is enabled. |
|
145
|
|
|
* |
|
146
|
|
|
* @see https://github.com/Automattic/kue#get-jobidlog |
|
147
|
|
|
*/ |
|
148
|
|
|
public function logs($id) |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->client->request('GET', "job/{$id}/log"); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Deletes a job by it's ID. |
|
155
|
|
|
* |
|
156
|
|
|
* @param integer $id Job ID. |
|
157
|
|
|
* |
|
158
|
|
|
* @return bool|mixed API response or `false` if API request fails. |
|
159
|
|
|
* @throws \kop\kue\exceptions\ApiException If API request fails and {@link Client::$_throwExceptions} is enabled. |
|
160
|
|
|
* |
|
161
|
|
|
* @see https://github.com/Automattic/kue#delete-jobid |
|
162
|
|
|
*/ |
|
163
|
|
|
public function delete($id) |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->client->request('DELETE', "job/{$id}"); |
|
166
|
|
|
} |
|
167
|
|
|
} |