๐Ÿ•’ 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:

bash rails db:migrate

  1. 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:

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

  4. ๐Ÿ•ฐ๏ธ 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

  5. ๐Ÿ’ป Command: The shell command to be executed.

  6. 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:

  3. โฑ๏ธ Start and end times

  4. ๐Ÿ“Š Status of execution

  5. ๐Ÿ”— 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. ๐Ÿš€