增加curl完成bark请求构造

This commit is contained in:
giaogiao 2024-03-07 20:09:13 +08:00
parent 018597fe2a
commit 3e28197644
3 changed files with 161 additions and 64 deletions

View File

@ -1,66 +1,76 @@
<?php <?php
/**
* 文件路径: \application\index\job\Hello.php
* 这是一个消费者类,用于处理 helloJobQueue 队列中的任务
*/
namespace app\job;
use think\queue\Job; /**
* 文件路径: \application\index\job\Hello.php
* 这是一个消费者类,用于处理 helloJobQueue 队列中的任务
*/
class Hello {
/** namespace app\job;
* 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); use think\queue\Job;
use Curl\Curl;
if ($isJobDone) { class Hello
// 如果任务执行成功, 记得删除任务 {
$job->delete();
print("<info>Hello Job has been done and deleted"."</info>\n");
}else{
if ($job->attempts() > 3) {
//通过这个方法可以检查这个任务已经重试了几次了
print("<warn>Hello Job has been retried more than 3 times!"."</warn>\n");
$job->delete(); /**
* 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);
//print("<info>Hello Job will be availabe again after 2s."."</info>\n");
//$job->release(2); //$delay为延迟时间表示该任务延迟2秒后再执行
}
}
}
/** if ($isJobDone) {
* 有些消息在到达消费者时,可能已经不再需要执行了 // 如果任务执行成功, 记得删除任务
* @param array|mixed $data 发布任务时自定义的数据 $job->delete();
* @return boolean 任务执行的结果 print("<info>Hello Job has been done and deleted" . "</info>\n");
*/ } else {
private function checkDatabaseToSeeIfJobNeedToBeDone($data){ if ($job->attempts() > 3) {
return true; //通过这个方法可以检查这个任务已经重试了几次了
} print("<warn>Hello Job has been retried more than 3 times!" . "</warn>\n");
/** $job->delete();
* 根据消息中的数据进行实际的业务处理...
*/
private function doHelloJob($data)
{
print("<info>Hello Job Started. job Data is: ".var_export($data,true)."</info> \n");
print("<info>Hello Job is Fired at " . date('Y-m-d H:i:s') ."</info> \n");
print("<info>Hello Job is Done!"."</info> \n");
return true; // 也可以重新发布这个任务
} //print("<info>Hello Job will be availabe again after 2s."."</info>\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("<info>Hello Job Started. job Data is: " . var_export($data, true) . "</info> \n");
print("<info>Hello Job is Fired at " . date('Y-m-d H:i:s') . "</info> \n");
print("<info>Hello Job is Done!" . "</info> \n");
return true;
}
}

View File

@ -24,7 +24,8 @@
"topthink/framework": "^6.1.0", "topthink/framework": "^6.1.0",
"topthink/think-orm": "^2.0", "topthink/think-orm": "^2.0",
"topthink/think-filesystem": "^1.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": { "require-dev": {
"symfony/var-dumper": "^4.2", "symfony/var-dumper": "^4.2",

88
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "cd4ee3a677ff5ee34177fccae9501c25", "content-hash": "6f1dedd32ad3424b3151c92ff5c86d89",
"packages": [ "packages": [
{ {
"name": "carbonphp/carbon-doctrine-types", "name": "carbonphp/carbon-doctrine-types",
@ -413,6 +413,92 @@
], ],
"time": "2024-01-25T10:35:09+00:00" "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", "name": "psr/cache",
"version": "1.0.1", "version": "1.0.1",