first commit

This commit is contained in:
Emir Buğra Köksalan 2020-06-29 22:43:57 +03:00
commit 43422e7c77
4 changed files with 90 additions and 0 deletions

11
.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
/vendor
composer.phar
composer.lock
.DS_Store
/node_modules
.env

12
composer.json Normal file
View File

@ -0,0 +1,12 @@
{
"name": "emir/beanstalkd-example",
"authors": [
{
"name": "Emir Buğra Köksalan",
"email": "kodmanyagha@gmail.com"
}
],
"require": {
"pda/pheanstalk": "^4.0"
}
}

33
producer.php Normal file
View File

@ -0,0 +1,33 @@
<?php
require __DIR__ . '/vendor/autoload.php';
use Pheanstalk\Pheanstalk;
$pheanstalk = Pheanstalk::create('127.0.0.1');
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
$msg = "id: " . $i . ' date: ' . date('Y-m-d H:i:s') . ' ' . json_encode($_SERVER);
$msg = json_encode(['id' => $i, 'date' => date('Y-m-d H:i:s'), 'server' => $_SERVER]);
$pheanstalk
->useTube('ExampleTube')
->put($msg);
echo 'Added: ' . $i . PHP_EOL;
if (((microtime(true) - $start) * 1000) >= 1000) {
break;
}
}
// with some settings
//$pheanstalk
// ->useTube('testtube')
// ->put(
// json_encode(['test' => 'data']), // encode data in payload
// Pheanstalk::DEFAULT_PRIORITY, // default priority
// 30, // delay by 30s
// 60 // beanstalk will retry job after 60s
// );

34
worker.php Normal file
View File

@ -0,0 +1,34 @@
<?php
require __DIR__ . '/vendor/autoload.php';
use Pheanstalk\Pheanstalk;
$pheanstalk = Pheanstalk::create('127.0.0.1');
// we want jobs from 'testtube' only.
$pheanstalk->watch('ExampleTube');
// this hangs until a Job is produced.
//for($i = 0; $i < 1000; $i++) {
while (true) {
$job = $pheanstalk->reserve();
try {
$jobPayload = $job->getData();
$jobPayload = json_decode($jobPayload);
echo date('Y-m-d H:i:s') . ' ' . $jobPayload->id . PHP_EOL;
//usleep(100);
//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);
}
}