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

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.

| improve this answer | |
  • Hurry for the documentation which Magento preserved... – janw Jan 5 '16 at 15:01
  • @janw pardon me? – Moose Jan 7 '16 at 8:11
  • 1
    The link above is broken (general redirect). I hate how Magento broke all those links. – janw Jan 7 '16 at 8:12
  • 1
    Yeah, it is upsetting, the same with their forum information too. Anyway, I did copy most of the article into my answer so it's still relatively complete :) – Moose Jan 7 '16 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.