ScheduledTask
for this.services.xml
file in a plugin works is also helpful, which will be taught in our guides about Dependency Injection and Creating a service. It is shortly explained here as well though, so no worries!ScheduledTask
and its respective ScheduledTaskHandler
are registered in a plugin's services.xml
. For it to be found by Shopware 6 automatically, you need to place the services.xml
file in a Resources/config/
directory, relative to the location of your plugin's base class. The path could look like this: <plugin root>/src/Resources/config/services.xml
.services.xml
containing a new ScheduledTask
as well as a new ScheduledTaskHandler
:shopware.scheduled.task
and messenger.message_handler
. Your custom task will now be saved into the database once your plugin is activated.services.xml
file tries to find both the task itself as well as the new task handler in a directory called Service/ScheduledTask
. This naming is up to you, Shopware 6 decided to use this name though.ScheduledTask
:ExampleTask
class has to extend from the Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTask
class, which will force you to implement two methods:getTaskName
: The technical name of your task. Make sure to add a vendor prefix to your custom task, to prevent collisions with other plugin's scheduled tasks. In this example this is swag
.getDefaultInterval
: The interval in seconds at which your scheduled task should be executed.ExampleTask
class.ExampleTaskHandler
as defined previously in your services.xml
, has to extend from the class Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler
. This also comes with two methods, that you need to implement first:getHandledMessages
: An iterable, or an array, of the scheduled tasks, that this handler will take care of. In this case this is only the ExampleTask
class.run
: This method is executed once your scheduled task is executed. Do everything, that your task is supposed to do here. In this example, it will just create a new file.bin/console scheduled-task:register
bin/console scheduled-task:run
. This will start the ScheduledTaskRunner
, which takes care of your scheduled tasks and their respective timings. It will dispatch a message to the message bus once your scheduled task's interval is due.bin/console messenger:consume
to actually execute the dispatched messages. Make sure, that the status
of your scheduled task is set to scheduled
in the scheduled_task
table, otherwise it won't be executed. This is not necessary, when you're using the admin worker.