diff --git a/app/Http/Controllers/SettingController.php b/app/Http/Controllers/SettingController.php index 3f0e58f..4ede46e 100644 --- a/app/Http/Controllers/SettingController.php +++ b/app/Http/Controllers/SettingController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\Jobs\WhatsappUnsoldCommodityNotification; use App\Jobs\WhatsappUnsoldCommodityScanner; +use App\JobStatus; use App\Setting; use Carbon\Carbon; use Illuminate\Http\Request; @@ -71,6 +72,7 @@ class SettingController extends Controller ->update(['value' => json_encode($value)]); } + $this->cancelPreviousNotificationJobs(); dispatch((new WhatsappUnsoldCommodityScanner())); return response()->json('success'); @@ -81,7 +83,7 @@ class SettingController extends Controller $uniqueIds = []; $filteredArray = []; - if($array !== [] ){ + if ($array !== []) { foreach ($array as $item) { if (!in_array($item['id'], $uniqueIds)) { $uniqueIds[] = $item['id']; @@ -94,4 +96,11 @@ class SettingController extends Controller $key => $filteredArray ]; } + + public function cancelPreviousNotificationJobs() + { + JobStatus::on('mysql7')->where('type', WhatsappUnsoldCommodityNotification::class)->update([ + 'status' => false + ]); + } } diff --git a/app/Jobs/WhatsappUnsoldCommodityNotification.php b/app/Jobs/WhatsappUnsoldCommodityNotification.php index 2a8763a..04fcad7 100644 --- a/app/Jobs/WhatsappUnsoldCommodityNotification.php +++ b/app/Jobs/WhatsappUnsoldCommodityNotification.php @@ -2,6 +2,7 @@ namespace App\Jobs; +use App\JobStatus; use Illuminate\Support\Facades\Http; use App\Setting; use Carbon\Carbon; @@ -56,6 +57,10 @@ class WhatsappUnsoldCommodityNotification extends Job return; } + if(JobStatus::where('job_id', $this->job->getJobId()) === false){ + return; + } + if ($this->status === '0') { return; } @@ -68,9 +73,9 @@ class WhatsappUnsoldCommodityNotification extends Job return; } - if (!$isKomoditiStillAvailable) { - return; - } + // if (!$isKomoditiStillAvailable) { + // return; + // } foreach ($phone_numbers as $phone_number) { Http::get(env('WABOT_API'), [ @@ -82,6 +87,13 @@ class WhatsappUnsoldCommodityNotification extends Job ]); } - dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(5))); + $job_id = dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::now()->addMinutes(1))); + Log::info($job_id); + + JobStatus::on('mysql7')->create([ + 'job_id' => $job_id, + 'type' => WhatsappUnsoldCommodityNotification::class, + 'status' => true, + ]); } } diff --git a/app/Jobs/WhatsappUnsoldCommodityScanner.php b/app/Jobs/WhatsappUnsoldCommodityScanner.php index d64df49..d7e184e 100644 --- a/app/Jobs/WhatsappUnsoldCommodityScanner.php +++ b/app/Jobs/WhatsappUnsoldCommodityScanner.php @@ -2,6 +2,7 @@ namespace App\Jobs; +use App\JobStatus; use App\Setting; use Carbon\Carbon; @@ -28,7 +29,7 @@ class WhatsappUnsoldCommodityScanner extends Job */ public function handle() { - $settings = Setting::on()->where('group', 'unsold_commodity_notifications')->get(); + $settings = Setting::on('mysql7')->where('group', 'unsold_commodity_notifications')->get(); $this->status = $settings->where('key', 'status')->first()->value; @@ -50,6 +51,12 @@ class WhatsappUnsoldCommodityScanner extends Job return; } - dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::parse($this->start_time))); + $job_id = dispatch((new WhatsappUnsoldCommodityNotification($this->message))->delay(Carbon::parse($this->start_time))); + + JobStatus::on('mysql7')->create([ + 'job_id' => $job_id, + 'type' => WhatsappUnsoldCommodityNotification::class, + 'status' => true, + ]); } } diff --git a/database/migrations/2024_05_30_150239_create_job_statuses.php b/database/migrations/2024_05_30_150239_create_job_statuses.php new file mode 100644 index 0000000..57da3be --- /dev/null +++ b/database/migrations/2024_05_30_150239_create_job_statuses.php @@ -0,0 +1,34 @@ +id(); + $table->string('type'); + $table->unsignedBigInteger('job_id'); + $table->boolean('status'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('job_statuses'); + } +}