Skip to content

Instantly share code, notes, and snippets.

@omerucel
Created October 5, 2016 12:06
Show Gist options
  • Save omerucel/500a021aa295c8b0665f373d7cb84fa6 to your computer and use it in GitHub Desktop.
Save omerucel/500a021aa295c8b0665f373d7cb84fa6 to your computer and use it in GitHub Desktop.
Clean gearmand job queue
php clear_queue.php --address=127.0.0.1 --port=4730
<?php
$cmdOptions = getopt('', ['address::', 'port::']);
$address = $cmdOptions['address'];
$port = $cmdOptions['port'];
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $address, $port);
$command = 'status' . PHP_EOL;
socket_write($socket, $command, strlen($command));
$out = socket_read($socket, 2048);
$items = explode("\n", substr($out, 0, strlen($out) - 2));
foreach ($items as $item) {
$item = explode("\t", $item);
if (count($item) != 4) {
continue;
}
if ($item[1] == 0) {
continue;
}
$jobCount = intval($item[1] - $item[2]);
if ($jobCount > 0) {
$command = 'gearman -n -h ' . $address . ' -p ' . $port . ' -w -f ' . $item[0]
. ' -c ' . $jobCount . ' > /dev/null';
exec('gearman -n -h ' . $address . ' -p ' . $port . ' -w -f ' . $item[0] . ' -c ' . $jobCount . ' > /dev/null');
}
}
socket_close($socket);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment