Skip to content

Commit 8458acb

Browse files
committed
Add cron_jobs.md.
1 parent 8d4e2af commit 8458acb

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

cron_jobs.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Cron Jobs XML Reference Documentation
2+
3+
## Introduction
4+
5+
This reference documentation provides an in-depth explanation of the cron_jobs.xml file in Magento 2. Cron jobs are
6+
scheduled tasks that are executed at predefined intervals. The cron_jobs.xml file allows you to configure and manage
7+
these cron jobs in Magento 2.
8+
9+
## File Location and Structure
10+
11+
The cron_jobs.xml file is located in the `app/code/*Vendor*/*Module*/etc` folder of your Magento installation. It
12+
follows the XML file format and defines the configuration for cron jobs.
13+
14+
Here is an example of the file structure:
15+
16+
```xml
17+
<?xml version="1.0"?>
18+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
20+
<group id="default">
21+
<job name="job_name" instance="*Vendor*\*Module*\Cron\*CronClass*" method="execute">
22+
<schedule>*schedule_expression*</schedule>
23+
</job>
24+
</group>
25+
</config>
26+
```
27+
28+
## File Components
29+
30+
The cron_jobs.xml file consists of the following components:
31+
32+
### 1. `config` Root Element
33+
34+
The `config` root element encloses the entire XML configuration. It includes the `xmlns:xsi` attribute, which specifies
35+
the XML namespace for the `xsi` prefix, and the `xsi:noNamespaceSchemaLocation` attribute, which points to the cron jobs
36+
XSD schema location.
37+
38+
### 2. `group` Element
39+
40+
The `group` element groups related cron jobs together. It has a required `id` attribute that identifies the group.
41+
42+
### 3. `job` Element
43+
44+
The `job` element defines a single cron job within a group. It has the following attributes:
45+
46+
- `name`: Specifies the unique name of the cron job.
47+
- `instance`: Specifies the fully qualified class name of the cron job's implementation class.
48+
- `method`: Specifies the method of the cron job's implementation class that should be executed.
49+
50+
### 4. `schedule` Element
51+
52+
The `schedule` element specifies the cron expression or schedule for the job. It defines when the cron job should be
53+
executed. The `*schedule_expression*` should follow the cron syntax.
54+
55+
## Example
56+
57+
Let's consider an example to illustrate the usage of the cron_jobs.xml file. Assume we have a module
58+
named `Vendor_Module` with a cron job that needs to run every hour.
59+
60+
1. Create the `cron_jobs.xml` file in the module's `etc` folder with the following content:
61+
62+
```xml
63+
<?xml version="1.0"?>
64+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
65+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
66+
<group id="default">
67+
<job name="vendor_module_cron" instance="Vendor\Module\Cron\MyCronClass" method="execute">
68+
<schedule>0 * * * *</schedule>
69+
</job>
70+
</group>
71+
</config>
72+
```
73+
74+
2. In the above example, the `group` element has an `id` attribute set to `default`. The `job` element has a `name`
75+
attribute set to `vendor_module_cron`, an `instance` attribute set to `Vendor\Module\Cron\MyCronClass`, and
76+
a `method` attribute set to `execute`. The `schedule` element specifies that the cron job should run every hour at
77+
the 0th minute.
78+
79+
3. In your PHP code, create the implementation class `MyCronClass` under `Vendor\Module\Cron`:
80+
81+
```php
82+
<?php
83+
namespace Vendor\Module\Cron;
84+
85+
class MyCronClass
86+
{
87+
public function execute()
88+
{
89+
// Perform the required cron job tasks here
90+
}
91+
}
92+
```
93+
94+
4. With the above configuration and code, the `execute()` method of the `MyCronClass` class will be executed every hour.
95+
96+
## Conclusion
97+
98+
The cron_jobs.xml file allows you to configure and manage cron jobs in Magento 2. By following the structure and syntax
99+
described in this documentation, you can define cron jobs and specify their schedules. Remember to provide the necessary
100+
implementation class and method for each cron job.
101+

0 commit comments

Comments
 (0)