Blog

How To Setup A Cron Job In Magento – Magento Cron Tutorial

Posted Jun 21st, 2010 at 06:21 AM and seen 691 times

A CRON is a daemon which keeps running and gets activated at a said interval (time period). Cron helps store owners to setup up and schedule commands which will run periodically at set time and date. Various Magento services such as “Sending Newsletter “, Cleanup with logs, Cleanup cache, Customer notification etc. requires a mechanism where store owners can setup and schedule commands to run periodically.

Most of the web hosts have their own set of task scheduler; you can ask and verify the task scheduler from your webhost. While setting a cron-job make sure that minimum time duration is set to run every 15 minutes (not less than this) as  Magento spends around 1 minute to index 1000 products and sets the status to “Success” from “Pending”.

Now to setup Crontab in Magento go to Admin => system => configuration => system => ‘Cron (Scheduled Tasks)’ tab.

You will notice that all times are in minutes.

Magento Cron Best Practices

Generate schedules every: 60
Schedule ahead for: 1
Missed if not run within: 60
History cleanup every: 120
Success history lifetime: 120
Failure history lifetime: 120

run the cron every 15 minutes for better results and that will not overlap with other Cron services.

What goes in the background

When you set the Cron this will happen

1
2
3
4
5
6
7
8
// 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:

  1. execute scheduled tasks
  2. generate future scheduled tasks if needed
  3. clean up history of scheduled tasks

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

  1. <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.

Please leave me a comment and let me know if you have any problems setting up cron in Magento. Subscribe our RSS to receive latest updates.



How To Integrate Mailchimp Into Your Magento Store

Posted Mar 05th, 2010 at 01:54 AM and seen 795 times

mailchimp magento integration

Are you looking to integrate Mailchimp email subscription within your Magento powered store? If yes then you are at right place. There is a sudden rise in Magento stores but some store owners are disappointed with Magento’s default email subscription and newsletter handling capabilities. Basically they face the following problems:

  1. They can not send the newsletter right away because their local time is not the same as of server time. Eventually, their newsletter campaign will be sent few hours later than the intended time. Although, this can be adjusted with a little tweak.
  2. They can not send large number of emails in one go. Magento limits how many emails should be sent at once.
  3. Managing CRON is a pain.

Most of the problems i have mentioned above can easily be managed by installing our Magento’s email subscription extension. Many customers are adding third party email subscriptions i.e. Mailchimp, Feedburner, Nurture etc. to manage their email lists and send out mass email newsletters or promotional emails. I will cover how to integrate feedburner email subscription & how to add nurture emails subscriptions later. In this article i will cover how to integrate Mailchimp email subscription within your Magento powered store and how to create html newsletter in Mailchimp.

Adding Mailchimp Email Subscription Form In Your Magento Store

There are two ways to integrate Magento email subscription into your store:

  1. Install existing extensions e.g. Advanced Newsletter (aheadworks), MailChimp Synchronization (ebizmarts) or E-Commerce 360 (mailchimp).
  2. Manual Install

You can read the installation instructions of the above extension from their Magento connect list but if you looking to install Mailchimp newsletter manually then read on.

Steps:

  1. Log on to your Mailchimp account.
  2. Click on “Lists” tab. You will be shown a list of all available Newsletter (If not please create one for your particular store).
  3. On your list there is a link called settings, just click on that.
  4. Click on the secondary tab called “for your website”
  5. Now, let’s click on “Signup Form Embed Code” link.
  6. It will open up your Signup form designer with options like “Design Form Structure”, “Form Colors” & “Form Enhancements”. Modify the form and once all done click on “Create Embed code”. Copy the embed code from the text area at the left.
  7. Your Mailchimp signup code is ready, simply add this code to your Magento store and replace the existing signup code. You can change the code by editing app/design/frontend/default/theme/template/newsletter/subscribe.phtml You may have to make the necessary CSS adjustments for look and feel.
  8. All done.

Now, as you have replaced the default Magento Email subscription with Mailchimp email subscription. Next step is to draft a nice looking email newsletter template. Please watch Mailchimp videos and setup a newsletter design.

Please leave me a comment and let me know if you face any problems installing Mailchimp Signup Embed form in your Magento store. Don’t forget to subscribe our RSS to receive latest updates.