Book Image

Magento 2 Development Cookbook

Book Image

Magento 2 Development Cookbook

Overview of this book

With the challenges of growing an online business, Magento 2 is an open source e-commerce platform with innumerable functionalities that gives you the freedom to make on-the-fly decisions. It allows you to customize multiple levels of security permissions and enhance the look and feel of your website, and thus gives you a personalized experience in promoting your business.
Table of Contents (18 chapters)
Magento 2 Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating and testing a new cronjob


In Magento 2, cronjobs are defined in the crontab.xml file of each module. Like every configuration in the Magento modules, the configuration of the cronjobs is easy to extend in custom modules. And that's what we will do in this recipe. We will create a new cronjob for our module.

Testing a cronjob is a bit tricky. You can wait while the cron will is executed, but in this recipe, we will see how we can trigger it for development purposes.

Getting ready

The workflow to create a new cronjob is mostly the same as working with event observers. We have to configure a new cronjob that will start a function in a configured class.

For the creation of a new cronjob, we will use the existing Packt_HelloWorld module. Make sure you have the latest version installed.

How to do it...

In the next steps, we will create the configuration for a new cronjob:

  1. The first thing is to create the configuration file. Create the app/code/Packt/HelloWorld/etc/crontab.xml file with the following...