Skip to content

Commit b6b8a6f

Browse files
committed
Scheduled jobs
1 parent fd99eeb commit b6b8a6f

File tree

2 files changed

+100
-18
lines changed

2 files changed

+100
-18
lines changed

1. Magento Architecture and Customization Techniques/5. Demonstrate ability to use plugins.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ The `sortOrder` property controls how your plugin interacts with other plugins o
3939
>
4040
> -- [Magento DevDocs - Plugins](https://devdocs.magento.com/guides/v2.2/extension-dev-guide/plugins.html#prioritizing-plugins)
4141
42+
Plugin sortOrder:
43+
44+
- before sortOrder=10, before sortOrder=20, before sortOrder=30 ...
45+
- before and around (first half) called together for same plugin!
46+
- around (second half) and after called together for same plugin!
47+
48+
Example:
49+
50+
- pluginA.beforeMethod, pluginA.aroundMethod first half
51+
- pluginB.beforeMethod, pluginB.aroundMethod first half
52+
- pluginC.beforeMethod, `____________________
53+
- ____________________, pluginD.aroundMethod first half
54+
- method()
55+
- ____________________, pluginD.aroundMethod second half
56+
- pluginC.afterMethod , ________________________________
57+
- pluginB.aroundMethod second half, pluginB.afterMethod
58+
- pluginA.aroundMethod second half, pluginA.afterMethod
59+
4260
### Identify strengths and weaknesses of plugins.
4361

4462
> The greatest weakness is exploited in the hands of a developer who is either not

1. Magento Architecture and Customization Techniques/6. Configure event observers and scheduled jobs.md

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,86 @@
11
# Configure event observers and scheduled jobs
22

3+
## Observers
4+
5+
Events are dispatched by modules when certain actions are triggered.
6+
In addition to its own events, Magento allows you to create your own events that can be dispatched in your code.
7+
When an event is dispatched, it can pass data to any observers configured to watch that event.
8+
9+
Best practices:
10+
- Make your observer efficient
11+
- Do not include business logic
12+
- Declare observer in the appropriate scope
13+
- Avoid cyclical event loops
14+
- Do not rely on invocation order
15+
16+
>
17+
> `14. Events`
18+
>
19+
> 14.1. All values (including objects) passed to an event MUST NOT be modified in the event observer. Instead, plugins SHOULD BE used for modifying the input or output of a function.
20+
>
21+
> -- [Magento DevDocs - Technical guidelines](https://devdocs.magento.com/guides/v2.2/coding-standards/technical-guidelines.html)
22+
23+
324
### Demonstrate how to configure observers
425
- can define observer in global area, then disable in specific area
526

6-
Observer sortOrder:
27+
Observers can be configured in the `events.xml` file.
28+
29+
Properties:
30+
- name (required) - Name of the observer for the event definition
31+
- instance (required) - Class name of the observer
32+
- disabled - Is observer active or not (Default: false)
33+
- shared - Class lifestyle (Default: false)
34+
35+
Example:
36+
```xml
37+
<?xml version="1.0"?>
38+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
39+
<event name="cms_block_save_before">
40+
<observer name="myObserverName" instance="MyCompany\MyModule\Observer\MyObserver" />
41+
</event>
42+
<event name="cms_block_save_commit_after">
43+
<observer name="myObserverName2" instance="MyCompany\MyModule\Observer\AnotherObserver" disabled="true" shared="true"/>
44+
</event>
45+
</config>
46+
```
47+
48+
Observer class should be placed in the <module-root>/Observer directory and implement
49+
[Magento\Framework\Event\ObserverInterface](https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/Event/ObserverInterface.php)
50+
interface.
51+
52+
```php
53+
<?php
54+
55+
class MyObserver implements \Magento\Framework\Event\ObserverInterface
56+
{
57+
public function execute(\Magento\Framework\Event\Observer $observer)
58+
{
59+
$order = $observer->getEvent()->getData('order'); // $observer->getEvent()->getOrder();
60+
// observer code...
61+
}
62+
}
63+
```
64+
65+
### Dispatching events
766

8-
- before sortOrder=10, before sortOrder=20, before sortOrder=30 ...
9-
- before and around (first half) called together for same plugin!
10-
- around (second half) and after called together for same plugin!
67+
Events dispatch by [Magento\Framework\Event\Manager](https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/Event/Manager.php) class
68+
that implement [Magento\Framework\Event\ManagerInterface](https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/Event/ManagerInterface.php) interface:
69+
> dispatch($eventName, array $data = []);
1170
1271
Example:
72+
> $this->eventManager->dispatch('cms_page_prepare_save', ['page' => $model, 'request' => $this->getRequest()]);
1373
14-
- pluginA.beforeMethod, pluginA.aroundMethod first half
15-
- pluginB.beforeMethod, pluginB.aroundMethod first half
16-
- pluginC.beforeMethod, `____________________
17-
- ____________________, pluginD.aroundMethod first half
18-
- method()
19-
- ____________________, pluginD.aroundMethod second half
20-
- pluginC.afterMethod , ________________________________
21-
- pluginB.aroundMethod second half, pluginB.afterMethod
22-
- pluginA.aroundMethod second half, pluginA.afterMethod
74+
###### Links
75+
- [Magento DevDocs - Events and observers](https://devdocs.magento.com/guides/v2.2/extension-dev-guide/events-and-observers.html)
76+
- [Magento DevDocs - Observers Best Practices](https://devdocs.magento.com/guides/v2.2/ext-best-practices/extension-coding/observers-bp.html)
77+
78+
79+
## Scheduled jobs
2380

2481
### Demonstrate how to configure a scheduled job
2582

26-
cron_groups.xml - store view scope:
83+
`cron_groups.xml` - store view scope:
2784

2885
- default (no separate process)
2986
- index - mview, targetrule
@@ -88,10 +145,14 @@ TODO: find out
88145
what is setup:cron:run?
89146
TODO: find out
90147

148+
###### Links
149+
- [Magento DevDocs - Configure a custom cron job and cron group (tutorial)](https://devdocs.magento.com/guides/v2.2/config-guide/cron/custom-cron-tut.html)
150+
- [Magento DevDocs - Custom cron job and cron group reference](https://devdocs.magento.com/guides/v2.2/config-guide/cron/custom-cron-ref.html)
151+
91152

92153
### Identify the function and proper use of automatically available events
93154

94-
Model events \Magento\Framework\Model\AbstractModel:
155+
Model events [\Magento\Framework\Model\AbstractModel](https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/Model/AbstractModel.php):
95156

96157
- `model_load_before`, `{$_eventPrefix}_load_before`
97158
- `model_load_after`, `{$_eventPrefix}_load_after`
@@ -102,21 +163,24 @@ Model events \Magento\Framework\Model\AbstractModel:
102163
- `model_delete_after`, `{$_eventPrefix}_delete_after`
103164
- `model_delete_commit_after`, `{$_eventPrefix}_delete_commit_after`
104165

105-
Flat collection events \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection:
166+
Flat collection events [\Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection](https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/Model/ResourceModel/Db/Collection/AbstractCollection.php):
106167

107168
- `core_collection_abstract_load_before`, `{_eventPrefix}_load_before`
108169
- `core_collection_abstract_load_after`, `{_eventPrefix}_load_after`
109170

110171
only if `_eventPrefix` and `_eventObject` defined: `{prefix}_load_before`, `{prefix}_load_after`
111172

112-
EAV collection events \Magento\Eav\Model\Entity\Collection\AbstractCollection:
173+
EAV collection events [\Magento\Eav\Model\Entity\Collection\AbstractCollection](https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php):
113174

114175
- eav_collection_abstract_load_before
115176

116-
\Magento\Framework\Model\AbstractModel:
177+
[\Magento\Framework\Model\AbstractModel](https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/Model/AbstractModel.php):
117178

118179
- _eventObject = 'object'
119180
- _eventPrefix = 'core_abstract', e.g. 'catalog_category'
120181
- _getEventData() - 'data_object' + $_eventObject
121182
- `model_load_before` (object, field=null, value=ID)
122183
- `{_eventPrefix}_load_before`, e.g. `catalog_category_load_before` (object, field, value, data_object, category)
184+
185+
###### Links
186+
- [Cyrill Schumacher's Blog - List of all dispatched events](https://cyrillschumacher.com/magento-2.2-list-of-all-dispatched-events/)

0 commit comments

Comments
 (0)