๐ 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
-
Run database migrations:
bash rails db:migrate
-
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
-
Navigate to the Add Cron Job page.
-
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.
-
Click Save โ to create the job.
โ๏ธ Edit an Existing Cron Job
-
Open the Edit Cron Job page for the desired job.
-
Modify the necessary fields.
-
Click Save ๐พ to update the job.
๐ View Job Logs
-
Visit the Logs tab on a cron jobโs detail page.
-
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. ๐