Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the new log trait for commands #211

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions app/Console/Commands/AddDemoUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console\Commands;

use App\Console\Commands\Traits\Logable;
use App\Enums\Content;
use App\Enums\Role;
use App\Models\Asset;
Expand All @@ -13,26 +14,15 @@

class AddDemoUser extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
use Logable;

protected $signature = 'app:add-demo-user';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Add Dr. Dolitle to the portal';

/**
* Execute the console command.
*/
public function handle()
public function handle(): int
{
$this->info('Adding Dr. Dolitle to users');
$this->commandLog(message: 'Adding Dr. Dolitle to users');
$user = User::create([
'username' => 'drdoli',
'first_name' => 'John',
Expand All @@ -42,22 +32,22 @@ public function handle()
'email' => '[email protected]',
]);
$user->assignRole(Role::MODERATOR);
$this->info('Adding Dr. Dolittle presenter');
$this->commandLog(message: 'Adding Dr. Dolitle to users');
$user->presenter()->create([
'academic_degree_id' => '1',
'first_name' => 'John',
'last_name' => 'Dolittle',
'username' => 'drdoli',
'email' => '[email protected]',
]);
$this->info('Adding Dr. Dolittle series');
$this->commandLog(message: 'Adding Dr. Dolittle series');
$user->series()->create([
'title' => 'The story of Dr. Dollitle',
// 'semester_id' => Semester::current()->get()->first()->id,
]);

$series = $user->series()->first();
$this->info('Adding Dr. Dolittle clips to his series');
$this->commandLog(message: 'Adding Dr. Dolittle clips to his series');
$series->clips()->create([
'episode' => 1,
'title' => 'Taby cat',
Expand All @@ -76,7 +66,7 @@ public function handle()
'has_video_assets' => 1,
'language_id' => '1',
]);
$this->info('Adding Dr. Dolittle assets to his clips');
$this->commandLog(message: 'Adding Dr. Dolittle assets to his clips');

$series->clips()->each(function ($clip) {
if ($clip->episode == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,41 @@

namespace App\Console\Commands;

use App\Console\Commands\Traits\Logable;
use App\Enums\Role;
use App\Models\Presenter;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

class AssosiateUserWithPresenterID extends Command
class AssociateUserWithPresenterID extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
use Logable;

protected $signature = 'app:link-user-with-presenter';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Links an existing user with an existing presenter';

/**
* Execute the console command.
*/
public function handle(): int
{
$this->info('Start to iterating over employees');
$this->commandLog(message: 'Start to iterating over employees');
$moderators = User::byRole(Role::MEMBER);
$bar = $this->output->createProgressBar($moderators->count());
$this->info("Found {$moderators->count()} members");
$this->commandLog(message: "Found {$moderators->count()} members");
$bar->start();
$moderators->get()->each(function ($moderator) use ($bar) {
$presenter = Presenter::where(function ($query) use ($moderator) {
$query->where('username', $moderator->username)
->orWhereRaw('LOWER(email) = ?', [Str::lower($moderator->email)]);
})->first();
$this->info("Found a presenter for user:{$moderator->getFullNameAttribute()}");
$this->commandLog(message: "Found a presenter for user:{$moderator->getFullNameAttribute()}");
if (! is_null($presenter) && is_null($moderator->presenter_id)) {
$this->newLine();
$moderator->presenter_id = $presenter->id;
$moderator->save();
$this->info("Presenter ID is set for user:{$moderator->getFullNameAttribute()}");
$this->commandLog(message: "Presenter ID is set for user:{$moderator->getFullNameAttribute()}");
} else {
$this->info('User already has a presenter ID');
$this->commandLog(message: 'User already has a presenter ID');
}
$this->newLine();
$bar->advance();
Expand Down
28 changes: 9 additions & 19 deletions app/Console/Commands/CheckAndCreateSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console\Commands;

use App\Console\Commands\Traits\Logable;
use App\Models\Setting;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
Expand All @@ -10,23 +11,12 @@

class CheckAndCreateSettings extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
use Logable;

protected $signature = 'app:check-and-create-settings';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Checks for new settings and stores them in the database';

/**
* Execute the console command.
*/
public function handle(): int
{
$settingsTypes = ['all', 'portal', 'opencast', 'streaming', 'openSearch'];
Expand All @@ -38,19 +28,19 @@ public function handle(): int
);

if ($settingToBeChecked === 'all') {
$this->info('Start migrating all settings');
$this->commandLog(message: 'Start migrating all settings');
foreach (Arr::except($settingsTypes, [0]) as $settingType) {
$this->info("Starting with {$settingType} settings");
$this->commandLog(message: "Starting with {$settingType} settings");
$this->processSettings($settingType);
$this->info("Finished with {$settingType} settings");
$this->commandLog(message: "Finished with {$settingType} settings");
}
} else {
$this->info("Starting with {$settingToBeChecked} settings");
$this->commandLog(message: "Starting with {$settingToBeChecked} settings");
$this->processSettings($settingToBeChecked);
$this->info("Finished with {$settingToBeChecked} settings");
$this->commandLog(message: "Finished with {$settingToBeChecked} settings");
}

$this->info($settingToBeChecked.'settings have been checked and created');
$this->commandLog(message: $settingToBeChecked.'settings have been checked and created');

return Command::SUCCESS;
}
Expand Down
17 changes: 2 additions & 15 deletions app/Console/Commands/CheckForExpiredUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Console\Commands;

use App\Console\Commands\traits\Logable;
use App\Console\Commands\Traits\Logable;
use App\Events\UserExpired;
use App\Mail\ExpiredUsersFound;
use App\Models\Setting;
Expand All @@ -16,24 +16,11 @@ class CheckForExpiredUsers extends Command
use Logable;
use Notifiable;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:check-for-expired-users';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Checks for expired users, fires up and event, and notifies portal admins';

/**
* Execute the console command.
*/
public function handle()
public function handle(): int
{
$this->commandLog(message: 'Check for expired users');
$expiredUsers = User::expired();
Expand Down
22 changes: 6 additions & 16 deletions app/Console/Commands/CheckLivestreams.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,30 @@

namespace App\Console\Commands;

use App\Console\Commands\Traits\Logable;
use App\Models\Livestream;
use App\Services\OpencastService;
use Illuminate\Console\Command;

class CheckLivestreams extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
use Logable;

protected $signature = 'app:check-livestreams';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Check for active livestreams and disables them';

/**
* Execute the console command.
*/
public function handle(OpencastService $opencastService)
{
if ($opencastService->getHealth()->get('status') === 'failed') {
$this->info('No Opencast server found or server is offline!');
$this->commandLog(message: 'No Opencast server found or server is offline!');

return Command::SUCCESS;
}

$activeLivestreams = Livestream::active()->get();
if ($activeLivestreams->isEmpty()) {
$this->info('No active livestreams found');
$this->commandLog(message: 'No active livestreams found');

return Command::SUCCESS;
}
Expand All @@ -46,7 +36,7 @@ public function handle(OpencastService $opencastService)
$livestream->clip_id = null;
$livestream->active = false;
$livestream->save();
$this->info("Disable livestream {$livestream->name}.");
$this->commandLog(message: "Disable livestream {$livestream->name}.");
} else {
$this->info("Livestream {$livestream->name} is still active.");
}
Expand Down
27 changes: 8 additions & 19 deletions app/Console/Commands/CheckTimeAvailabilityClips.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,33 @@

namespace App\Console\Commands;

use App\Console\Commands\Traits\Logable;
use App\Models\Clip;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class CheckTimeAvailabilityClips extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
use Logable;

protected $signature = 'app:check-time-availability-clips';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Check and toggle time availability for clips';

/**
* Execute the console command.
*/
public function handle(): int
{
$now = Carbon::now();
$clips = Clip::where('has_time_availability', true)->get();

Log::info('Starting command to check time availability for clips.');
$this->commandLog(message: 'Starting command to check time availability for clips.');

if ($clips->isEmpty()) {
$this->info("No clips with time availability found as of {$now}");
$this->commandLog(message: "No clips with time availability found as of {$now}");

return Command::SUCCESS;
}

$this->info("Processing {$clips->count()} clips with time availability.");
$this->commandLog(message: "Processing {$clips->count()} clips with time availability.");

$bar = $this->output->createProgressBar($clips->count());
$bar->start();
Expand All @@ -51,7 +40,7 @@ public function handle(): int
});

$bar->finish();
$this->info('Time availability check completed.');
$this->commandLog(message: 'Time availability check completed.');

return Command::SUCCESS;
}
Expand All @@ -77,6 +66,6 @@ private function processClip(Clip $clip, Carbon $now): void
}

$clip->save();
$this->info("ClipID: {$clip->id} / Title: {$clip->episode} {$clip->title} {$message}");
$this->commandLog(message: "ClipID: {$clip->id} / Title: {$clip->episode} {$clip->title} {$message}");
}
}
16 changes: 3 additions & 13 deletions app/Console/Commands/DeleteTempUploadedFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,19 @@

namespace App\Console\Commands;

use App\Console\Commands\Traits\Logable;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;

class DeleteTempUploadedFiles extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
use Logable;

protected $signature = 'app:delete-temp-uploaded-files';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete temporary uploaded files older than 24 hours.';

/**
* Execute the console command.
*/
public function handle(): void
{
$directories = Storage::directories('tmp');
Expand Down
Loading
Loading