7

When we run cron.php of magento then some jobs created in cron_schedule table of magento ? Can anyone let me know how magento execute these jobs exactly ? I mean how job status is changed from pending to success. Cron.php only insert jobs in the table not execute jobs

Hashid
  • 946
  • 2
  • 9
  • 26
Pankaj Gupta
  • 91
  • 1
  • 3

1 Answers1

16

You'd want to read through the following article available directly from the MagentoCommerce website which covers all that you need to know about cron functionality & Magento:

Magento - Wiki - How to Set Up a Cron Job

To highlight: Inner workings

Magento crontab mechanism is triggered periodically using system cron job. The call is initiated in cron.php file:

<?php
// initialize configuration and load event observers only from /crontab/ section
Mage::getConfig()->init()->loadEventObservers('crontab');

// initialize crontab event area Mage::app()->addEventArea('crontab');

// dispatch 'default' event for observers specified in crontab configuration Mage::dispatchEvent('default');

This sequence will invoke Mage_Cron_Model_Observer→dispatch(), which in turn will:

  • execute scheduled tasks
  • generate future scheduled tasks if needed
  • clean up history of scheduled tasks

Tasks are scheduled for each time the job needs to be run based on

<schedule><cron_expr>0 1 * * *</cron_expr></schedule>

expression and stored in cron_schedule table. Each record consists of the following fields:

  • schedule_id - unique identifier for scheduled task

  • job_code - job identifier from configuration

  • status - can be one of pending, running, success, missed, error

  • messages - custom text reported by method that was executed by the job

  • created_at - date/time when the task was created at

  • scheduled_at - date/time when the task is planned to be executed

  • executed_at - date/time when the task was actually executed (null prior to execution)

  • finished_at - date/time when the task finished execution (null prior to execution)

    When schedules are generated, status is set to pending, created_at to now() and scheduled_at to target date/time.

When pending schedules are executed, status is set to running and executed_at to now().

When scheduled task is finished successfully, status is set to success and finished_at to now().

When scheduled task has thrown an exception, status is set to error and finished_at to now().

If task status is pending and scheduled_at is older than “Missed if not run within” configured value, status is set to missed.

Moose
  • 7,495
  • 7
  • 48
  • 91