*/ use HasFactory; /** * @return BelongsTo */ public function store(): BelongsTo { return $this->belongsTo(Store::class); } /** * Jobs that are waiting or running. * * @param Builder $query */ public function scopeActive(Builder $query): void { $query->whereIn('status', [GenerationStatus::Queued, GenerationStatus::Running]); } /** * Mark jobs that have been waiting or running for too long as failed. * This happens when the background worker is not running or was stopped mid-job. */ public static function failStale(int $storeId): void { static::query() ->where('store_id', $storeId) ->active() ->where('created_at', '<', now()->subMinutes((int) config('plans.stale_after_minutes', 10))) ->update([ 'status' => GenerationStatus::Failed, 'error' => 'This took too long and was stopped. Please try again.', 'finished_at' => now(), ]); } public function markFailed(string $message): void { $this->status = GenerationStatus::Failed; $this->error = mb_substr($message, 0, 500); $this->finished_at = now(); $this->save(); } /** * @return array */ protected function casts(): array { return [ 'type' => GenerationType::class, 'status' => GenerationStatus::class, 'is_regeneration' => 'boolean', 'changed' => 'boolean', 'started_at' => 'datetime', 'finished_at' => 'datetime', ]; } }