tp5.1框架 think-queue 队列
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Cms.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace app\index\controller;
  3. use think\Db;
  4. use think\Queue;
  5. use think\Controller;
  6. /*
  7. *
  8. */
  9. class Cms extends Controller
  10. {
  11. /*
  12. * 队列处理标签
  13. * https://queue.dev.zx2049.com/index/Cms/tagHandle
  14. */
  15. public function tagHandle()
  16. {
  17. $h = date('H',time());
  18. $h = (int)$h;
  19. if($h > 8 && $h < 18){
  20. //总共10小时
  21. // 1.当前任务将由哪个类来负责处理。
  22. // 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法
  23. $jobHandlerClassName = 'app\jobs\cms\Tag';
  24. // 2.当前任务归属的队列名称,如果为新队列,会自动创建
  25. $jobQueueName = "TagJobQueue";
  26. // 3.当前任务所需的业务数据, 不能为 resource 类型,其他类型最终将转化为json形式的字符串
  27. // (jobData 为对象时,存储其public属性的键值对 )
  28. $area = Db::connect('db1')
  29. ->table('ey_citysite')
  30. ->where(['level'=>1,'is_open'=>1,'status'=>1])
  31. //->limit(1)
  32. ->select();
  33. //dump($area);
  34. $area[] = [
  35. 'id' => 10000,
  36. 'name' => '主站',
  37. ];
  38. foreach ($area as $k=>$v){
  39. //http://console.zx2049.com/api/cms/tagHandle?taskid=10&diqu=1
  40. $jobData = ['ts' => time(),'url'=>'http://console.zx2049.com/api/cms/tagHandle','taskid'=>10, 'area_id' => $v['id'], 'area_name' => $v['name']];//当前任务所需的业务数据
  41. // 4.将该任务推送到消息队列,等待对应的消费者去执行
  42. $isPushed = Queue::push($jobHandlerClassName, $jobData, $jobQueueName);
  43. // database 驱动时,返回值为 1|false;redis 驱动时,返回值为 随机字符串|false
  44. //域名解析存在两条 *.dev
  45. //这里明明返回0 0 !== false 是成立的
  46. //以为一直成功 结果根本没插入数据库
  47. //既然能执行到这里的代码 为何无法插入数据库 返回0
  48. if( $isPushed !== false ){
  49. echo date('Y-m-d H:i:s') . " a new Hello Job is Pushed to the MQ" . "<br>";
  50. }else{
  51. echo 'Oops, something went wrong.';
  52. }
  53. echo "<br/>";
  54. }
  55. }else{
  56. echo "不在任务时间段内".$h;
  57. }
  58. }
  59. /*
  60. * 队列处理任务
  61. * https://queue.dev.zx2049.com/index/Cms/taskHandle
  62. */
  63. public function taskHandle()
  64. {
  65. $h = date('H', time());
  66. $h = (int)$h;
  67. if ($h > 8 && $h < 18) {
  68. // 1.当前任务将由哪个类来负责处理。
  69. // 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法
  70. $jobHandlerClassName = 'app\jobs\cms\Task';
  71. // 2.当前任务归属的队列名称,如果为新队列,会自动创建
  72. $jobQueueName = "TaskJobQueue";
  73. // 3.当前任务所需的业务数据, 不能为 resource 类型,其他类型最终将转化为json形式的字符串
  74. // (jobData 为对象时,存储其public属性的键值对 )
  75. $area = Db::connect('db1')
  76. ->table('ey_citysite')
  77. ->where(['level' => 1, 'is_open' => 1, 'status' => 1])
  78. //->limit(1)
  79. ->select();
  80. //dump($area);
  81. $area[] = [
  82. 'id' => 10000,
  83. 'name' => '主站',
  84. ];
  85. foreach ($area as $k => $v) {
  86. //http://console.zx2049.com/api/cms/tagHandle?taskid=10&diqu=1
  87. $jobData = ['ts' => time(), 'url' => 'http://console.zx2049.com/api/cms/taskHandle', 'taskid' => 10, 'area_id' => $v['id'], 'area_name' => $v['name']];//当前任务所需的业务数据
  88. // 4.将该任务推送到消息队列,等待对应的消费者去执行
  89. $isPushed = Queue::push($jobHandlerClassName, $jobData, $jobQueueName);
  90. // database 驱动时,返回值为 1|false;redis 驱动时,返回值为 随机字符串|false
  91. //域名解析存在两条 *.dev
  92. //这里明明返回0 0 !== false 是成立的
  93. //以为一直成功 结果根本没插入数据库
  94. //既然能执行到这里的代码 为何无法插入数据库 返回0
  95. if ($isPushed !== false) {
  96. echo date('Y-m-d H:i:s') . " a new Hello Job is Pushed to the MQ" . "<br>";
  97. } else {
  98. echo 'Oops, something went wrong.';
  99. }
  100. echo "<br/>";
  101. }
  102. }else{
  103. echo "不在任务时间段内".$h;
  104. }
  105. }
  106. /*
  107. * 百度提交api
  108. * 主动推送
  109. * 晚上12点执行
  110. * https://queue.dev.zx2049.com/index/Cms/baiduSearchApi
  111. */
  112. public function baiduSearchApi(){
  113. $num = 1;
  114. $area = Db::connect('db1')
  115. ->table('ey_citysite')
  116. ->where(['level' => 1, 'is_open' => 1, 'status' => 1])
  117. //->limit(1)
  118. ->select();
  119. //dump($area);
  120. $area[] = [
  121. 'id' => 10000,
  122. 'name' => '主站',
  123. 'domain' => 'www'
  124. ];
  125. //dump($area);
  126. $arr = [];
  127. foreach ($area as $k => $v) {
  128. //31区域(30+1-www) 山东关闭, 11个无法添加站点 , 剩余19个
  129. //'yunnan','guizhou','zhongqing','sichuan' 这4个未添加站点
  130. $pl = ['sx','xinjiangweiwuer','ningxia','qinghai','gansu','xicangzizhi','hainan','heilongjiang','jilin','liaoning','namenggu','yunnan','guizhou','zhongqing','sichuan'];
  131. //echo $v['id'].'<br/>';
  132. if(in_array($v['domain'], $pl)){
  133. //echo "---------------------<br/>";
  134. echo $num.'-'.$v['domain']."<font style='color:red;'>不存在站点,无法添加</font>!<br/>";
  135. //echo "---------------------<br/>";
  136. $num++;
  137. }else{
  138. $list = Db::connect('console')->table('yzn_task')
  139. ->where('diqu','=',$v['id'])
  140. ->where('is_run','=',1)
  141. ->where('is_ts','=',0)
  142. ->limit(1)
  143. ->select();
  144. foreach ($list as $in => $row){
  145. //echo $row['link'].'<br/>';
  146. $arr[$v['id']]['list'][] = $row['link'];
  147. $arr[$v['id']]['ids'][] = $row['id'];
  148. }
  149. $arr[$v['id']]['domain'] = $v['domain'];
  150. if(empty($arr[$v['id']]['list'])){
  151. //echo "---------------------<br/>";
  152. echo $num.'-'.$v['domain']."<font style='color:green;'>获取执行记录为空,无最新记录!</font><br/>";
  153. $num++;
  154. //echo "---------------------<br/>";
  155. }
  156. }
  157. }
  158. foreach ($arr as $k => $v){
  159. //查询今日已发了几条
  160. $count = Db::connect('console')->table('yzn_task')->where([
  161. 'diqu' => $k,
  162. 'is_run' => 1,
  163. 'is_ts' => 1,
  164. ])
  165. ->whereTime('ts_time', 'today') //推送时间
  166. ->count();
  167. // dump($count); die;
  168. if($count >= 10){
  169. //超过10条
  170. //假设没有手动提交的记录 只有api提交
  171. echo $num.'-'.$v['domain']."<font style='color:blue;'>已提交达到10条记录</font><br/>";
  172. $num++;
  173. }else {
  174. $urls = array();
  175. if (!empty($v['list'])) {
  176. foreach ($v['list'] as $key => $item) {
  177. $urls[] = $item;
  178. }
  179. /* dump($urls);
  180. dump($v['domain']);
  181. dump($v['ids']);*/
  182. if (!empty($urls)) {
  183. $api = 'http://data.zz.baidu.com/urls?site=https://' . $v['domain'] . '.gdzcfw.com&token=Jc7Qp1sfpQYGcaqY';
  184. $ch = curl_init();
  185. $options = array(
  186. CURLOPT_URL => $api,
  187. CURLOPT_POST => true,
  188. CURLOPT_RETURNTRANSFER => true,
  189. CURLOPT_POSTFIELDS => implode("\n", $urls),
  190. CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
  191. );
  192. curl_setopt_array($ch, $options);
  193. $result_s = curl_exec($ch);
  194. $result = json_decode($result_s, true);
  195. if (isset($result['success']) && (int)$result['success'] > 0) {
  196. //推送成功 按站点批量提交
  197. Db::connect('console')->table('yzn_task')->where('id', 'in', $v['ids'])->update(['is_ts' => 1, 'ts_time' => time()]);
  198. //插入日志
  199. $insert_data = [
  200. 'catid' => 11,
  201. 'title' => '域名' . $v['domain'] . '提交情况-本次条数(' . $result['success'] . ')-剩余('.$result['remain'].')',
  202. 'content' => json_encode($arr) . '<br>百度原始响应结果:<br/>' . $result_s,
  203. 'create_time' => time(),
  204. 'update_time' => time(),
  205. 'status' => 1,
  206. 'site_id' => 1,
  207. 'cid' => 0,
  208. ];
  209. Db::connect('console')->table('yzn_log')->insert($insert_data);
  210. echo $num.'-'.$insert_data['title']."<br/>";
  211. $num++;
  212. } else {
  213. $insert_data = [
  214. 'catid' => 11,
  215. 'title' => '域名' . $v['domain'] . '提交情况(失败)',
  216. 'content' => $result_s,
  217. 'create_time' => time(),
  218. 'update_time' => time(),
  219. 'status' => 0, //标识 失败
  220. 'site_id' => 1,
  221. 'cid' => 0,
  222. ];
  223. Db::connect('console')->table('yzn_log')->insert($insert_data);
  224. echo $num.'-'.$insert_data['title']."<br/>";
  225. $num++;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. //dump($arr);
  232. }
  233. }