Logs job execution in custom logs

This commit is contained in:
Afiq Hamzah
2024-08-09 18:01:31 +08:00
parent 8fd70cd639
commit facf829563
5 changed files with 153 additions and 81 deletions
+3
View File
@@ -2,6 +2,7 @@
namespace App\Console; namespace App\Console;
use App\Jobs\CleanTransactionOnlineLogJob;
use Illuminate\Console\Scheduling\Schedule; use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel; use Laravel\Lumen\Console\Kernel as ConsoleKernel;
@@ -34,5 +35,7 @@ class Kernel extends ConsoleKernel
$schedule->command('gadai:daily')->daily()->timezone('Asia/Kuala_Lumpur')->at('00:00'); $schedule->command('gadai:daily')->daily()->timezone('Asia/Kuala_Lumpur')->at('00:00');
$schedule->job(new WhatsappUnsoldCommodityScanner)->daily()->timezone('Asia/Kuala_Lumpur')->at('00:00'); $schedule->job(new WhatsappUnsoldCommodityScanner)->daily()->timezone('Asia/Kuala_Lumpur')->at('00:00');
$schedule->job(new CleanTransactionOnlineLogJob)->monthly()->timezone('Asia/Kuala_Lumpur')->at('00:00');
} }
} }
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace App\Jobs;
use App\TransactionOnlineLog;
use Carbon\Carbon ;
class CleanTransactionOnlineLogJob extends Job
{
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$numberOfMonthDeletedBack = 3;
$logger = app('job_log');
$logger->info('Cleaning Transaction Online Log');
try {
TransactionOnlineLog::where('created_at', '<', Carbon::now()->subMonths($numberOfMonthDeletedBack))->get()->each(function ($log) use($logger) {
$logger->info('Deleting Transaction Online Log : ' . $log->id);
$log->delete();
});
} catch (\Exception $e) {
$logger->error($e);
throw $e;
}
}
}
@@ -31,7 +31,9 @@ class WhatsappUnsoldCommodityNotification extends Job
*/ */
public function handle() public function handle()
{ {
$logger = app('job_log');
try {
$settings = Setting::on('mysql7')->where('group', 'unsold_commodity_notifications')->get(); $settings = Setting::on('mysql7')->where('group', 'unsold_commodity_notifications')->get();
$status = $settings->where('key', 'status')->first()->value; $status = $settings->where('key', 'status')->first()->value;
$start_time = $settings->where('key', 'start_time')->first()->value; $start_time = $settings->where('key', 'start_time')->first()->value;
@@ -55,34 +57,37 @@ class WhatsappUnsoldCommodityNotification extends Job
}, $pic); }, $pic);
if ($isNotActiveJobServer) { if ($isNotActiveJobServer) {
$logger->error('Job server is not active');
return false; return false;
} }
if ($isWabotCredentialNotComplete) { if ($isWabotCredentialNotComplete) {
Log::error('Wabot credential not complete'); $logger->error('Wabot credential not complete');
return false; return false;
} }
if ($phone_numbers[0] === NULL) { if ($phone_numbers[0] === NULL) {
Log::error('No PIC phone numbers detected'); $logger->error('No PIC phone numbers detected');
return false; return false;
} }
if ($status === '0') { if ($status === '0') {
Log::info('Unsold commodity notifications are turned off, no notification will be sent'); $logger->info('Unsold commodity notifications are turned off, no notification will be sent');
return false; return false;
} }
if (Carbon::parse($start_time) > Carbon::now()) { if (Carbon::parse($start_time) > Carbon::now()) {
$logger->info('Start time is in the future');
return false; return false;
} }
if (Carbon::parse($end_time) < Carbon::now()) { if (Carbon::parse($end_time) < Carbon::now()) {
$logger->info('End time is in the past');
return false; return false;
} }
if ($isLatestKomoditiToday && $isAllKomoditiSold) { if ($isLatestKomoditiToday && $isAllKomoditiSold) {
Log::info('All today komoditi is sold'); $logger->info('All today komoditi is sold');
return false; return false;
} }
@@ -107,11 +112,15 @@ class WhatsappUnsoldCommodityNotification extends Job
$logblastwasap->updated_at = Carbon::now(); $logblastwasap->updated_at = Carbon::now();
$logblastwasap->save(); $logblastwasap->save();
Log::info("WABOT - Notifikasi Komoditi Tidak Terjual" . $response); $logger->info("WABOT - Notifikasi Komoditi Tidak Terjual" . $response);
} }
dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(15))); dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(15)));
} catch (\Exception $e) {
$logger->error($e);
throw $e;
}
} }
} }
+12 -1
View File
@@ -27,16 +27,27 @@ class WhatsappUnsoldCommodityScanner extends Job
*/ */
public function handle() public function handle()
{ {
$logger = app('job_log');
try {
$logger->info('Scanning Unsold Commodity Notifications Settings');
$settings = Setting::on('mysql7')->where('group', 'unsold_commodity_notifications')->get(); $settings = Setting::on('mysql7')->where('group', 'unsold_commodity_notifications')->get();
$status = $settings->where('key', 'status')->first()->value; $status = $settings->where('key', 'status')->first()->value;
$start_time = $settings->where('key', 'start_time')->first()->value; $start_time = $settings->where('key', 'start_time')->first()->value;
if ($status === '0') { if ($status === '0') {
$logger->info('Unsold commodity notifications are turned off, no notification will be sent');
return false; return false;
} }
dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::parse($start_time))); dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::parse($start_time)));
} catch (\Exception $e) {
$logger->error($e);
throw $e;
}
} }
} }
+8
View File
@@ -3,6 +3,8 @@
namespace App\Providers; namespace App\Providers;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
@@ -21,5 +23,11 @@ class AppServiceProvider extends ServiceProvider
//else register your services you require for production //else register your services you require for production
$this->app['request']->server->set('HTTPS', true); $this->app['request']->server->set('HTTPS', true);
} }
$this->app->singleton('job_log', function () {
$logger = new Logger('custom_log');
$logger->pushHandler(new RotatingFileHandler(storage_path('logs/jobs.log'), 0, Logger::DEBUG));
return $logger;
});
} }
} }