1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in all |
14
|
|
|
* copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22
|
|
|
* SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Pterodactyl\Jobs; |
26
|
|
|
|
27
|
|
|
use Cron; |
28
|
|
|
use Carbon; |
29
|
|
|
use Pterodactyl\Models\Task; |
30
|
|
|
use Pterodactyl\Models\TaskLog; |
31
|
|
|
use Illuminate\Queue\SerializesModels; |
32
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
33
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
34
|
|
|
use Pterodactyl\Repositories\Daemon\PowerRepository; |
35
|
|
|
use Pterodactyl\Repositories\Daemon\CommandRepository; |
36
|
|
|
|
37
|
|
|
class SendScheduledTask extends Job implements ShouldQueue |
38
|
|
|
{ |
39
|
|
|
use InteractsWithQueue, SerializesModels; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \Pterodactyl\Models\Task |
43
|
|
|
*/ |
44
|
|
|
protected $task; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a new job instance. |
48
|
|
|
* |
49
|
|
|
* @return void |
|
|
|
|
50
|
|
|
*/ |
51
|
|
|
public function __construct(Task $task) |
52
|
|
|
{ |
53
|
|
|
$this->task = $task; |
54
|
|
|
|
55
|
|
|
$this->task->queued = true; |
56
|
|
|
$this->task->save(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Execute the job. |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function handle() |
65
|
|
|
{ |
66
|
|
|
$time = Carbon::now(); |
67
|
|
|
$log = new TaskLog; |
68
|
|
|
|
69
|
|
|
if ($this->attempts() >= 1) { |
70
|
|
|
// Just delete the job, we will attempt it again later anyways. |
71
|
|
|
$this->delete(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
if ($this->task->action === 'command') { |
76
|
|
|
$repo = new CommandRepository($this->task->server, $this->task->user); |
77
|
|
|
$response = $repo->send($this->task->data); |
78
|
|
|
} elseif ($this->task->action === 'power') { |
79
|
|
|
$repo = new PowerRepository($this->task->server, $this->task->user); |
80
|
|
|
$response = $repo->do($this->task->data); |
81
|
|
|
} else { |
82
|
|
|
throw new \Exception('Task type provided was not valid.'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$log->fill([ |
86
|
|
|
'task_id' => $this->task->id, |
87
|
|
|
'run_time' => $time, |
88
|
|
|
'run_status' => 0, |
89
|
|
|
'response' => $response, |
90
|
|
|
]); |
91
|
|
|
} catch (\Exception $ex) { |
92
|
|
|
$log->fill([ |
93
|
|
|
'task_id' => $this->task->id, |
94
|
|
|
'run_time' => $time, |
95
|
|
|
'run_status' => 1, |
96
|
|
|
'response' => $ex->getMessage(), |
97
|
|
|
]); |
98
|
|
|
} finally { |
99
|
|
|
$cron = Cron::factory(sprintf('%s %s %s %s %s %s', |
100
|
|
|
$this->task->minute, |
101
|
|
|
$this->task->hour, |
102
|
|
|
$this->task->day_of_month, |
103
|
|
|
$this->task->month, |
104
|
|
|
$this->task->day_of_week, |
105
|
|
|
$this->task->year |
106
|
|
|
)); |
107
|
|
|
$this->task->fill([ |
108
|
|
|
'last_run' => $time, |
109
|
|
|
'next_run' => $cron->getNextRunDate(), |
110
|
|
|
'queued' => false, |
111
|
|
|
]); |
112
|
|
|
$this->task->save(); |
113
|
|
|
$log->save(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.