Skip to content

feat: add jobs cleanup CLI command and auto-expiry#519

Merged
chubes4 merged 1 commit intoExtra-Chill:mainfrom
Sarai-Chinwag:feat/jobs-cleanup
Mar 3, 2026
Merged

feat: add jobs cleanup CLI command and auto-expiry#519
chubes4 merged 1 commit intoExtra-Chill:mainfrom
Sarai-Chinwag:feat/jobs-cleanup

Conversation

@saraichinwag
Copy link
Contributor

Summary

  • New CLI command: wp datamachine jobs cleanup --older-than=30d --status=failed with --dry-run and --yes flags. Supports 30d, 4w, 72h duration formats.
  • Daily auto-cleanup: Action Scheduler recurring task deletes failed jobs older than 30 days (filterable via datamachine_failed_jobs_max_age_days).
  • Deactivation cleanup: Wires up datamachine_deactivate_plugin() to unschedule all datamachine-maintenance group actions on plugin deactivation.

Problem

The wp_datamachine_jobs table accumulates failed jobs indefinitely. At time of writing there were 400+ failed jobs dating back to February 3rd. No mechanism existed to expire old failures, making it harder to spot fresh problems.

Changes

File What
inc/Core/Database/Jobs/JobsOperations.php delete_old_jobs() and count_old_jobs() — age + status-based deletion with LIKE prefix matching for compound statuses
inc/Core/Database/Jobs/Jobs.php Facade methods for the new operations
inc/Cli/Commands/JobsCommand.php cleanup subcommand with duration parsing (30d/4w/72h), dry-run, and confirmation
inc/Core/ActionScheduler/JobsCleanup.php New file — daily recurring AS action (datamachine_cleanup_failed_jobs)
inc/bootstrap.php Include the new JobsCleanup file
data-machine.php Deactivation hook unschedules maintenance actions

Testing

Tested on production against live data:

$ wp datamachine jobs cleanup --dry-run --older-than=14d
Found 94 "failed" job(s) older than 14d (14 days).
Success: Dry run: 94 job(s) would be deleted.

$ wp datamachine jobs cleanup --older-than=25d --yes
Found 53 "failed" job(s) older than 25d (25 days).
Success: Deleted 53 "failed" job(s) older than 25d.

Also verified: invalid duration error handling, different status targets (completed, agent_skipped), hours format (48h).

Add age-based job cleanup to prevent stale failed jobs from accumulating
indefinitely in the wp_datamachine_jobs table.

New WP-CLI command:
  wp datamachine jobs cleanup --older-than=30d --status=failed [--dry-run] [--yes]
  Supports duration formats: 30d (days), 4w (weeks), 72h (hours)

Auto-cleanup:
  Daily Action Scheduler task deletes failed jobs older than 30 days.
  Max age is filterable via datamachine_failed_jobs_max_age_days.

Database layer:
  JobsOperations::delete_old_jobs() and count_old_jobs() use LIKE prefix
  matching to handle compound statuses (e.g. 'failed - timeout').

Also wires up the deactivation hook to unschedule all maintenance actions.
@chubes4 chubes4 merged commit 89735ac into Extra-Chill:main Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants