Skip to content

Commit bcaaefb

Browse files
authored
Merge pull request pterodactyl#155 from Pterodactyl/feature/database-improvements
Foreign Keys & Deletion Improvements
2 parents 045864a + dabd943 commit bcaaefb

28 files changed

+931
-80
lines changed

.env.example

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ APP_KEY=SomeRandomString3232RandomString
44
APP_THEME=default
55
APP_TIMEZONE=UTC
66
APP_CLEAR_TASKLOG=720
7+
APP_DELETE_MINUTES=10
78
CONSOLE_PUSH_FREQ=250
89
CONSOLE_PUSH_COUNT=10
910

@@ -15,7 +16,6 @@ DB_PASSWORD=secret
1516

1617
CACHE_DRIVER=file
1718
SESSION_DRIVER=database
18-
QUEUE_DRIVER=database
1919

2020
MAIL_DRIVER=smtp
2121
MAIL_HOST=mailtrap.io
@@ -28,3 +28,12 @@ MAIL_FROM=you@example.com
2828
API_PREFIX=api
2929
API_VERSION=v1
3030
API_DEBUG=false
31+
32+
QUEUE_DRIVER=database
33+
QUEUE_HIGH=high
34+
QUEUE_STANDARD=standard
35+
QUEUE_LOW=low
36+
37+
SQS_KEY=aws-public
38+
SQS_SECRET=aws-secret
39+
SQS_QUEUE_PREFIX=aws-queue-prefix

app/Console/Commands/RunTasks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function handle()
7474

7575
foreach ($tasks as &$task) {
7676
$bar->advance();
77-
$this->dispatch(new SendScheduledTask(Models\Server::findOrFail($task->server), $task));
77+
$this->dispatch((new SendScheduledTask(Models\Server::findOrFail($task->server), $task))->onQueue(env('QUEUE_LOW', 'low')));
7878
}
7979

8080
$bar->finish();

app/Events/ServerDeleted.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Events;
25+
26+
use Illuminate\Queue\SerializesModels;
27+
28+
class ServerDeleted
29+
{
30+
use SerializesModels;
31+
32+
public $server;
33+
34+
/**
35+
* Create a new event instance.
36+
*
37+
* @return void
38+
*/
39+
public function __construct($id)
40+
{
41+
$this->server = $id;
42+
}
43+
44+
}

app/Http/Controllers/Admin/ServersController.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct()
5151

5252
public function getIndex(Request $request)
5353
{
54-
$query = Models\Server::select(
54+
$query = Models\Server::withTrashed()->select(
5555
'servers.*',
5656
'nodes.name as a_nodeName',
5757
'users.email as a_ownerEmail',
@@ -84,7 +84,7 @@ public function getIndex(Request $request)
8484
$servers = $query->paginate(20);
8585
} catch (\Exception $ex) {
8686
Alert::warning('There was an error with the search parameters provided.');
87-
$servers = Models\Server::select(
87+
$servers = Models\Server::withTrashed()->select(
8888
'servers.*',
8989
'nodes.name as a_nodeName',
9090
'users.email as a_ownerEmail',
@@ -112,7 +112,7 @@ public function getNew(Request $request)
112112

113113
public function getView(Request $request, $id)
114114
{
115-
$server = Models\Server::select(
115+
$server = Models\Server::withTrashed()->select(
116116
'servers.*',
117117
'nodes.name as a_nodeName',
118118
'users.email as a_ownerEmail',
@@ -394,7 +394,7 @@ public function deleteServer(Request $request, $id, $force = null)
394394
try {
395395
$server = new ServerRepository;
396396
$server->deleteServer($id, $force);
397-
Alert::success('Server was successfully deleted from the panel and the daemon.')->flash();
397+
Alert::success('Server has been marked for deletion on the system.')->flash();
398398
return redirect()->route('admin.servers');
399399
} catch (DisplayException $ex) {
400400
Alert::danger($ex->getMessage())->flash();
@@ -510,4 +510,31 @@ public function postUnsuspendServer(Request $request, $id)
510510
}
511511
}
512512

513+
public function postQueuedDeletionHandler(Request $request, $id)
514+
{
515+
try {
516+
$repo = new ServerRepository;
517+
if (!is_null($request->input('cancel'))) {
518+
$repo->cancelDeletion($id);
519+
Alert::success('Server deletion has been cancelled. This server will remain suspended until you unsuspend it.')->flash();
520+
return redirect()->route('admin.servers.view', $id);
521+
} else if(!is_null($request->input('delete'))) {
522+
$repo->deleteNow($id);
523+
Alert::success('Server was successfully deleted from the system.')->flash();
524+
return redirect()->route('admin.servers');
525+
} else if(!is_null($request->input('force_delete'))) {
526+
$repo->deleteNow($id, true);
527+
Alert::success('Server was successfully force deleted from the system.')->flash();
528+
return redirect()->route('admin.servers');
529+
}
530+
} catch (DisplayException $ex) {
531+
Alert::danger($ex->getMessage())->flash();
532+
return redirect()->route('admin.servers.view', $id);
533+
} catch (\Exception $ex) {
534+
Log::error($ex);
535+
Alert::danger('An unhandled error occured while attempting to perform this action.')->flash();
536+
return redirect()->route('admin.servers.view', $id);
537+
}
538+
}
539+
513540
}

app/Http/Routes/AdminRoutes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ public function map(Router $router) {
210210
'uses' => 'Admin\ServersController@deleteServer'
211211
]);
212212

213+
$router->post('/view/{id}/queuedDeletion', [
214+
'uses' => 'Admin\ServersController@postQueuedDeletionHandler',
215+
'as' => 'admin.servers.post.queuedDeletion'
216+
]);
217+
213218
});
214219

215220
// Node Routes

app/Jobs/DeleteServer.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Jobs;
25+
26+
use DB;
27+
28+
use Illuminate\Bus\Queueable;
29+
use Illuminate\Queue\SerializesModels;
30+
use Illuminate\Queue\InteractsWithQueue;
31+
use Illuminate\Contracts\Queue\ShouldQueue;
32+
33+
use Pterodactyl\Models;
34+
use Pterodactyl\Repositories\ServerRepository;
35+
36+
class DeleteServer extends Job implements ShouldQueue
37+
{
38+
use InteractsWithQueue, SerializesModels;
39+
40+
/**
41+
* Id of server to be deleted.
42+
* @var object
43+
*/
44+
protected $id;
45+
46+
/**
47+
* Create a new job instance.
48+
*
49+
* @param integer $server
50+
* @return void
51+
*/
52+
public function __construct($id)
53+
{
54+
$this->id = $id;
55+
}
56+
57+
/**
58+
* Execute the job.
59+
*
60+
* @return void
61+
*/
62+
public function handle()
63+
{
64+
$repo = new ServerRepository;
65+
$repo->deleteNow($this->id);
66+
}
67+
}

app/Jobs/SuspendServer.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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\Jobs;
25+
26+
use Debugbar;
27+
use Illuminate\Bus\Queueable;
28+
use Illuminate\Queue\SerializesModels;
29+
use Illuminate\Queue\InteractsWithQueue;
30+
use Illuminate\Contracts\Queue\ShouldQueue;
31+
32+
use Pterodactyl\Repositories\ServerRepository;
33+
34+
class SuspendServer extends Job implements ShouldQueue
35+
{
36+
use InteractsWithQueue, SerializesModels;
37+
38+
/**
39+
* ID of associated server model.
40+
* @var object
41+
*/
42+
protected $id;
43+
44+
/**
45+
* Create a new job instance.
46+
*
47+
* @param integer $id
48+
* @return void
49+
*/
50+
public function __construct($id)
51+
{
52+
$this->id = $id;
53+
}
54+
55+
/**
56+
* Execute the job.
57+
*
58+
* @return void
59+
*/
60+
public function handle()
61+
{
62+
$repo = new ServerRepository;
63+
$repo->suspend($this->id, true);
64+
}
65+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\Listeners;
25+
26+
use Carbon;
27+
28+
use Pterodactyl\Events\ServerDeleted;
29+
use Illuminate\Foundation\Bus\DispatchesJobs;
30+
31+
use Pterodactyl\Jobs\SuspendServer;
32+
use Pterodactyl\Jobs\DeleteServer;
33+
34+
class DeleteServerListener
35+
{
36+
37+
use DispatchesJobs;
38+
39+
/**
40+
* Create the event listener.
41+
*
42+
* @return void
43+
*/
44+
public function __construct()
45+
{
46+
//
47+
}
48+
49+
/**
50+
* Handle the event.
51+
*
52+
* @param DeleteServerEvent $event
53+
* @return void
54+
*/
55+
public function handle(ServerDeleted $event)
56+
{
57+
$this->dispatch((new SuspendServer($event->server))->onQueue(env('QUEUE_HIGH', 'high')));
58+
$this->dispatch(
59+
(new DeleteServer($event->server))
60+
->delay(Carbon::now()->addMinutes(env('APP_DELETE_MINUTES', 10)))
61+
->onQueue(env('QUEUE_STANDARD', 'standard'))
62+
);
63+
}
64+
}

0 commit comments

Comments
 (0)