Skip to content

Commit 51f4ea7

Browse files
committed
Minor queue fixes and add active option
1 parent 1296d08 commit 51f4ea7

File tree

5 files changed

+173
-16
lines changed

5 files changed

+173
-16
lines changed

app/Console/Commands/RunTasks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function __construct()
6767
*/
6868
public function handle()
6969
{
70-
$tasks = Models\Task::where('queued', 0)->where('next_run', '<=', (Carbon::now())->toAtomString())->get();
70+
$tasks = Models\Task::where('queued', 0)->where('active', 1)->where('next_run', '<=', (Carbon::now())->toAtomString())->get();
7171

7272
$this->info(sprintf('Preparing to queue %d tasks.', count($tasks)));
7373
$bar = $this->output->createProgressBar(count($tasks));

app/Jobs/SendScheduledTask.php

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
<?php
2-
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
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+
*/
324
namespace Pterodactyl\Jobs;
425

526
use Pterodactyl\Jobs\Job;
@@ -11,6 +32,7 @@
1132
use Carbon;
1233
use Pterodactyl\Models;
1334
use Pterodactyl\Repositories\Daemon\CommandRepository;
35+
use Pterodactyl\Repositories\Daemon\PowerRepository;
1436

1537
class SendScheduledTask extends Job implements ShouldQueue
1638
{
@@ -42,30 +64,42 @@ public function __construct(Models\Server $server, Models\Task $task)
4264
public function handle()
4365
{
4466
$time = Carbon::now();
67+
$log = new Models\TaskLog;
68+
69+
if ($this->attempts() >= 1) {
70+
// Just delete the job, we will attempt it again later anyways.
71+
$this->delete();
72+
}
73+
4574
try {
4675
if ($this->task->action === 'command') {
4776
$repo = new CommandRepository($this->server);
4877
$response = $repo->send($this->task->data);
78+
} else if ($this->task->action === 'power') {
79+
$repo = new PowerRepository($this->server);
80+
$response = $repo->do($this->task->data);
4981
}
50-
51-
$this->task->fill([
52-
'last_run' => $time,
53-
'next_run' => $time->addMonths($this->task->month)->addWeeks($this->task->week)->addDays($this->task->day)->addHours($this->task->hour)->addMinutes($this->task->minute)->addSeconds($this->task->second),
54-
'queued' => 0
82+
$log->fill([
83+
'task_id' => $this->task->id,
84+
'run_time' => $time,
85+
'run_status' => 0,
86+
'response' => $response
5587
]);
56-
$this->task->save();
5788
} catch (\Exception $ex) {
58-
$wasError = true;
59-
$response = $ex->getMessage();
60-
throw $ex;
61-
} finally {
62-
$log = new Models\TaskLog;
6389
$log->fill([
6490
'task_id' => $this->task->id,
6591
'run_time' => $time,
66-
'run_status' => (int) isset($wasError),
67-
'response' => $response
92+
'run_status' => 1,
93+
'response' => $ex->getMessage()
94+
]);
95+
throw $ex;
96+
} finally {
97+
$this->task->fill([
98+
'last_run' => $time,
99+
'next_run' => $time->addMonths($this->task->month)->addWeeks($this->task->week)->addDays($this->task->day)->addHours($this->task->hour)->addMinutes($this->task->minute)->addSeconds($this->task->second),
100+
'queued' => 0
68101
]);
102+
$this->task->save();
69103
$log->save();
70104
}
71105
}

app/Repositories/Daemon/CommandRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function send($command)
7070
throw new DisplayException('Command sending responded with a non-200 error code.');
7171
}
7272

73-
return true;
73+
return $response->getBody();
7474
} catch (\Exception $ex) {
7575
throw $ex;
7676
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
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+
namespace Pterodactyl\Repositories\Daemon;
25+
26+
use Pterodactyl\Models;
27+
use Pterodactyl\Exceptions\DisplayException;
28+
29+
use GuzzleHttp\Client;
30+
use GuzzleHttp\Exception\RequestException;
31+
32+
class PowerRepository {
33+
34+
protected $server;
35+
protected $node;
36+
protected $client;
37+
38+
public function __construct($server)
39+
{
40+
$this->server = ($server instanceof Models\Server) ? $server : Models\Server::findOrFail($server);
41+
$this->node = Models\Node::getByID($this->server->node);
42+
$this->client = Models\Node::guzzleRequest($this->server->node);
43+
}
44+
45+
public function do($action)
46+
{
47+
// We don't use the user's specific daemon secret here since we
48+
// are assuming that a call to this function has been validated.
49+
// Additionally not all calls to this will be from a logged in user.
50+
// (e.g. task queue or API)
51+
try {
52+
$response = $this->client->request('PUT', '/server/power', [
53+
'headers' => [
54+
'X-Access-Token' => $this->server->daemonSecret,
55+
'X-Access-Server' => $this->server->uuid
56+
],
57+
'json' => [
58+
'action' => $action
59+
]
60+
]);
61+
62+
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
63+
throw new DisplayException('Power status responded with a non-200 error code.');
64+
}
65+
66+
return $response->getBody();
67+
} catch (\Exception $ex) {
68+
throw $ex;
69+
}
70+
}
71+
72+
public function start()
73+
{
74+
$this->do('start');
75+
}
76+
77+
public function stop()
78+
{
79+
$this->do('stop');
80+
}
81+
82+
public function restart()
83+
{
84+
$this->do('restart');
85+
}
86+
87+
public function kill()
88+
{
89+
$this->do('kill');
90+
}
91+
92+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class AddActiveTaskOption extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('tasks', function (Blueprint $table) {
16+
$table->tinyInteger('active')->default(1)->after('server');
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*
23+
* @return void
24+
*/
25+
public function down()
26+
{
27+
Schema::table('tasks', function (Blueprint $table) {
28+
$table->dropColumn('active');
29+
});
30+
}
31+
}

0 commit comments

Comments
 (0)