1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\Asana; |
4
|
|
|
|
5
|
|
|
use Helix\Asana\Base\AbstractEntity; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* An asynchronous job. |
9
|
|
|
* |
10
|
|
|
* @see https://developers.asana.com/docs/asana-jobs |
11
|
|
|
* @see https://developers.asana.com/docs/job |
12
|
|
|
* |
13
|
|
|
* @immutable Jobs may only be polled. |
14
|
|
|
* |
15
|
|
|
* @method null|Project getNewProject () |
16
|
|
|
* @method null|Task getNewTask () |
17
|
|
|
* @method string getResourceSubtype () |
18
|
|
|
* @method string getStatus () |
19
|
|
|
*/ |
20
|
|
|
class Job extends AbstractEntity { |
21
|
|
|
|
22
|
|
|
const TYPE = 'job'; |
23
|
|
|
const TYPE_DUPLICATE_PROJECT = 'duplicate_project'; |
24
|
|
|
const TYPE_DUPLICATE_TASK = 'duplicate_task'; |
25
|
|
|
|
26
|
|
|
const STATUS_QUEUED = 'not_started'; |
27
|
|
|
const STATUS_ACTIVE = 'in_progress'; |
28
|
|
|
const STATUS_SUCCESS = 'succeeded'; // api docs say "completed" but that's wrong. |
29
|
|
|
const STATUS_FAIL = 'failed'; |
30
|
|
|
|
31
|
|
|
protected static $map = [ |
32
|
|
|
'new_project' => Project::class, |
33
|
|
|
'new_task' => Task::class |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
final public function __toString (): string { |
37
|
|
|
return "jobs/{$this->getGid()}"; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Whether the job is in progress. |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function isActive (): bool { |
46
|
|
|
return $this->getStatus() === self::STATUS_ACTIVE; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Whether the job has completed successfully or failed. |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function isDone (): bool { |
55
|
|
|
return $this->isSuccessful() or $this->isFailed(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
final public function isDuplicatingProject (): bool { |
59
|
|
|
return $this->getResourceSubtype() === self::TYPE_DUPLICATE_PROJECT; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
final public function isDuplicatingTask (): bool { |
63
|
|
|
return $this->getResourceSubtype() === self::TYPE_DUPLICATE_TASK; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Whether the job failed. |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function isFailed (): bool { |
72
|
|
|
return $this->getStatus() === self::STATUS_FAIL; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Whether the job has yet to be started. |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function isQueued (): bool { |
81
|
|
|
return $this->getStatus() === self::STATUS_QUEUED; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Whether the job completed successfully. |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function isSuccessful (): bool { |
90
|
|
|
return $this->getStatus() === self::STATUS_SUCCESS; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sleeps a few seconds between reloads until the job completes successfully or fails. |
95
|
|
|
* |
96
|
|
|
* A spinner can be called every sleep cycle to indicate progress. |
97
|
|
|
* |
98
|
|
|
* @param callable $spinner `fn( Job $this ): void` |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function wait (callable $spinner = null) { |
102
|
|
|
while (!$this->isDone()) { |
103
|
|
|
if ($spinner) { |
104
|
|
|
call_user_func($spinner, $this); |
105
|
|
|
} |
106
|
|
|
sleep(3); |
107
|
|
|
$this->reload(); |
108
|
|
|
} |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
} |