protected function deleteAllFromTube($state, $tube)
{
try {
do {
switch ($state) {
case 'ready':
$job = $this->interface->_client->useTube($tube)->peekReady();
break;
case 'delayed':
try {
$ready = $this->interface->_client->useTube($tube)->peekReady();
if ($ready) {
$this->_errors[] = 'Cannot delete Delayed until there are Ready messages on this tube';
return;
}
} catch (Exception $e) {
// there might be no jobs to peek at, and peekReady raises exception in this situation
if (strpos($e->getMessage(), Pheanstalk_Response::RESPONSE_NOT_FOUND) === false) {
throw $e;
}
}
try {
$bury = $this->interface->_client->useTube($tube)->peekBuried();
if ($bury) {
$this->_errors[] = 'Cannot delete Delayed until there are Bury messages on this tube';
return;
}
} catch (Exception $e) {
// there might be no jobs to peek at, and peekReady raises exception in this situation
if (strpos($e->getMessage(), Pheanstalk_Response::RESPONSE_NOT_FOUND) === false) {
throw $e;
}
}
$job = $this->interface->_client->useTube($tube)->peekDelayed();
if ($job) {
//when we found job with Delayed, kick all messages, to be ready, so that we can Delete them.
$this->interface->kick($tube, 100000000);
$this->deleteAllFromTube('ready', $tube);
return;
}
break;
case 'buried':
$job = $this->interface->_client->useTube($tube)->peekBuried();
break;
}
if ($job) {
$this->interface->_client->delete($job);
set_time_limit(5);
}
} while (!empty($job));
} catch (Exception $e) {
// there might be no jobs to peek at, and peekReady raises exception in this situation
// skip not found exception
if (strpos($e->getMessage(), Pheanstalk_Response::RESPONSE_NOT_FOUND) === false) {
$this->_errors[] = $e->getMessage();
}
}
}