<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Made of Everything You&#039;re Not &#187; cron</title>
	<atom:link href="http://blog.ericlamb.net/tag/cron/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ericlamb.net</link>
	<description>Thoughts on programming, people and life</description>
	<lastBuildDate>Thu, 27 Oct 2011 01:29:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Half Assed Cron With WP Cron</title>
		<link>http://blog.ericlamb.net/2010/02/half-assed-cron-with-wp-cron/</link>
		<comments>http://blog.ericlamb.net/2010/02/half-assed-cron-with-wp-cron/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 09:06:27 +0000</pubDate>
		<dc:creator>Eric Lamb</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://dev.blog.ericlamb.net/?p=2941</guid>
		<description><![CDATA[I was working on a project recently that was using WordPress as the base platform and had a need for a scheduled task function. Ordinarily, this would be the type of functionality that I would just setup a cron job for; but since WordPress has a pseudo cron system in place I decided to investigate [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on a project recently that was using <a title="WordPress" href="http://wordpress.org/" onclick="return TrackClick('http%3A%2F%2Fwordpress.org%2F','WordPress')" target="_blank">WordPress</a> as the base platform and had a need for a scheduled task function. Ordinarily, this would be the type of functionality that I would just setup a cron job for; but since WordPress has a <a title="Pseudu-Cron" href="http://www.bitfolge.de/?l=en&amp;s=pseudocron" onclick="return TrackClick('http%3A%2F%2Fwww.bitfolge.de%2F%3Fl%3Den%26amp%3Bs%3Dpseudocron','Pseudu-Cron')" target="_blank">pseudo cron</a> system in place I decided to investigate that as an option. Long story short; fuck that man.</p>
<div id="attachment_2942" class="wp-caption aligncenter" style="width: 310px"><img class="size-full wp-image-2942" title="Working With WP Cron" src="http://dev.blog.ericlamb.net/wp-content/uploads/2010/03/hourglass.jpg" alt="Working With WP Cron" width="300" height="191" /><p class="wp-caption-text">Working With WP Cron</p></div>
<p>As mentioned, WordPress has a pseudo cron mechanism available to WordPress developers for scheduling tasks called WP Cron. WP Cron works using series of functions available for plugin developers to create their own cron style tasks. In theory WordPress makes this easy(ish) using the built in cron API but the reality is that it’s not only confusing and awkward to implement but once you’re complete you have to allow for a bit of latitude on the scheduling.</p>
<p>Take a look at scheduling a task in WordPress. Basically, it works by adding a function to your activation hook, within a plugin, that like the below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
register_activation_hook<span style="color: #000;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'my_activation'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
add_action<span style="color: #000;">&#40;</span><span style="color: #0000ff;">'my_hourly_event'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'do_this_hourly'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> my_activation<span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
	wp_schedule_event<span style="color: #000;">&#40;</span><span style="color: #990000;">time</span><span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hourly'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'my_hourly_event'</span><span style="color: #000;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> do_this_hourly<span style="color: #000;">&#40;</span><span style="color: #000;">&#41;</span> <span style="color: #000;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// do something every hour</span>
<span style="color: #000;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Pretty simple implementation really though not very “cron” like in syntax. The important part is the call to “<a href="http://codex.wordpress.org/Function_Reference/wp_schedule_event" onclick="return TrackClick('http%3A%2F%2Fcodex.wordpress.org%2FFunction_Reference%2Fwp_schedule_event','wp_schedule_event')" title="wp_schedule_event" target="_blank">wp_schedule_event()</a>” which takes 4 parameters (start date, recurrence, hook and arguments). According to the API manual the function:</p>
<blockquote><p>
Schedules a hook which will be executed by the WordPress actions core on a specific interval, specified by you. The action will trigger when someone visits your WordPress site, if the scheduled time has passed. See the <a href="http://codex.wordpress.org/Plugin_API" onclick="return TrackClick('http%3A%2F%2Fcodex.wordpress.org%2FPlugin_API','Wordpress+Plugin+API')" target="_blank" title="Wordpress Plugin API">Plugin API</a> for a list of hooks.
</p></blockquote>
<p>The problem I had was that this function only allows for 3 options on recurrence; hourly, twicedaily and daily. I needed my function to run at a specific time. (It was EXTREMELY important that it ran during a specific interval.)</p>
<p>This isn’t to say that it’s not possible to schedule a task for a specific time; it’s just a pain in the ass. You have to write all the logic to determine when to add a new task and removing expired tasks. Ughh. To me the question becomes “Should I write a couple hundred lines of code or just add a single line to my crontab?”.</p>
<p>Crontab baby.</p>
<div><a class="addthis_button" href="http://blog.ericlamb.net//addthis.com/bookmark.php?v=250" addthis:url='http://blog.ericlamb.net/2010/02/half-assed-cron-with-wp-cron/' addthis:title='Half Assed Cron With WP Cron '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></content:encoded>
			<wfw:commentRss>http://blog.ericlamb.net/2010/02/half-assed-cron-with-wp-cron/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

