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