<?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>Mats Lindh &#187; Gearman</title>
	<atom:link href="http://e-mats.org/category/gearman/feed/" rel="self" type="application/rss+xml" />
	<link>http://e-mats.org</link>
	<description>Where desperate is just another word for a regular day.</description>
	<lastBuildDate>Fri, 23 Jul 2010 14:23:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Adding Support for Asynchronous Status Requests for Net_Gearman</title>
		<link>http://e-mats.org/2009/07/adding-support-for-asynchronous-status-requests-for-net_gearman/</link>
		<comments>http://e-mats.org/2009/07/adding-support-for-asynchronous-status-requests-for-net_gearman/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 19:14:57 +0000</pubDate>
		<dc:creator>Mats</dc:creator>
				<category><![CDATA[Gearman]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[gearmand]]></category>
		<category><![CDATA[net_german]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[patchset]]></category>
		<category><![CDATA[pear]]></category>

		<guid isPermaLink="false">http://e-mats.org/?p=541</guid>
		<description><![CDATA[I spent the evening yesterday playing around a bit more with Gearman, a system for farming out tasks to workers across several servers. As my workstation at home still runs Windows, the only PHP library available is the Net_Gearman in PEAR. Net_Gearman supports tasks (something to do), sets (a collection of tasks), workers (the processes [...]]]></description>
			<content:encoded><![CDATA[<p>I spent the evening yesterday playing around a bit more with <a href="http://www.gearman.org/">Gearman</a>, a system for farming out tasks to workers across several servers. As my workstation at home still runs Windows, the only PHP library available is the Net_Gearman in PEAR. Net_Gearman supports tasks (something to do), sets (a collection of tasks), workers (the processes that performs the task) and clients (which requests tasks to be performed). The gearman protocol supports retrieving the current status of a task from the gearman server (which contains information about how the worker is progressing, reported by the worker itself), but Net_Gearman did not.</p>
<p>The reason for &#8216;did not&#8217; is that I&#8217;ve created a small patchset to add the functionality to Net_Gearman. All internal methods and properties are still used as they were before, but I&#8217;ve added two helper methods for retrieving the socket connection for a particular gearman server (Net_Gearman usually just picks a random server, but we need to contact the server that&#8217;s responsible for the task) and a getStatus(server, handle) method to the Gearman Client. I&#8217;ve also added a property keeping the address of the server which were assigned the task to the Task class.</p>
<p>After submitting a task to be performed in the background (you do not need this to get the status for foreground tasks, as you can provide a callback to handle that), your Task object will have its <code>handle</code> and <code>server</code> properties set. These can be used to retrieve status information about the task later. You&#8217;ll still need to provide the possible servers to the Gearman client when creating the client (through the constructor).</p>
<p>Example of creating a task and retrieving the server / handle pair after starting the task:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">require_once</span> <span class="st0">&#39;Net/Gearman/Client.php&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$client</span> <span class="sy0">=</span> <span class="kw2">new</span> Net_Gearman_Client<span class="br0">&#40;</span><span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;host:4730&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$task</span> <span class="sy0">=</span> <span class="kw2">new</span> Net_Gearman_Task<span class="br0">&#40;</span><span class="st0">&#39;Reverse&#39;</span><span class="sy0">,</span> <span class="kw3">range</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="sy0">,</span><span class="nu0">5</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$task</span><span class="sy0">-&gt;</span><span class="me1">type</span> <span class="sy0">=</span> Net_Gearman_Task<span class="sy0">::</span><span class="me2">JOB_BACKGROUND</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$set</span> <span class="sy0">=</span> <span class="kw2">new</span> Net_Gearman_Set<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$set</span><span class="sy0">-&gt;</span><span class="me1">addTask</span><span class="br0">&#40;</span><span class="re1">$task</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$client</span><span class="sy0">-&gt;</span><span class="me1">runSet</span><span class="br0">&#40;</span><span class="re1">$set</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">print</span><span class="br0">&#40;</span><span class="st0">&quot;Status information: <span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">print</span><span class="br0">&#40;</span><span class="re1">$task</span><span class="sy0">-&gt;</span><span class="me1">handle</span> <span class="sy0">.</span> <span class="st0">&quot;<span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">print</span><span class="br0">&#40;</span><span class="re1">$task</span><span class="sy0">-&gt;</span><span class="me1">server</span> <span class="sy0">.</span> <span class="st0">&quot;<span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>Retrieving the status:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">require_once</span> <span class="st0">&#39;Net/Gearman/Client.php&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$client</span> <span class="sy0">=</span> <span class="kw2">new</span> Net_Gearman_Client<span class="br0">&#40;</span><span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;host:4730&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$status</span> <span class="sy0">=</span> <span class="re1">$client</span><span class="sy0">-&gt;</span><span class="me1">getStatus</span><span class="br0">&#40;</span><span class="st0">&#39;host:4730&#39;</span><span class="sy0">,</span> <span class="st0">&#39;H:mats-ubuntu:1&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>The array returned from the getStatus() method is the same array as returned from the gearman server and contains information about the current status (numerator, denominator, finished, etc, var_dump it to get the current structure). I&#8217;ve also added <a href="http://e-mats.org/resources/GearmanGetStatusSupport.tar.gz">the patchset</a> to <a href="http://github.com/lenn0x/net_gearman/issues">the Issue tracker</a> for <a href="http://github.com/lenn0x/net_gearman/tree/master">Net_Gearman at github</a>.</p>
<p>The patchset (created from the current master branch at github) can be downloaded here: <a href="http://e-mats.org/resources/GearmanGetStatusSupport.tar.gz">GearmanGetStatusSupport.tar.gz</a>.</p>
<p>UPDATE: I&#8217;ve finally gotten around to creating <a href="http://github.com/matslindh/net_gearman/tree/master">my own fork of NET_Gearman on github</a> too. This fork features the patch mentioned above.</p>
]]></content:encoded>
			<wfw:commentRss>http://e-mats.org/2009/07/adding-support-for-asynchronous-status-requests-for-net_gearman/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
