From bf11e587935be0a3bebf18bb7f39c224fefc3213 Mon Sep 17 00:00:00 2001 From: Emir Bugra Koksalan Date: Fri, 7 Jan 2022 23:44:57 +0300 Subject: [PATCH] minor changes --- .idea/.gitignore | 8 + .idea/beanstalkd-example.iml | 13 ++ .idea/dbnavigator.xml | 456 +++++++++++++++++++++++++++++++++++++++++++ .idea/markdown.xml | 10 + .idea/modules.xml | 8 + .idea/php.xml | 12 ++ .idea/vcs.xml | 6 + bstd.php | 15 ++ bstd_data/.gitignore | 2 + composer.json | 19 +- producer.php | 45 +++-- readme.md | 11 ++ startbstd.sh | 8 + worker.php | 56 ++++-- 14 files changed, 624 insertions(+), 45 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/beanstalkd-example.iml create mode 100644 .idea/dbnavigator.xml create mode 100644 .idea/markdown.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/php.xml create mode 100644 .idea/vcs.xml create mode 100644 bstd.php create mode 100644 bstd_data/.gitignore create mode 100644 readme.md create mode 100755 startbstd.sh diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/beanstalkd-example.iml b/.idea/beanstalkd-example.iml new file mode 100644 index 0000000..07afcd1 --- /dev/null +++ b/.idea/beanstalkd-example.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml new file mode 100644 index 0000000..ae4f687 --- /dev/null +++ b/.idea/dbnavigator.xml @@ -0,0 +1,456 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/markdown.xml b/.idea/markdown.xml new file mode 100644 index 0000000..21988a0 --- /dev/null +++ b/.idea/markdown.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..163ba7d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..d9f67a7 --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/bstd.php b/bstd.php new file mode 100644 index 0000000..2b9c868 --- /dev/null +++ b/bstd.php @@ -0,0 +1,15 @@ + $i, 'date' => date('Y-m-d H:i:s'), 'server' => $_SERVER]); + $msg = json_encode([ + 'id' => $i, + 'date' => date('Y-m-d H:i:s'), + 'PHP_SELF' => $_SERVER['PHP_SELF'], + 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'], + 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'], + ]); + $msg = json_encode([ + 'id' => $i, + 'date' => date('Y-m-d H:i:s'), + '_SERVER' => $_SERVER, + //'_SERVER2' => $_SERVER, + ]); -$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); - $pheanstalk - ->useTube('ExampleTube') - ->put($msg); - - echo 'Added: ' . $i . PHP_EOL; - - if (((microtime(true) - $start) * 1000) >= 1000) { - break; - } + //echo 'Added: ' . $i . PHP_EOL; + if (((microtime(true) - $start) * 1000) >= 1000) { + $lastIndex = $i; + break; + } } +echo "Last index: " . $lastIndex . PHP_EOL; + // with some settings //$pheanstalk // ->useTube('testtube') diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..72c243e --- /dev/null +++ b/readme.md @@ -0,0 +1,11 @@ +# Connect to beanstalkd with PHP + +Start beanstalkd daemon like that: + +``` +beanstalkd -l unix:/tmp/bstd.sock +``` + + + + diff --git a/startbstd.sh b/startbstd.sh new file mode 100755 index 0000000..cbbdb36 --- /dev/null +++ b/startbstd.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +pkill -f wpcrm_bstd_service +nohup beanstalkd -V -b ./bstd_data -f 300 -l unix://$(pwd)/bstd_data/wpcrm_bstd_service.sock -z 2097152 > bstd_data/bstd.log 2>&1 & + +#beanstalkd -V -l unix://$(pwd)/bstd_data/bstd.sock + + diff --git a/worker.php b/worker.php index c8dc4cc..89e837d 100644 --- a/worker.php +++ b/worker.php @@ -1,8 +1,9 @@ watch('ExampleTube'); @@ -11,24 +12,39 @@ $pheanstalk->watch('ExampleTube'); //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; + $job = null; - //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); + try { + $job = $pheanstalk->reserveWithTimeout(3); - // eventually we're done, delete job. - $pheanstalk->delete($job); - } catch (\Exception $e) { - // handle exception. - // and let some other worker retry. - $pheanstalk->release($job); - } + if (is_null($job)) + throw new Exception("Job is null"); + + $jobPayload = $job->getData(); + $jobPayload = json_decode($jobPayload); + + echo date('Y-m-d H:i:s') . ' ' . str_pad($jobPayload->id, 5, " ", STR_PAD_LEFT) . ' ' . $jobPayload->date . PHP_EOL; + //echo date('Y-m-d H:i:s') . ' ' . str_pad($jobPayload->id, 5, " ", STR_PAD_LEFT) . ' ' . json_encode($jobPayload) . PHP_EOL; + + usleep(mt_rand(1, 10)); + //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); + + // eventually we're done, delete job. + $pheanstalk->delete($job); + } catch (\Throwable $e) { + // handle exception. + // and let some other worker retry. + if (!is_null($job)) + $pheanstalk->release($job); + + if (str_contains($e->getMessage(), 'Connection refused')) { + echo 'Beanstalkd server is down'; + die(); + } else if (!($e instanceof Exception)) { + echo $e->getMessage() . PHP_EOL; + } + } }