Scheduled Job
The Scheduled Job DocType in TechMaju allows developers to automate tasks that can be performed at a specific time or on a recurring schedule. This helps in automating routine tasks, improving efficiency, and ensuring that important operations are performed consistently and on time.
Understanding the Scheduled Job DocType
Purpose: The Scheduled Job DocType is designed to automate various tasks within the TechMaju platform. It allows developers to define jobs that run at specified intervals, ensuring that tasks are executed automatically without manual intervention.
Function: Scheduled Jobs can automate a wide range of tasks, such as:
Automatically generating and distributing reports.
Automatically generating and scheduling entities of records, such as incidents, change items, or configuration items from a template.
Running scheduled jobs from scripts or business rules.
Scheduling tasks at the end of the month.
Scheduling tasks for weekdays.
Check the Scheduled Job Log to see the state of Scheduled Jobs. Scheduled Job states include:
Scheduled: Job is ready to run at the next scheduled interval.
Complete: Job ran successfully.
Failed: An error occurred while running the job.
Role Required
TM Core Developer: Can create, write, and delete Scheduled Jobs.
Scheduled Job Fields
Field | Description |
|---|---|
Name | The name of the Scheduled Job. |
Application | The application where the job will be used. |
Event Frequency | The frequency at which the job will run:
|
Active | A checkbox to activate or deactivate the job. |
Cron Format | The cron schedule format for defining complex schedules (only applicable if Event Frequency is Cron). |
Custom Frequency | The custom frequency for defining schedules:
|
Time | The time at which the job should run (only applicable if Event Frequency is Custom). |
Script | The script or logic that will be executed when the job runs. |
Cron Schedule Explanation
A Cron schedule is a simple yet powerful way to define the timing for recurring tasks. It uses a specific syntax to represent time intervals. The format consists of five fields:
Minute (0-59)
Hour (0-23)
Day of Month (1-31)
Month (1-12)
Day of Week (0-6, where 0 is Sunday)
Cron Schedule Syntax
* * * * *
| | | | |
| | | | +-- Day of week (0 - 6) (0 is Sunday)
| | | +---- Month (1 - 12)
| | +------ Day of month (1 - 31)
| +-------- Hour (0 - 23)
+---------- Minute (0 - 59)Special Characters
*(Asterisk): Represents any value./(Slash): Used to specify step values.
Example Cron Expressions Without Step Values
0 0 * * *: Runs every day at midnight.0 12 * * *: Runs every day at noon.0 0 1 * *: Runs on the first day of every month at midnight.0 0 * * 0: Runs every Sunday at midnight.
Step Value Explanation
The / character in a Cron schedule represents a step value, which allows you to define intervals within the specified time range for each of the 5 fields.
Step Value Syntax
<start-time>/<interval>This means "starting at start-time, execute every interval units."
Step Value Examples
Every 5 minutes:
*/5 * * * *This means "every 5 minutes starting from minute 0".
Every 2 hours:
0 */2 * * *This means "at minute 0 past every 2nd hour of every day".
Every 3 days at midnight:
0 0 */3 * *This means "at midnight every 3rd day of the month".
Breakdown of a More Complex Example
Let's break down a more complex example:
15 0-23/2 * * 1-515: At minute 150-23/2: Every 2 hours between 0 and 23 (i.e., 0, 2, 4, ..., 22)*: Every day of the month*: Every month1-5: On weekdays (Monday to Friday)
So, this Cron expression means "at minute 15 past every 2nd hour from 0 through 22 on every weekday."
Procedure
Navigate to Develop > Scheduled Job.
Adding a New Scheduled Job:
Click on '+ Add Scheduled Job' to open the creation form.
Fill in the required fields: Name, Application, and Event Frequency.
Check the 'Active' checkbox if you want the job to be active immediately.
For 'Cron' Event Frequency, specify the cron schedule using the 'Cron Format' field.
For 'Custom' Event Frequency, select the 'Custom Frequency' (Daily, Weekly, Monthly, Yearly) and the 'Time' when the job should run.
Enter the script or logic that will be executed when the job runs in the 'Script' field.
Configuring the Scheduled Job:
Adjust the script, event frequency, and schedule as needed to fit the specific requirements of the task.
Use the 'Expand' option to write complex scripts if necessary.
Saving and Viewing the Scheduled Job:
Click on 'Apply' to save the Scheduled Job.
The job will be executed according to the specified schedule and conditions.
Check the Scheduled Job Log to see the state of Scheduled Jobs.
Best Practices
Test the Scheduled Jobs in a development environment before applying them to production.
Use Scheduled Jobs to automate routine tasks, ensuring they are performed consistently and on time.
Regularly update and review Scheduled Jobs to ensure they remain effective and compatible with new versions of the TechMaju platform.
Document the purpose and functionality of each Scheduled Job within the platform to aid future maintenance.