Logs job execution in custom logs
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Jobs\CleanTransactionOnlineLogJob;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
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->job(new WhatsappUnsoldCommodityScanner)->daily()->timezone('Asia/Kuala_Lumpur')->at('00:00');
|
||||
|
||||
$schedule->job(new CleanTransactionOnlineLogJob)->monthly()->timezone('Asia/Kuala_Lumpur')->at('00:00');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,87 +31,96 @@ class WhatsappUnsoldCommodityNotification extends Job
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$logger = app('job_log');
|
||||
|
||||
$settings = Setting::on('mysql7')->where('group', 'unsold_commodity_notifications')->get();
|
||||
$status = $settings->where('key', 'status')->first()->value;
|
||||
$start_time = $settings->where('key', 'start_time')->first()->value;
|
||||
$end_time = $settings->where('key', 'end_time')->first()->value;
|
||||
$message = $settings->where('key', 'message')->first()->value;
|
||||
try {
|
||||
$settings = Setting::on('mysql7')->where('group', 'unsold_commodity_notifications')->get();
|
||||
$status = $settings->where('key', 'status')->first()->value;
|
||||
$start_time = $settings->where('key', 'start_time')->first()->value;
|
||||
$end_time = $settings->where('key', 'end_time')->first()->value;
|
||||
$message = $settings->where('key', 'message')->first()->value;
|
||||
|
||||
$latestKomoditi = KomuditiPurchase::on('mysql7')->latest()->first();
|
||||
$isAllKomoditiSold = $latestKomoditi->unit_komuditi === 0.0;
|
||||
$isLatestKomoditiToday = $latestKomoditi->created_at->isToday();
|
||||
$latestKomoditi = KomuditiPurchase::on('mysql7')->latest()->first();
|
||||
$isAllKomoditiSold = $latestKomoditi->unit_komuditi === 0.0;
|
||||
$isLatestKomoditiToday = $latestKomoditi->created_at->isToday();
|
||||
|
||||
$isWabotCredentialNotComplete = env('WABOT_API') === null || env('WABOT_INSTANCES_ID') === null || env('WABOT_TOKEN_KEYS') === null;
|
||||
$isNotActiveJobServer = env('ACTIVE_JOB_SERVER') !== true;
|
||||
$isWabotCredentialNotComplete = env('WABOT_API') === null || env('WABOT_INSTANCES_ID') === null || env('WABOT_TOKEN_KEYS') === null;
|
||||
$isNotActiveJobServer = env('ACTIVE_JOB_SERVER') !== true;
|
||||
|
||||
$pic_key = 'pic_' . strtolower(Carbon::now()->englishDayOfWeek);
|
||||
$pic = json_decode($settings->where('key', $pic_key)->first()->value);
|
||||
$pic_key = 'pic_' . strtolower(Carbon::now()->englishDayOfWeek);
|
||||
$pic = json_decode($settings->where('key', $pic_key)->first()->value);
|
||||
|
||||
$phone_numbers = array_map(function ($p) {
|
||||
if (isset($p->no_telefon)) {
|
||||
return $p->no_telefon;
|
||||
$phone_numbers = array_map(function ($p) {
|
||||
if (isset($p->no_telefon)) {
|
||||
return $p->no_telefon;
|
||||
}
|
||||
}, $pic);
|
||||
|
||||
if ($isNotActiveJobServer) {
|
||||
$logger->error('Job server is not active');
|
||||
return false;
|
||||
}
|
||||
}, $pic);
|
||||
|
||||
if ($isNotActiveJobServer) {
|
||||
return false;
|
||||
if ($isWabotCredentialNotComplete) {
|
||||
$logger->error('Wabot credential not complete');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($phone_numbers[0] === NULL) {
|
||||
$logger->error('No PIC phone numbers detected');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($status === '0') {
|
||||
$logger->info('Unsold commodity notifications are turned off, no notification will be sent');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Carbon::parse($start_time) > Carbon::now()) {
|
||||
$logger->info('Start time is in the future');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Carbon::parse($end_time) < Carbon::now()) {
|
||||
$logger->info('End time is in the past');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($isLatestKomoditiToday && $isAllKomoditiSold) {
|
||||
$logger->info('All today komoditi is sold');
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
foreach ($phone_numbers as $phone_number) {
|
||||
$response = Http::get(env('WABOT_API'), [
|
||||
'number' => $phone_number,
|
||||
'type' => 'text',
|
||||
'message' => $message,
|
||||
'instance_id' => env('WABOT_INSTANCES_ID'),
|
||||
'access_token' => env('WABOT_TOKEN_KEYS')
|
||||
]);
|
||||
|
||||
|
||||
//insert log blast wasap
|
||||
$logblastwasap = new BlastWasap();
|
||||
$logblastwasap->kodcaw = 'OPERASI';
|
||||
$logblastwasap->notelefon = $phone_number;
|
||||
$logblastwasap->norujukan = 'Notifikasi Komoditi Tidak Terjual. ' . 'baki : ' . KomuditiPurchase::on('mysql7')->latest()->first()->unit_komuditi;
|
||||
$logblastwasap->status = $response;
|
||||
$logblastwasap->created_at = Carbon::now();
|
||||
$logblastwasap->updated_at = Carbon::now();
|
||||
$logblastwasap->save();
|
||||
|
||||
$logger->info("WABOT - Notifikasi Komoditi Tidak Terjual" . $response);
|
||||
}
|
||||
|
||||
|
||||
|
||||
dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(15)));
|
||||
} catch (\Exception $e) {
|
||||
$logger->error($e);
|
||||
throw $e;
|
||||
}
|
||||
|
||||
if ($isWabotCredentialNotComplete) {
|
||||
Log::error('Wabot credential not complete');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($phone_numbers[0] === NULL) {
|
||||
Log::error('No PIC phone numbers detected');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($status === '0') {
|
||||
Log::info('Unsold commodity notifications are turned off, no notification will be sent');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Carbon::parse($start_time) > Carbon::now()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Carbon::parse($end_time) < Carbon::now()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($isLatestKomoditiToday && $isAllKomoditiSold) {
|
||||
Log::info('All today komoditi is sold');
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
foreach ($phone_numbers as $phone_number) {
|
||||
$response = Http::get(env('WABOT_API'), [
|
||||
'number' => $phone_number,
|
||||
'type' => 'text',
|
||||
'message' => $message,
|
||||
'instance_id' => env('WABOT_INSTANCES_ID'),
|
||||
'access_token' => env('WABOT_TOKEN_KEYS')
|
||||
]);
|
||||
|
||||
|
||||
//insert log blast wasap
|
||||
$logblastwasap = new BlastWasap();
|
||||
$logblastwasap->kodcaw = 'OPERASI';
|
||||
$logblastwasap->notelefon = $phone_number;
|
||||
$logblastwasap->norujukan = 'Notifikasi Komoditi Tidak Terjual. ' . 'baki : ' . KomuditiPurchase::on('mysql7')->latest()->first()->unit_komuditi;
|
||||
$logblastwasap->status = $response;
|
||||
$logblastwasap->created_at = Carbon::now();
|
||||
$logblastwasap->updated_at = Carbon::now();
|
||||
$logblastwasap->save();
|
||||
|
||||
Log::info("WABOT - Notifikasi Komoditi Tidak Terjual" . $response);
|
||||
}
|
||||
|
||||
|
||||
|
||||
dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(15)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,16 +27,27 @@ class WhatsappUnsoldCommodityScanner extends Job
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$settings = Setting::on('mysql7')->where('group', 'unsold_commodity_notifications')->get();
|
||||
$status = $settings->where('key', 'status')->first()->value;
|
||||
$start_time = $settings->where('key', 'start_time')->first()->value;
|
||||
$logger = app('job_log');
|
||||
|
||||
try {
|
||||
|
||||
if ($status === '0') {
|
||||
return false;
|
||||
$logger->info('Scanning Unsold Commodity Notifications Settings');
|
||||
|
||||
$settings = Setting::on('mysql7')->where('group', 'unsold_commodity_notifications')->get();
|
||||
$status = $settings->where('key', 'status')->first()->value;
|
||||
$start_time = $settings->where('key', 'start_time')->first()->value;
|
||||
|
||||
if ($status === '0') {
|
||||
$logger->info('Unsold commodity notifications are turned off, no notification will be sent');
|
||||
return false;
|
||||
}
|
||||
|
||||
dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::parse($start_time)));
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$logger->error($e);
|
||||
throw $e;
|
||||
}
|
||||
|
||||
dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::parse($start_time)));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Monolog\Handler\RotatingFileHandler;
|
||||
use Monolog\Logger;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -21,5 +23,11 @@ class AppServiceProvider extends ServiceProvider
|
||||
//else register your services you require for production
|
||||
$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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user