-
Notifications
You must be signed in to change notification settings - Fork 223
/
Task.php
145 lines (127 loc) · 3.29 KB
/
Task.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Studio\Totem;
use Cron\CronExpression;
use Studio\Totem\Traits\HasFrequencies;
use Illuminate\Notifications\Notifiable;
class Task extends TotemModel
{
use Notifiable, HasFrequencies;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'description',
'command',
'parameters',
'expression',
'timezone',
'is_active',
'dont_overlap',
'run_in_maintenance',
'notification_email_address',
'notification_phone_number',
'notification_slack_webhook',
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = [
'activated',
'upcoming',
];
/**
* Activated Accessor.
*
* @return bool
*/
public function getActivatedAttribute()
{
return $this->is_active;
}
/**
* Upcoming Accessor.
*
* @return string
*/
public function getUpcomingAttribute()
{
return CronExpression::factory($this->getCronExpression())->getNextRunDate()->format('Y-m-d H:i:s');
}
/**
* Convert a string of command arguments and options to an array.
*
* @param bool $console if true will convert arguments to non associative array
* @return array
*/
public function compileParameters($console = false)
{
if ($this->parameters) {
$regex = '/(?=\S)[^\'"\s]*(?:\'[^\']*\'[^\'"\s]*|"[^"]*"[^\'"\s]*)*/';
preg_match_all($regex, $this->parameters, $matches, PREG_SET_ORDER, 0);
$argument_index = 0;
$parameters = collect($matches)->mapWithKeys(function ($parameter) use ($console, &$argument_index) {
$param = explode('=', $parameter[0]);
return count($param) > 1 ? [$param[0] => $param[1]]
: (starts_with($param[0], '--') && ! $console ? [$param[0] => true] : [$argument_index++ => $param[0]]);
})->toArray();
return $parameters;
}
return [];
}
/**
* Results Relation.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function results()
{
return $this->hasMany(Result::class, 'task_id', 'id');
}
/**
* Returns the most recent result entry for this task.
*
* @return Model|null
*/
public function getLastResultAttribute()
{
return $this->results()->orderBy('id', 'desc')->first();
}
/**
* @return float
*/
public function getAverageRuntimeAttribute()
{
return $this->results()->avg('duration') ?? 0.00;
}
/**
* Route notifications for the mail channel.
*
* @return string
*/
public function routeNotificationForMail()
{
return $this->notification_email_address;
}
/**
* Route notifications for the Nexmo channel.
*
* @return string
*/
public function routeNotificationForNexmo()
{
return $this->notification_phone_number;
}
/**
* Route notifications for the Slack channel.
*
* @return string
*/
public function routeNotificationForSlack()
{
return $this->notification_slack_webhook;
}
}