Skip to main content

๐Ÿ•’ Cron Job Setup Documentation

๐Ÿ“Œ Overviewโ€‹

The CM Cron Job feature allows you to manage scheduled tasks directly within the cm-admin interface. You can create, edit, enable, disable, execute, and view logs for each cron job from a user-friendly dashboard.


โœจ Featuresโ€‹

  • ๐Ÿ” Filters: Search cron jobs by:

    • Name
    • Status
    • Last run time
    • Last run status
  • ๐Ÿ“Š Sortable Columns:

    • Name
    • Updated time
    • Created time
  • โš™๏ธ Custom Actions:

    • โœ… Enable or โŒ Disable a cron job
    • โ–ถ๏ธ Manually execute a cron job
  • ๐Ÿ”Ž Detailed Views:

    • Job information: name, cron string, command, status, last run time, output
    • Access logs for each run

๐Ÿ› ๏ธ Setup Instructionsโ€‹

1. ๐Ÿ“ฆ Run the CMAdmin Scheduler Generatorโ€‹

Generate the necessary files:

rails g cm_admin:add_scheduler

2. ๐Ÿงฑ Ensure Required Models Existโ€‹

Your application must include the following models:

  • CmCronJob
  • CmCronJobLog

3. ๐Ÿ”— Integrate Sidekiqโ€‹

  1. Run database migrations:

    rails db:migrate
  2. Start Sidekiq to enable background processing.

๐Ÿ’ก Note:

  • All cm_cron jobs are removed from Sidekiq when Sidekiq is shut down.
  • When Sidekiq is started, all enabled cron jobs will be automatically re-added to the queue.

4. ๐Ÿงฉ Use Custom Actionsโ€‹

Available via the CmAdmin::CmCronJob concern:

  • ๐Ÿšซ Disable Job โ€“ Deactivates an active job.
  • ๐ŸŸข Enable Job โ€“ Activates a disabled job.
  • ๐Ÿ”„ Execute Job โ€“ Runs a job immediately.
  • ๐Ÿ—‘๏ธ Delete Job โ€“ Removes the job permanently.

โš ๏ธ Note: Newly created jobs are disabled by default. Use the Enable button to activate them.


5. ๐Ÿ“„ View Cron Job Logsโ€‹

Each jobโ€™s Logs tab includes:

  • ๐Ÿ•‘ Start and completion timestamps
  • ๐Ÿ“ˆ Execution status
  • ๐Ÿ” Links to detailed log output

๐Ÿงช Example Usageโ€‹

โž• Add a New Cron Jobโ€‹

  1. Navigate to the Add Cron Job page.

  2. Provide the following details:

    • ๐Ÿท๏ธ Name: A unique identifier for the job.

    • ๐Ÿ•ฐ๏ธ Cron String: Set the schedule using a valid cron expression. Common presets:

      • @hourly โ€“ Every hour
      • @daily โ€“ Every day
      • @weekly โ€“ Every week
      • @monthly โ€“ Every month
      • @yearly โ€“ Every year
    • ๐Ÿ’ป Command: The shell command to be executed.

  3. Click Save โœ… to create the job.


โœ๏ธ Edit an Existing Cron Jobโ€‹

  1. Open the Edit Cron Job page for the desired job.
  2. Modify the necessary fields.
  3. Click Save ๐Ÿ’พ to update the job.

๐Ÿ“œ View Job Logsโ€‹

  1. Visit the Logs tab on a cron jobโ€™s detail page.

  2. Review:

    • โฑ๏ธ Start and end times
    • ๐Ÿ“Š Status of execution
    • ๐Ÿ”— Link to output logs

โœ… Conclusionโ€‹

The CM Cron Job feature in cm-admin empowers you to manage scheduled tasks with confidence. With detailed controls, logging, and Sidekiq support, you can automate background operations efficiently and reliably. ๐Ÿš€