| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Scheduler\AutoSchedules; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Status; |
| 6 |
use FluentCart\App\Models\ScheduledAction; |
| 7 |
use FluentCart\App\Models\Subscription; |
| 8 |
|
| 9 |
class HourlyScheduler |
| 10 |
{ |
| 11 |
private $startTimeStamp = null; |
| 12 |
|
| 13 |
public function register(): void |
| 14 |
{ |
| 15 |
add_action('fluent_cart/scheduler/hourly_tasks', [$this, 'handle'], 10); |
| 16 |
} |
| 17 |
|
| 18 |
public function handle() |
| 19 |
{ |
| 20 |
$this->startTimeStamp = time(); |
| 21 |
|
| 22 |
// hourly tasks, remove all completed tasks |
| 23 |
$this->removeCompleteTasks(); |
| 24 |
|
| 25 |
$this->checkAndExpireSubscriptions(); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
private function removeCompleteTasks() |
| 30 |
{ |
| 31 |
ScheduledAction::query()->where('status', Status::SCHEDULE_COMPLETED) |
| 32 |
->limit(5000) |
| 33 |
->delete(); |
| 34 |
} |
| 35 |
|
| 36 |
private function checkAndExpireSubscriptions() |
| 37 |
{ |
| 38 |
if ((time() - $this->startTimeStamp) > 60) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
Subscription::checkAndExpireSubscriptions(); |
| 43 |
} |
| 44 |
} |
| 45 |
|