From 3e281976443af721b9fbaa5933540fcab4743b94 Mon Sep 17 00:00:00 2001 From: giaogiao Date: Thu, 7 Mar 2024 20:09:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0curl=E5=AE=8C=E6=88=90bark?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=9E=84=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/job/Hello.php | 134 +++++++++++++++++++++++++--------------------- composer.json | 3 +- composer.lock | 88 +++++++++++++++++++++++++++++- 3 files changed, 161 insertions(+), 64 deletions(-) diff --git a/app/job/Hello.php b/app/job/Hello.php index 7d11778..99966e2 100644 --- a/app/job/Hello.php +++ b/app/job/Hello.php @@ -1,66 +1,76 @@ checkDatabaseToSeeIfJobNeedToBeDone($data); - if(!$isJobStillNeedToBeDone){ - $job->delete(); - return; - } - - $isJobDone = $this->doHelloJob($data); - - if ($isJobDone) { - // 如果任务执行成功, 记得删除任务 - $job->delete(); - print("Hello Job has been done and deleted"."\n"); - }else{ - if ($job->attempts() > 3) { - //通过这个方法可以检查这个任务已经重试了几次了 - print("Hello Job has been retried more than 3 times!"."\n"); - - $job->delete(); - - // 也可以重新发布这个任务 - //print("Hello Job will be availabe again after 2s."."\n"); - //$job->release(2); //$delay为延迟时间,表示该任务延迟2秒后再执行 - } - } - } - - /** - * 有些消息在到达消费者时,可能已经不再需要执行了 - * @param array|mixed $data 发布任务时自定义的数据 - * @return boolean 任务执行的结果 - */ - private function checkDatabaseToSeeIfJobNeedToBeDone($data){ - return true; - } - /** - * 根据消息中的数据进行实际的业务处理... - */ - private function doHelloJob($data) - { - print("Hello Job Started. job Data is: ".var_export($data,true)." \n"); - print("Hello Job is Fired at " . date('Y-m-d H:i:s') ." \n"); - print("Hello Job is Done!"." \n"); - - return true; - } - } \ No newline at end of file +namespace app\job; + +use think\queue\Job; +use Curl\Curl; + +class Hello +{ + + /** + * fire方法是消息队列默认调用的方法 + * @param Job $job 当前的任务对象 + * @param array|mixed $data 发布任务时自定义的数据 + */ + public function fire(Job $job, $data) + { + // 有些消息在到达消费者时,可能已经不再需要执行了 + $isJobStillNeedToBeDone = $this->checkDatabaseToSeeIfJobNeedToBeDone($data); + if (!$isJobStillNeedToBeDone) { + $job->delete(); + return; + } + + $isJobDone = $this->doHelloJob($data); + + if ($isJobDone) { + // 如果任务执行成功, 记得删除任务 + $job->delete(); + print("Hello Job has been done and deleted" . "\n"); + } else { + if ($job->attempts() > 3) { + //通过这个方法可以检查这个任务已经重试了几次了 + print("Hello Job has been retried more than 3 times!" . "\n"); + + $job->delete(); + + // 也可以重新发布这个任务 + //print("Hello Job will be availabe again after 2s."."\n"); + //$job->release(2); //$delay为延迟时间,表示该任务延迟2秒后再执行 + } + } + } + + /** + * 有些消息在到达消费者时,可能已经不再需要执行了 + * @param array|mixed $data 发布任务时自定义的数据 + * @return boolean 任务执行的结果 + */ + private function checkDatabaseToSeeIfJobNeedToBeDone($data) + { + return true; + } + + /** + * 根据消息中的数据进行实际的业务处理... + */ + private function doHelloJob($data) + { + $curl = new Curl(); + $title=""; + $mssg=""; + $curl->get('https://api.day.app/3vrp4DbTGmSWxVbzHnUnPB/'.$title.$mssg); + print("Hello Job Started. job Data is: " . var_export($data, true) . " \n"); + print("Hello Job is Fired at " . date('Y-m-d H:i:s') . " \n"); + print("Hello Job is Done!" . " \n"); + + return true; + } +} diff --git a/composer.json b/composer.json index 7009e4c..deb1732 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ "topthink/framework": "^6.1.0", "topthink/think-orm": "^2.0", "topthink/think-filesystem": "^1.0", - "topthink/think-queue": "^3.0" + "topthink/think-queue": "^3.0", + "php-curl-class/php-curl-class": "^9.19" }, "require-dev": { "symfony/var-dumper": "^4.2", diff --git a/composer.lock b/composer.lock index ff4ee0d..cf8fec4 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cd4ee3a677ff5ee34177fccae9501c25", + "content-hash": "6f1dedd32ad3424b3151c92ff5c86d89", "packages": [ { "name": "carbonphp/carbon-doctrine-types", @@ -413,6 +413,92 @@ ], "time": "2024-01-25T10:35:09+00:00" }, + { + "name": "php-curl-class/php-curl-class", + "version": "9.19.1", + "source": { + "type": "git", + "url": "https://github.com/php-curl-class/php-curl-class.git", + "reference": "680afbcfd964f299fb320dba6c30f9930d1d478e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-curl-class/php-curl-class/zipball/680afbcfd964f299fb320dba6c30f9930d1d478e", + "reference": "680afbcfd964f299fb320dba6c30f9930d1d478e", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "ext-curl": "*", + "php": ">=7.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "*", + "ext-gd": "*", + "friendsofphp/php-cs-fixer": "*", + "phpcompatibility/php-compatibility": "dev-develop", + "phpcsstandards/phpcsutils": "@alpha", + "phpunit/phpunit": "*", + "squizlabs/php_codesniffer": "*", + "vimeo/psalm": ">=0.3.63" + }, + "suggest": { + "ext-mbstring": "*" + }, + "type": "library", + "autoload": { + "psr-4": { + "Curl\\": "src/Curl/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Unlicense" + ], + "authors": [ + { + "name": "Zach Borboa" + }, + { + "name": "Contributors", + "homepage": "https://github.com/php-curl-class/php-curl-class/graphs/contributors" + } + ], + "description": "PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs.", + "homepage": "https://github.com/php-curl-class/php-curl-class", + "keywords": [ + "API-Client", + "api", + "class", + "client", + "curl", + "framework", + "http", + "http-client", + "http-proxy", + "json", + "php", + "php-curl", + "php-curl-library", + "proxy", + "requests", + "restful", + "web-scraper", + "web-scraping ", + "web-service", + "xml" + ], + "support": { + "issues": "https://github.com/php-curl-class/php-curl-class/issues", + "source": "https://github.com/php-curl-class/php-curl-class/tree/9.19.1" + }, + "time": "2024-02-27T18:03:38+00:00" + }, { "name": "psr/cache", "version": "1.0.1",