Skip to content

Instantly share code, notes, and snippets.

@darklow
Created August 10, 2012 07:00
Show Gist options
  • Save darklow/3312032 to your computer and use it in GitHub Desktop.
Save darklow/3312032 to your computer and use it in GitHub Desktop.
GearmanClient class extended to prevent duplicate jobs
<?php
class GearmanClient extends \GearmanClient
{
const PRIORITY_LOW = 'low';
const PRIORITY_HIGH = 'high';
const PRIORITY_NORMAL = 'normal';
public function getUniqueId($function_name, $workload, $priority = null)
{
return sha1($function_name.$workload.$priority);
}
public function getMethodName($runBackground, $priority)
{
$method = 'do';
if ($priority !== self::PRIORITY_NORMAL || !$runBackground) {
$method .= ucfirst($priority);
}
if ($runBackground) {
$method .= 'Background';
}
return $method;
}
public function doSpecific($function_name, $workload, $runBackground, $priority = self::PRIORITY_NORMAL, $unique = null)
{
$method = $this->getMethodName($runBackground, $priority);
return $this->$method($function_name, $workload, $unique);
}
public function doNormal($function_name, $workload, $unique = null)
{
$unique = $this->getUniqueId($function_name, $workload, self::PRIORITY_NORMAL);
return parent::doNormal($function_name, $workload, $unique);
}
public function doHigh($function_name, $workload, $unique = null)
{
$unique = $this->getUniqueId($function_name, $workload, self::PRIORITY_HIGH);
return parent::doHigh($function_name, $workload, $unique);
}
public function doLow($function_name, $workload, $unique = null)
{
$unique = $this->getUniqueId($function_name, $workload, self::PRIORITY_LOW);
return parent::doLow($function_name, $workload, $unique);
}
public function doBackground($function_name, $workload, $unique = null)
{
$unique = $this->getUniqueId($function_name, $workload, self::PRIORITY_NORMAL);
return parent::doBackground($function_name, $workload, $unique);
}
public function doHighBackground($function_name, $workload, $unique = null)
{
$unique = $this->getUniqueId($function_name, $workload, self::PRIORITY_HIGH);
return parent::doHighBackground($function_name, $workload, $unique);
}
public function doLowBackground($function_name, $workload, $unique = null)
{
$unique = $this->getUniqueId($function_name, $workload, self::PRIORITY_LOW);
return parent::doLowBackground($function_name, $workload, $unique);
}
public function addTask($function_name, $workload, $context = null, $unique = null)
{
$unique = $this->getUniqueId($function_name, $workload, self::PRIORITY_NORMAL);
return parent::addTask($function_name, $workload, $context, $unique);
}
public function addTaskHigh($function_name, $workload, $context = null, $unique = null)
{
$unique = $this->getUniqueId($function_name, $workload, self::PRIORITY_HIGH);
return parent::addTaskHigh($function_name, $workload, $context, $unique);
}
public function addTaskLow($function_name, $workload, $context = null, $unique = null)
{
$unique = $this->getUniqueId($function_name, $workload, self::PRIORITY_LOW);
return parent::addTaskLow($function_name, $workload, $context, $unique);
}
public function addTaskBackground($function_name, $workload, $context = null, $unique = null)
{
$unique = $this->getUniqueId($function_name, $workload, self::PRIORITY_NORMAL);
return parent::addTaskBackground($function_name, $workload, $context, $unique);
}
public function addTaskHighBackground($function_name, $workload, $context = null, $unique = null)
{
$unique = $this->getUniqueId($function_name, $workload, self::PRIORITY_HIGH);
return parent::addTaskHighBackground($function_name, $workload, $context, $unique);
}
public function addTaskLowBackground($function_name, $workload, $context = null, $unique = null)
{
$unique = $this->getUniqueId($function_name, $workload, self::PRIORITY_LOW);
return parent::addTaskLowBackground($function_name, $workload, $context, $unique);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment