Book Image

Getting Started with Magento Extension Development

By : Branko Ajzele
Book Image

Getting Started with Magento Extension Development

By: Branko Ajzele

Overview of this book

Modules, are a group of php and xml files meant to extend the system with new functionality, or override core system behavior. Most of the base Magento system is built using the module system, so you can see why they are an important feature for this rich open-source e-commerce solutions. This book explores key module development techniques and teaches you to modify, understand and structure your modules making it easy for you to get a strong foundation for clean and unobtrusive Magento module development. Getting Started with Magento Extension Development is a practical, hands-on guide to building Magento modules from scratch. This book provides an in depth introduction and helps you discover features such as; blocks, controllers, models, configuration files, and other crucial elements which contribute to the Magento architecture. This book introduces the you to real-world modules and helps provide a strong foundation which you need to become a professional Magento module developer. The book further explores best practices and tips and tricks offering you the ultimate go to guide. Getting Started with Magento Extension Development focuses on three areas. First you are guided through the entire Magento structure, where each important directory or file is explored in detail. Then the essence of the module structure and development is explained through the detailed coverage of models, blocks, controllers, configuration, and other files that manifest a single module. Finally, a detailed set of instructions is given for building four real-world modules, including a payment and shipping module.
Table of Contents (13 chapters)
Getting Started with Magento Extension Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Cron jobs


Cron jobs are used to schedule commands to be executed periodically by the cron. Cron is a a time-based job scheduler software in Unix-like computer operating systems. It is driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule.

Magento cron jobs is a different kind of functionality than that just mentioned. It merely relies on the system cron software to trigger the root Magento cron.php or cron.sh files periodically. Keep that in mind while talking about cron and Magento cron jobs.

It is highly important that the system administrator has the system cron set to trigger the Magento cron.sh file at regular intervals, at least every five minutes. This can be done by adding the following line to system cron:*/5 * * * * /path/to /magento/root/folder/cron.sh. This way, you as a Magento extension developer have the ability to create a new Magento cron jobs definitions through your extensions configuration files and rest assured they will get executed. Magento cron jobs are defined in the config.xml file as follows:

<?xml version="1.0"?>
<config>
<!-- … other elements ... -->
   <crontab>
      <jobs>
         <foggyline_happyhour_ordersToFulfilment>
            <schedule>
               <cron_expr>*/2 * * * *</cron_expr>
            </schedule>
            <run>
               <model>foggyline_happyhour/service::ordersToFulfilment</model>
            </run>
         </foggyline_happyhour_ordersToFulfilment>
      </jobs>
   </crontab>   
<!-- … other elements ... -->
</config>

In this example, our cron job has been defined with the name foggyline_happyhour_ordersToFulfilment and set to execute every two minutes, which can be seen by the schedule >cron_expr value. For more details on writing cron expressions, check out the following URL: http://en.wikipedia.org/wiki/Cron.

The model element, which in this case is set to foggyline_happyhour/service::ordersToFulfilment, means that the following code from the app/code/community/Foggyline/HappyHour/Model/Service.php file would get executed by this cron job:

<?php

class Foggyline_HappyHour_Model_Service
{
   public function ping() 
   {
      Mage::log('ping');
   }
}

The same as with observer classes, cron-defined model classes do not need to extend anything.

Even though this Magento cron job has been set to run every two minutes, you as an extension developer have no guarantee that the system cron will ever be run on a third-party website, or if it's run it might not be run in small enough intervals. Thus, you might end up with a code logic that never gets executed, or gets executed in larger than planned intervals.

During development, however, you do not need to have the system cron set up. It is sufficient to just execute the http://magento.loc/cron.php URL in the browser, as this will trigger the Magento cron system.

For example, if you were writing an extension that has one or more Magento cron jobs defined in config.xml, the easiest way to check if you correctly defined your cron job would be to truncate the cron_schedule database table and then trigger the http://magento.loc/cron.php URL in the browser. Obviously, in this case, later on you would have to periodically trigger the http://magento.loc/cron.php URL to check if your cron job execution went through, monitoring the executed_at column within the cron_schedule database table.