Skip to content

Commit

Permalink
fix server assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiritin committed Aug 31, 2023
1 parent 20a97ab commit de5132b
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protected function schedule(Schedule $schedule): void
$schedule->job(new \App\Jobs\ServerAssignmentJob())->everyFifteenSeconds();
$schedule->job(new \App\Jobs\CheckClientActivityJob())->everyFifteenMinutes();
$schedule->job(new \App\Jobs\CleanUpUnusedClientsJob())->everyMinute();
$schedule->job(new \App\Jobs\CleanUpInactiveServerAssignmentsJob())->everyFiveMinutes();
}

/**
Expand Down
15 changes: 15 additions & 0 deletions app/Http/Controllers/Api/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@ public function play(HookRequest $request)
return new Response("Missing streamkey or client id", 422);
}

// This is the streamkey for external apps
if($result['streamkey'] === config('services.signage.streamkey')) {
return new Response(0, 200);
}

$user = User::where('streamkey', $result['streamkey'])->first();

if (is_null($user)) {
return new Response("No assigned server found by streamkey", 403);
}

if((int) $request->get('server_id') !== $user->server_id && !\App::isLocal()) {
return new Response("Server id does not match", 403);
}

if (isset($result['client'])) {
$client = ($result['client'] === "vlc") ? 'vlc' : 'web';
}
Expand All @@ -66,6 +75,12 @@ public function stop(HookRequest $request)
if (!isset($result['streamkey'])) {
return new Response(422, 422);
}

// This is the streamkey for external apps
if($result['streamkey'] === config('services.signage.streamkey')) {
return new Response(0, 200);
}

$user = User::where('streamkey', $result['streamkey'])->first();
if (is_null($user)) {
return new Response(403, 403);
Expand Down
33 changes: 33 additions & 0 deletions app/Jobs/CleanUpInactiveServerAssignmentsJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Jobs;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class CleanUpInactiveServerAssignmentsJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct()
{
}

public function handle(): void
{
// Get list of Users that connection ended more than 5 minutes ago
$users = User::whereNotNull('server_id')
->whereDoesntHave('clients', fn($q) => $q->where(fn($q) => $q->connected())
->orWhere('stop', '>', now()->subMinutes(5)))
->get()
->each(function (User $user) {
$user->server_id = null;
$user->save();
});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Jobs\Server\Deprovision;

use App\Events\ServerAssignmentChanged;
use App\Models\Client;
use App\Models\Server;
use App\Models\User;
Expand Down Expand Up @@ -30,7 +29,6 @@ public function handle(): void

$connectedClients->each(function (Client $client) {
$foundNewServer = $client->user->assignServerToUser();
ServerAssignmentChanged::dispatch($client->user, !$foundNewServer);
});
}
}
8 changes: 1 addition & 7 deletions app/Jobs/ServerAssignmentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ public function handle(): void
{
// Get all users waiting for provisioning
$users = User::where('is_provisioning',true)->get();
$users->each(function($user) {
$server = $user->assignServerToUser();
// If server could be assigned, send broadcast to user
if ($server === true) {
event(new ServerAssignmentChanged($user));
}
});
$users->each(fn($user) => $user->assignServerToUser());
}
}
1 change: 0 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class User extends Authenticatable implements FilamentUser
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];

Expand Down
33 changes: 33 additions & 0 deletions app/Observers/UserObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Observers;

use App\Events\ServerAssignmentChanged;
use App\Models\User;

class UserObserver
{
public function created(User $user): void
{

}

public function updated(User $user): void
{
if($user->isDirty('server_id')) {
ServerAssignmentChanged::dispatch($user, is_null($user->server_id));
}
}

public function deleted(User $user): void
{
}

public function restored(User $user): void
{
}

public function forceDeleted(User $user): void
{
}
}
3 changes: 3 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use App\Models\User;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -31,5 +32,7 @@ public function boot(): void
->withToken(Session::get('access_token'))
->baseUrl(config('services.attsrv.url'));
});

//User::observe(\App\Observers\UserObserver::class);
}
}
4 changes: 4 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
'token' => env('HETZNER_TOKEN')
],

'signage' => [
'streamkey' => env('SIGNAGE_STREAMKEY'),
],

// This is the URL of the origin server, where a low res version is being pushed to via rtmp.
'forward' => [
'url' => env('RTMP_FORWARD')
Expand Down
4 changes: 2 additions & 2 deletions resources/js/Pages/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ export default {
});
let userid = this.$page.props.auth.user.id;
this.notifyDeviceChange = Echo.private(`User.${userid}.StreamUrl`)
Echo.private(`User.${userid}.StreamUrl`)
.listen('.server.assignment.changed', (e) => {
this.streamUrls = e.streamUrls;
this.clientId = e.clientId;
this.provisioning = e.provisioning;
if (this.notifyDeviceChange !== null) {
this.notifyDeviceChange().unsubscribe()
this.notifyDeviceChange.unsubscribe()
}
this.startNotifyDeviceChange();
Expand Down

0 comments on commit de5132b

Please sign in to comment.