infrangible / m2-task
Framework for background tasks of Infrangible modules: run history in the admin, per-task settings, log files and summary emails after each run.
Package info
github.com/infrangible-dev/m2-task
Type:magento2-module
pkg:composer/infrangible/m2-task
Requires
- php: >=8.2
- ext-filter: *
- fewedev/base: ~2.4.0
- infrangible/m2-backend-widget: ~4.0.2
- infrangible/m2-core: ~5.0.2
- infrangible/m2-foundation: ~3.0.1
- infrangible/m2-log: ~3.0.0
- infrangible/m2-simple-mail: ~2.0.0
- magento/framework: ~103.0.6
- magento/module-backend: ~102.0.0
- magento/module-config: ~101.2.0
- magento/module-cron: ~100.4.0
- magento/module-eav: ~102.1.0
- magento/module-store: ~101.1.0
- psr/log: ~2.0.0||~3.0.0
- symfony/console: ~5.4.0||~6.4.0||~7.4.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Base of background tasks of Infrangible modules, for example product feeds or sitemaps: a task runs from the admin, by cron or on the console, keeps a record of every run, writes log files and sends summaries by mail.
Runs
Infrangible Solutions > Tasks > Runs lists every run with store, task name, ID, status (running, finished, broken), success, start and finish date, duration and memory usage. A run shows its log file and its error log file.
- A run is broken if its process ended without finishing it, for example because it was killed. The cron job
check_tasksfinds these runs every minute (on servers with/proc). - The cron job
clean_tasksdeletes runs that started more than 90 days ago, with their log files. - The log files are in
var/log/task/<task name>/<store code>/<task ID>.logand.err.
Configuration
Each task has a section in the configuration tab "Tasks" (task_<task name>); "General" (task_general) holds the
values that all tasks use unless they overwrite them ("Overwrite Task General" in each group).
| Group | Fields |
|---|---|
| Settings | Max Memory (MB), Wait for Predecessor, Suppress Empty Mails, Depends On (tasks only) |
| Logging | Log Level, Log Warning as Error |
| Success Summary | Send, Sender, Recipients, Copy Recipients, Blind Copy Recipients, Subject (separated by ;) |
| Error Summary | the same fields for the errors |
- A task runs only once at the same time. Another run fails, unless "Wait for Predecessor" is enabled; then it waits.
- A task does not run while one of the tasks of "Depends On" (separated by
;) runs. - The error log and the error summary contain the errors whatever log level is set, and the warnings, if "Log Warning as Error" is enabled.
Writing a task
A task extends Infrangible\Task\Task\Base and implements four methods:
<?php declare(strict_types=1); namespace Vendor\Module\Task; use Infrangible\Task\Task\Base; class Export extends Base { private bool $emptyRun = true; protected function prepare(): void { // check the settings, open connections } protected function runTask(): bool { $this->logging->info('Exporting'); // the work; the result is the success of the run return true; } protected function dismantle(bool $success): void { // clean up, also after errors } public function isEmptyRun(): bool { return $this->emptyRun; } }
- Everything the task logs with
$this->logging(Magento's logger) goes into the log files of the run, the summaries and, on the console, the output. - Errors and exceptions of the task make the run unsuccessful; the run is always dismantled and finished.
getTaskSetting()andgetConfigValue()read the configuration of the task,isTest()tells the test mode.Infrangible\Task\Task\Fileimports the files of the settingpath(optionally matching the regular expressionfile_pattern) one by one withimportFile()and moves them intoarchive_pathorerror_pathafterwards.
Running a task
- Admin: a controller extends
Infrangible\Task\Controller\Adminhtml\Runand returns the task name, the task class and the ACL resource of the task (getTaskName(),getClassName(),getTaskResourceId()). Admin users need the resourceInfrangible_Task::infrangible_taskand the resource of the task. Parameters:store_code,test,localeandbackend_user(the locale of the admin user). Without redirect path (getRedirectPath()), the controller shows the summary. - Cron: a class extends
Infrangible\Task\Cron\Base(one task) orInfrangible\Task\Cron\TaskList(several tasks); a failed task fails the cron job with the summary.Infrangible\Task\Cron\Execution\Baseis the backend model of a configuration field with the cron expression of the task. - Console: a command extends
Infrangible\Task\Console\Command\Taskwith a script that extendsInfrangible\Task\Console\Command\Script\Task(orTaskList):
bin/magento task:<task name> --store_code=default,second --log_level=debug --console --test --id=my-run
The command fails if the task failed with one of the stores.
Requirements
- Magento Open Source or Adobe Commerce 2.4.6 or newer
- PHP 8.2 or newer
Installation
composer require infrangible/m2-task bin/magento module:enable Infrangible_Task bin/magento setup:upgrade
In production mode, also run bin/magento setup:di:compile and bin/magento setup:static-content:deploy.
License
Infrangible Task is licensed under the MIT License - see the LICENSE file for details.