Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d7313e

Browse files
authored
Merge pull request magento#857 from magento/revert-854-jc_remove-magento2-samples-links
Revert "Remove references to magento2-samples repo"
2 parents abe348e + 5d608fe commit 3d7313e

File tree

11 files changed

+276
-1
lines changed

11 files changed

+276
-1
lines changed
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
---
2+
layout: default
3+
group: config-guide
4+
subgroup: 12_cron
5+
title: Configure a custom cron job and cron group (tutorial)
6+
menu_title: Configure a custom cron job and cron group (tutorial)
7+
menu_order: 3
8+
menu_node:
9+
version: 2.0
10+
github_link: config-guide/cron/custom-cron-tut.md
11+
---
12+
13+
This tutorial shows you step-by-step how to create a custom cron job and optionally a cron group in a sample {% glossarytooltip c1e4242b-1f1a-44c3-9d72-1d5b1435e142 %}module{% endglossarytooltip %}. You can use a module you already have or you can use a sample module from our [`magento2-samples` repository](https://github.com/magento/magento2-samples){:target="_blank"}.
14+
15+
Running the cron job results in a row being added to the `cron_schedule` table with the name of the cron job, `custom_cron`.
16+
17+
We also show you how to optionally create a cron group, which you can use to run custom cron jobs with settings other than Magento application defaults.
18+
19+
In this tutorial, we assume the following:
20+
21+
* The Magento application is installed in `/var/www/html/magento2`
22+
* Your Magento database user name and password are both `magento`
23+
* You perform all actions as the [Magento file system owner]({{ page.baseurl }}install-gde/prereq/file-sys-perms-over.html)
24+
25+
## Step 1: Get a sample module {#cron-tut-get}
26+
To set up a custom cron job, you need a sample module. We suggest the `magento-module-minimal` module.
27+
28+
If you already have a sample module, you can use it; skip this step and the next step and continue with [Step 3: Create a class to run cron](#cron-tut-class).
29+
30+
{% collapsible To get a sample module: %}
31+
32+
1. Log in to your Magento server as, or switch to, the [Magento file system owner]({{ page.baseurl }}install-gde/prereq/file-sys-perms-over.html).
33+
2. Change to a directory that is not in your Magento application root (for example, your home directory).
34+
2. Clone the [`magento2-samples` repository](https://github.com/magento/magento2-samples){:target="_blank"}.
35+
36+
For example,
37+
38+
cd ~
39+
git clone [email protected]:magento/magento2-samples.git
40+
41+
If the command fails with the error `Permission denied (publickey).`, you must [add your SSH public key to github.com](https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account){:target="_blank"}.
42+
4. Make a directory to which to copy the sample code:
43+
44+
mkdir -p /var/www/html/magento2/app/code/Magento/SampleMinimal
45+
5. Copy the sample module code:
46+
47+
cp -r ~/magento2-samples/sample-module-minimal/* /var/www/html/magento2/app/code/Magento/SampleMinimal
48+
5. Verify the files copied properly:
49+
50+
ls -al /var/www/html/magento2/app/code/Magento/SampleMinimal
51+
52+
You should see the following result:
53+
54+
drwxrwsr-x. 4 magento_user apache 4096 Oct 30 13:19 .
55+
drwxrwsr-x. 121 magento_user apache 4096 Oct 30 13:19 ..
56+
-rw-rw-r--. 1 magento_user apache 372 Oct 30 13:19 composer.json
57+
drwxrwsr-x. 2 magento_user apache 4096 Oct 30 13:19 etc
58+
-rw-rw-r--. 1 magento_user apache 10376 Oct 30 13:19 LICENSE_AFL.txt
59+
-rw-rw-r--. 1 magento_user apache 10364 Oct 30 13:19 LICENSE.txt
60+
-rw-rw-r--. 1 magento_user apache 1157 Oct 30 13:19 README.md
61+
-rw-rw-r--. 1 magento_user apache 270 Oct 30 13:19 registration.php
62+
drwxrwsr-x. 3 magento_user apache 4096 Oct 30 13:19 Test
63+
6. Update the Magento database and schema:
64+
65+
php /var/www/html/magento2/bin/magento setup:upgrade
66+
67+
{% endcollapsible %}
68+
69+
## Step 2: Verify the sample module {#cron-tut-verify}
70+
Before you continue, make sure the sample module is registered and enabled.
71+
72+
{% collapsible To verify the sample module: %}
73+
74+
1. Log in to the Magento Admin as an administrator.
75+
2. Click **Stores** > **Configuration** > ADVANCED > **Advanced**.
76+
3. In the right pane, under Disable Modules Output, look for **Magento_SampleMinimal** as the following figure shows.
77+
78+
![Verify your sample module]({{ site.baseurl }}common/images/config_module-enabled.png){:width="900px"}
79+
80+
If the module doesn't display, review [step 1](#cron-tut-get) carefully. Make sure your code is in the correct directory. Spelling and case are important; if anything is different, the module won't load. Also, don't forget to run `magento setup:upgrade`.
81+
82+
{% endcollapsible %}
83+
84+
## Step 3: Create a class to run cron {#cron-tut-class}
85+
This step shows a simple class to create a cron job. The class only writes a row to the `cron_schedule` table that confirms it's set up successfully.
86+
87+
{% collapsible To create a class: %}
88+
89+
1. Create a directory for the class and change to that directory:
90+
91+
mkdir /var/www/html/magento2/app/code/Magento/SampleMinimal/Cron && cd /var/www/html/magento2/app/code/Magento/SampleMinimal/Cron
92+
2. Created a file named `Test.php` in that directory with the following contents:
93+
94+
{% highlight php %}
95+
<?php
96+
namespace Magento\SampleMinimal\Cron;
97+
use \Psr\Log\LoggerInterface;
98+
99+
class Test {
100+
protected $logger;
101+
102+
public function __construct(LoggerInterface $logger) {
103+
$this->logger = $logger;
104+
}
105+
106+
/**
107+
* Write to system.log
108+
*
109+
* @return void
110+
*/
111+
112+
public function execute() {
113+
$this->logger->info('Cron Works');
114+
}
115+
116+
}
117+
{% endhighlight %}
118+
119+
<!-- ?> -->
120+
121+
{% endcollapsible %}
122+
123+
## Step 4: Create `crontab.xml` {#cron-tut-crontab}
124+
`crontab.xml` sets a schedule to run your custom cron code.
125+
126+
{% collapsible To create crontab.xml: %}
127+
128+
Create `crontab.xml` as follows in the `/var/www/html/magento2/app/code/Magento/SampleMinimal/etc` directory:
129+
130+
{% highlight xml %}
131+
<?xml version="1.0"?>
132+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
133+
<group id="default">
134+
<job name="custom_cronjob" instance="Magento\SampleMinimal\Cron\Test" method="execute">
135+
<schedule>* * * * *</schedule>
136+
</job>
137+
</group>
138+
</config>
139+
{% endhighlight %}
140+
141+
The preceding `crontab.xml` runs the `Magento/SampleMinimal/Cron/Test.php` class once per minute, resulting in a row being added to the `cron_schedule` table.
142+
143+
{% endcollapsible %}
144+
145+
## Step 5: Verify the cron job {#cron-tut-cronver}
146+
This step shows how to verify the custom cron job successfully using a SQL query on the `cron_schedule` database table.
147+
148+
{% collapsible To verify cron: %}
149+
150+
1. Run Magento cron jobs:
151+
152+
php /var/www/html/magento2/bin/magento cron:run
153+
2. Enter the `magento cron:run` command two or three times.
154+
155+
The first time you enter the command, it queues jobs; subsequently, the cron jobs are run. You must enter the command _at least_ twice.
156+
2. Run the SQL query `SELECT * from cron_schedule WHERE job_code like '%custom%'` as follows:
157+
158+
1. Enter `mysql -u magento -p`
159+
2. At the `mysql>` prompt, enter `use magento;`
160+
3. Enter `SELECT * from cron_schedule WHERE job_code like '%custom%';`
161+
162+
The result should be similar to the following:
163+
164+
+-------------+----------------+---------+----------+---------------------+---------------------+---------------------+---------------------+
165+
| schedule_id | job_code | status | messages | created_at | scheduled_at | executed_at | finished_at |
166+
+-------------+----------------+---------+----------+---------------------+---------------------+---------------------+---------------------+
167+
| 3670 | custom_cronjob | success | NULL | 2016-11-02 09:38:03 | 2016-11-02 09:38:00 | 2016-11-02 09:39:03 | 2016-11-02 09:39:03 |
168+
| 3715 | custom_cronjob | success | NULL | 2016-11-02 09:53:03 | 2016-11-02 09:53:00 | 2016-11-02 09:54:04 | 2016-11-02 09:54:04 |
169+
| 3758 | custom_cronjob | success | NULL | 2016-11-02 10:09:03 | 2016-11-02 10:09:00 | 2016-11-02 10:10:03 | 2016-11-02 10:10:03 |
170+
| 3797 | custom_cronjob | success | NULL | 2016-11-02 10:24:03 | 2016-11-02 10:24:00 | 2016-11-02 10:25:03 | 2016-11-02 10:25:03 |
171+
+-------------+----------------+---------+----------+---------------------+---------------------+---------------------+---------------------+
172+
173+
3. (Optional) Verify messages are written to Magento's system log:
174+
175+
cat /var/www/html/magento2/var/log/system.log
176+
177+
You should see one or more entries like the following:
178+
179+
[2016-11-02 22:17:03] main.INFO: Cron Works [] []
180+
181+
These messages come from the `execute` method in `Test.php`:
182+
183+
public function execute() {
184+
$this->logger->info('Cron Works');
185+
186+
If the SQL command and system log contain no entries, run the `magento cron:run` command a few more times and wait. It can take some time for the database to update.
187+
188+
{% endcollapsible %}
189+
190+
## Step 6 (optional): Set up a custom cron group
191+
This step shows how to optionally set up a custom cron group. You should set up a custom cron group you want your custom cron job to run on a different schedule than other cron jobs (typically, once per minute) or if you want several custom cron jobs to run with different settings.
192+
193+
{% collapsible To set up a custom cron group: %}
194+
195+
1. Open `crontab.xml` in a text editor.
196+
2. Change `<group id="default">` to `<group id="custom_crongroup">`
197+
3. Exit the text editor.
198+
4. Create `/var/www/html/magento2/app/code/Magento/SampleMinimal/etc/cron_groups.xml` with the following contents:
199+
200+
{% highlight xml %}
201+
<?xml version="1.0"?>
202+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd">
203+
<group id="custom_crongroup">
204+
<schedule_generate_every>1</schedule_generate_every>
205+
<schedule_ahead_for>4</schedule_ahead_for>
206+
<schedule_lifetime>2</schedule_lifetime>
207+
<history_cleanup_every>10</history_cleanup_every>
208+
<history_success_lifetime>60</history_success_lifetime>
209+
<history_failure_lifetime>600</history_failure_lifetime>
210+
</group>
211+
</config>
212+
{% endhighlight %}
213+
214+
For a description of what the options mean, see [Configure custom cron jobs and cron groups reference]({{ page.baseurl }}config-guide/cron/custom-cron-ref.html).
215+
216+
{% endcollapsible %}
217+
218+
## Step 7 (optional): Verify your custom cron group
219+
This step shows how to verify your custom cron group using the {% glossarytooltip 18b930cf-09cc-47c9-a5e5-905f86c43f81 %}Magento Admin{% endglossarytooltip %}.
220+
221+
{% collapsible To verify your custom cron group: %}
222+
223+
1. Run Magento cron jobs for your custom group:
224+
225+
php /var/www/html/magento2/bin/magento cron:run --group="custom_crongroup"
226+
227+
Run the command at least twice.
228+
2. Clean the Magento cache:
229+
230+
php /var/www/html/magento2/bin/magento cache:clean
231+
2. Log in to the Magento Admin as an administrator.
232+
3. Click **Stores** > **Configuration** > **Advanced** > **System**.
233+
4. In the right pane, expand **Cron**.
234+
235+
Your cron group displays as follows:
236+
237+
![Your custom cron group]({{ site.baseurl }}common/images/config_cron-group.png)
238+
239+
240+
241+
{% endcollapsible %}
242+

guides/v2.0/extension-dev-guide/build/create_component.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ where:
9999
<p>Magento does not currently support the <a href="https://getcomposer.org/doc/05-repositories.md#path" target="_blank"><code>path</code></a> repository.</p>
100100
</div>
101101

102+
<!-- <div class="bs-callout bs-callout-info" id="info">
103+
<p>Take a look at a <a href="https://github.com/magento/magento2-samples/tree/master/sample-module-minimal"> sample module</a> created by the Magento Core Team. </p>
104+
<p>The team is creating a <a href="https://github.com/magento/magento2-samples"> collection of samples</a> to demonstrate technologies introduced in Magento 2. You can edit your Magento 2 <code>composer.json</code> file to declare a dependency upon this package of sample modules, and then run <code>composer update</code> to download them. Look for more sample modules as we build them.</p>
105+
</div> -->
106+
107+
108+
102109
#### Next
103110

104111
[Component load order]({{page.baseurl}}extension-dev-guide/build/module-load-order.html)

guides/v2.0/extension-dev-guide/cli-cmds/cli-howto.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Before you begin, make sure you understand the following:
2929
* <a href="#cli-autoload">Add CLI commands using the Composer autoloader</a>
3030

3131
<h2 id="cli-sample">Add CLI commands using dependency injection</h2>
32+
The Magento 2 sample modules provide a demonstration of many programming techniques, including adding a CLI command using {% glossarytooltip 2be50595-c5c7-4b9d-911c-3bf2cd3f7beb %}dependency injection{% endglossarytooltip %}. Look at the <a href="https://github.com/magento/magento2-samples/tree/master/sample-module-command" target="_blank">`sample-module-command`</a> for an example. The module's <a href="https://github.com/magento/magento2-samples/blob/master/sample-module-command/README.md" target="_blank">README.md</a> discusses how to install it.
3233

3334
Following is a summary of the process:
3435

@@ -41,6 +42,9 @@ Following is a summary of the process:
4142
cd <your Magento install dir>/var
4243
rm -rf cache/* page_cache/* di/* generation/*
4344

45+
<h2 id="cli-autoload">Add CLI commands using the Composer autoloader</h2>
46+
To be added at a later time.
47+
4448
#### Related topic
4549
<a href="{{page.baseurl}}extension-dev-guide/cli-cmds/cli-naming-guidelines.html">Command naming guidelines</a>
4650

guides/v2.0/extension-dev-guide/extension_attributes/adding-attributes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,4 @@ In second one:
209209
</product>
210210
{% endhighlight %}
211211

212+
<a href="https://github.com/magento-south/magento2-samples/tree/MAGETWO-55017/sample-external-links">Sample module on github</a>

guides/v2.0/extension-dev-guide/intro/developers_roadmap.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ Key points:
3535
#### Related topics
3636
* <a href="{{page.baseurl}}extension-dev-guide/intro/intro-composer.html">Introduction to Composer</a>
3737
* <a href="{{page.baseurl}}extension-dev-guide/intro/intro-composer-gloss.html">Glossary of common terms</a>
38+
* <a href="https://github.com/magento/magento2-samples" target="_blank">Sample extensions</a> created by the Magento 2 Core team
3839
* <a href="http://magento.com/developers/magento2" target="_blank">Magento 2 Developers Hub</a>

guides/v2.0/frontend-dev-guide/css-topics/custom_preprocess.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ github_link: frontend-dev-guide/css-topics/custom_preprocess.md
1212

1313
This topic describes how to add a custom {% glossarytooltip 6c5cb4e9-9197-46f2-ba79-6147d9bfe66d %}CSS{% endglossarytooltip %} preprocessor. Adding [Sass](http://sass-lang.com/) support is used as an example.
1414

15+
## Sample Sass module
16+
17+
Magento has a sample [module-sample-scss](https://github.com/magento/magento2-samples/tree/master/module-sample-scss) {% glossarytooltip c1e4242b-1f1a-44c3-9d72-1d5b1435e142 %}module{% endglossarytooltip %} implementing the {% glossarytooltip 45f1f76d-91cd-4789-a8b5-1e3f321a6280 %}Sass{% endglossarytooltip %} preprocessor.
18+
19+
You can view it as example when adding your custom preprocessor. Or install the module as is if you need to add Sass preprocessing. Installing a module is described in the [repository's Readme file](https://github.com/magento/magento2-samples/blob/master/README.md).
20+
21+
1522
## Adding a custom preprocessor
1623

1724
### Prerequisites
@@ -25,6 +32,7 @@ For details about creating a module refer to the [Magento PHP Developer Guide]({
2532
To add a custom preprocessor, take the following steps:
2633

2734
1. In your module directory, add the {% glossarytooltip edb42858-1ff8-41f9-80a6-edf0d86d7e10 %}adapter{% endglossarytooltip %} {% glossarytooltip bf703ab1-ca4b-48f9-b2b7-16a81fd46e02 %}PHP{% endglossarytooltip %} class. It must implement the `Magento\Framework\View\Asset\ContentProcessorInterface` interface.
35+
For illustration, see the adapter for Sass in the sample module: [module-sample-scss/Preprocessor/Adapter/Scss/Processor.php](https://github.com/magento/magento2-samples/blob/master/module-sample-scss/Preprocessor/Adapter/Scss/Processor.php)
2836

2937
2. If the browser compilation is possible for your file types, that is, if the corresponding {% glossarytooltip 312b4baf-15f7-4968-944e-c814d53de218 %}JavaScript{% endglossarytooltip %} {% glossarytooltip 08968dbb-2eeb-45c7-ae95-ffca228a7575 %}library{% endglossarytooltip %} exists, create the custom renderer for the client-side compilation. This will allow the default [client-side compilation functionality]({{page.baseurl}}frontend-dev-guide/css-topics/css-preprocess.html#client-side) to be applied for your files type as well.
3038
You can use the default Magento renderer for reference: [app/code/Magento/Developer/Model/View/Page/Config/ClientSideLessCompilation/Renderer.php]({{site.mage2000url}}app/code/Magento/Developer/Model/View/Page/Config/ClientSideLessCompilation/Renderer.php)

guides/v2.0/frontend-dev-guide/themes/admin_theme_apply.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ This topic describes how to apply your custom {% glossarytooltip d2093e4a-2b71-4
2525
</module>
2626
{%endhighlight%}
2727

28+
<div class="bs-callout bs-callout-info" id="info">
29+
<p>If you choose to create a separate dedicated module, you can use the <a href="https://github.com/magento/magento2-samples/tree/master/sample-module-minimal">Magento_SampleMinimal module from the Magento 2 sample modules repository</a> as example of a minimal module you need. If you will copy and use Magento_SampleMinimal, do not forget to enter your vendor and module naming, instead the ones used in the sample, in the <code>&lt;your_module_dir&gt;/etc/module.xml</code>, <code>&lt;your_module_dir>/registration.php</code>, and <code>&lt;your_module_dir>/composer.json</code> files .</p>
30+
<p>If you decide to use the existing module, keep in mind, that theme declaring might be affected when the module is changed.</p>
31+
</div>
32+
2833
## Apply a custom theme in Admin: Overview
2934

3035
To apply the {% glossarytooltip 29ddb393-ca22-4df9-a8d4-0024d75739b1 %}Admin{% endglossarytooltip %} theme, take the following steps:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../v2.0/config-guide/cron/custom-cron-tut.md

guides/v2.1/payments-integrations/base-integration/integration-intro.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ You can create integration with other payment providers, using [Magento payment
2121
The topics of this chapter explain how to add an integration with a custom payment service provider (in other words, add a new payment method) and implement the authorize payment action for this {% glossarytooltip 422b0fa8-b181-4c7c-93a2-c553abb34efd %}payment method{% endglossarytooltip %}. For illustration we use code
2222
samples from the [Braintree]({{site.mage2100url}}app/code/Magento/Braintree) payment integration.
2323

24+
To simplify the development of a new payment integration, Magento developed the [Payment sample module](https://github.com/magento/magento2-samples/tree/master/sample-module-payment-gateway).
25+
It contains all required infrastructure and you can use it as starting point.
26+
2427
To add a new payment method, take the following high-level steps:
2528

2629
1. Configure general payment method module options. Described in the [Payment method module configuration]({{page.baseurl}}payments-integrations/base-integration/module-configuration.html) topic.
@@ -49,4 +52,4 @@ Payment additional information
4952
Array of data where you can store any payment-related information
5053
</td>
5154
</tr>
52-
</table>
55+
</table>

guides/v2.1/payments-integrations/base-integration/module-configuration.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ github_link: payments-integrations/base-integration/module-configuration.md
1111

1212
For the sake of compatibility, upgradability and easy maintenance, do not edit the default Magento code; add your customizations in a separate {% glossarytooltip c1e4242b-1f1a-44c3-9d72-1d5b1435e142 %}module{% endglossarytooltip %}.
1313

14+
You can use the [sample Magento_SamplePaymentGateway module](https://github.com/magento/magento2-samples/tree/master/sample-module-payment-gateway) files as basis for your custom module structure and files.
15+
1416
## Specify your module dependencies
1517

1618
Your custom payment integration module must have at least the following dependencies:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../v2.1/config-guide/cron/custom-cron-tut.md

0 commit comments

Comments
 (0)