Background Jobs & Scheduled Workflows – Cleanup and Data Hygiene
Keeping the database lean and consistent is critical for system stability. This section covers automations that clear stale records and enforce data hygiene across the platform.
🧹 Key Cleanup Commands
Below is an at-a-glance summary of the console commands dedicated to cleanup tasks.
| Command | Signature | Description | Schedule |
|---|---|---|---|
| Expired Checkout Sessions | app:clear-expired-checkout-sessions |
Deletes checkout_data and shoppingcart records older than 24 hours. |
Daily at 01:00 |
| Packet Data Cleanup | app:clean-packet-data |
Dispatches jobs to purge or reformat stale packet_data entries for events. |
Manual / future schedule |
ClearExpiredCheckoutSessions
This command removes abandoned carts and expired Stripe checkout sessions to prevent DB bloat.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class ClearExpiredCheckoutSessions extends Command
{
protected $signature = 'app:clear-expired-checkout-sessions';
protected $description = 'Clears expired checkout sessions and cart data older than 24 hours';
public function handle(): int
{
try {
DB::beginTransaction();
$checkoutDeleted = DB::table('checkout_data')
->where('created_at', '<', now()->subHours(24))
->delete();
$cartDeleted = DB::table('shoppingcart')
->where('created_at', '<', now()->subHours(24))
->delete();
DB::commit();
$this->info("Deleted {$checkoutDeleted} checkout sessions and {$cartDeleted} cart records.");
return Command::SUCCESS;
} catch (\Exception $e) {
DB::rollBack();
$this->error("Failed to delete expired sessions: {$e->getMessage()}");
return Command::FAILURE;
}
}
}
– Located in app/Console/Commands/ClearExpiredCheckoutSessions.php
– Scheduled daily at 01:00 via the Laravel scheduler
Workflow Diagram
flowchart TD
S[Scheduler (daily 01:00)] --> C[ClearExpiredCheckoutSessions]
C -->|Begin Transaction| T1{DB::beginTransaction()}
T1 --> D1[Delete checkout_data < 24h old]
D1 --> D2[Delete shoppingcart < 24h old]
D2 -->|Success| T2[DB::commit()]
D2 -->|Error| T3[DB::rollBack()]
CleanPacketData
Legacy or oversized packet_data objects can accumulate over time. This command dispatches a background job per event to sanitize or remove stale packet data.
<?php
namespace App\Console\Commands;
use App\Jobs\CleanPacketDataJob;
use Illuminate\Console\Command;
class CleanPacketData extends Command
{
protected $signature = 'app:clean-packet-data';
protected $description = 'Dispatch cleanup jobs for stale event packet data';
public function handle()
{
$events = \App\Models\Event::whereNotNull('packet_data')->get('id');
foreach ($events as $event) {
dispatch(new CleanPacketDataJob($event->id));
}
}
}
– Located in app/Console/Commands/CleanPacketData.php
– Intended for periodic execution (e.g., via scheduler) or manual runs
Job Dispatch Flow
flowchart TD
E[Manual/Scheduler Trigger] --> C[app:clean-packet-data]
C --> L[Fetch events where packet_data ≠ null]
L --> J[dispatch CleanPacketDataJob(event_id)]
J --> K[CleanPacketDataJob handles per-event cleanup]
Scheduling & Registration
All cleanup commands must be registered in app/Console/Kernel.php or routes/console.php and linked to the scheduler:
protected function schedule(Schedule $schedule)
{
// Cleanup stale sessions & carts
$schedule->command('app:clear-expired-checkout-sessions')
->dailyAt('1:00')
->onOneServer();
// (Optional) Packet data hygiene
// $schedule->command('app:clean-packet-data')->weekly()->onOneServer();
}
By running these jobs on a single server instance (->onOneServer()), the platform avoids overlapping executions in multi-instance deployments.
Dependencies & Patterns
- Illuminate\Console\Command: Base class for all console commands.
- Illuminate\Support\Facades\DB: Raw query builder for delete operations.
- dispatch(job): Laravel helper for queueing background jobs.
- Carbon/
now(): Date calculations for purging threshold. - Scheduler Macros:
dailyAt,weekly,onOneServer,withoutOverlapping.
Best Practices
- Transaction Safety: Wrap multi-step deletes in transactions to maintain atomicity.
- One-Server Execution: Prevent concurrent cleanup runs on clustered environments.
- Monitoring: Use tools like Sentry or custom logging to track failures.
- Selective Scheduling: Align cleanup frequency with business needs—e.g., cart data every 24 h, packet data weekly.
By automating these cleanup workflows, the Prep Network Events platform maintains optimal performance, avoids unnecessary storage costs, and ensures data quality over time.