1. Go to this page and download the library: Download pda/pheanstalk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pda / pheanstalk example snippets
use Pheanstalk\Pheanstalk;
use Pheanstalk\Values\TubeName;
$pheanstalk = Pheanstalk::create('127.0.0.1');
$tube = new TubeName('testtube');
// Queue a Job
$pheanstalk->useTube($tube);
$pheanstalk->put("job payload goes here\n");
$pheanstalk->useTube($tube);
$pheanstalk->put(
data: json_encode(['test' => 'data'], JSON_THROW_ON_ERROR),
priority: Pheanstalk::DEFAULT_PRIORITY,
delay: 30,
timeToRelease: 60
);
use Pheanstalk\Pheanstalk;
use Pheanstalk\Values\TubeName;
$pheanstalk = Pheanstalk::create('127.0.0.1');
$tube = new TubeName('testtube');
// we want jobs from 'testtube' only.
$pheanstalk->watch($tube);
// this hangs until a Job is produced.
$job = $pheanstalk->reserve();
try {
$jobPayload = $job->getData();
// do work.
echo "Starting job with payload: {$jobPayload}\n";
sleep(2);
// If it's going to take a long time, periodically
// tell beanstalk we're alive to stop it rescheduling the job.
$pheanstalk->touch($job);
sleep(2);
// eventually we're done, delete job.
$pheanstalk->delete($job);
}
catch(\Exception $e) {
// handle exception.
// and let some other worker retry.
$pheanstalk->release($job);
}
use Pheanstalk\Pheanstalk;
use Pheanstalk\Values\TubeName;
interface Task {
}
interface TaskFactory {
public function fromData(string $data): Task;
}
interface CommandBus {
public function handle(Task $task): void;
}
function run(\Pheanstalk\PheanstalkSubscriber $pheanstalk, CommandBus $commandBus, TaskFactory $taskFactory): void
{
/**
* @phpstan-ignore-next-line
*/
while (true) {
$job = $pheanstalk->reserveWithTimeout(50);
if (isset($job)) {
try {
$task = $taskFactory->fromData($job->getData());
$commandBus->handle($task);
echo "Deleting job: {$job->getId()}\n";
$pheanstalk->delete($job);
} catch (\Throwable $t) {
echo "Burying job: {$job->getId()}\n";
$pheanstalk->bury($job);
}
}
}
}