taskModel.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. class taskClassModel extends Model
  3. {
  4. /**
  5. * 读取计划任务运行列表
  6. */
  7. public function getrunlist($dt='', $lx=0, $ntime=0)
  8. {
  9. if($dt=='')$dt = $this->rock->date;
  10. $fields = '`id`,`url`,`type`,`time`';
  11. if($lx==1)$fields.=',`name`';
  12. $rows = $this->getrows('`status`=1 order by `sort`', $fields);
  13. $runa = array();
  14. $sdts = strtotime($dt);
  15. $edts = strtotime($dt.' 23:59:59');
  16. if($ntime==0)$ntime = time();
  17. $ntime = $ntime-20;//稍微减一下防止出现跳过的
  18. $brows = array();
  19. $dtobj = c('date');
  20. $w = (int)date('w', $sdts);if($w==0)$w=7;//星期7
  21. foreach($rows as $k=>$rs){
  22. $ate = explode(',', $rs['type']);
  23. $ati = explode(',', $rs['time']);
  24. if(count($ate)!=count($ati))continue;
  25. $len = count($ate);
  26. $rs['adminid'] = 1;
  27. $rs['atype'] = 'runurl';
  28. $rs['urllu'] = $rs['url'];
  29. $rs['url'] = $this->showgeurl($rs['url'],$rs['id'], $lx);
  30. for($i=0;$i<$len;$i++){
  31. $rs['type'] = $ate[$i];
  32. $rs['time'] = $ati[$i];
  33. $brows[] = $rs;
  34. }
  35. }
  36. foreach($brows as $k=>$rs){
  37. $type = $rs['type'];
  38. $atime = $rs['time'];
  39. $jg = (int)str_replace(array('d','i','h','m','w'),array('','','','',''), $type);
  40. if($jg==0)$jg=1;
  41. $type = str_replace($jg,'', $type);
  42. $jgs = $jg; if($jg<10)$jgs = '0'.$jg.'';
  43. $time = '';
  44. //每天
  45. if($type=='d'){
  46. $time = $dt.' '.$rs['time'];
  47. $rs['runtimes'] = $time;
  48. $rs['runtime'] = strtotime($time);
  49. $runa[] = $rs;
  50. }
  51. //分钟
  52. if($type=='i'){
  53. $ges = $jg*60;
  54. for($i=$sdts;$i<=$edts;$i=$i+$ges){
  55. $rs['runtimes'] = date('Y-m-d H:i:s', $i);
  56. $rs['runtime'] = $i;
  57. $runa[] = $rs;
  58. }
  59. }
  60. //小时
  61. if($type=='h'){
  62. for($i=0;$i<=23;$i=$i+$jg){
  63. $time = date('Y-m-d H:'.$atime.'', $sdts+$i*3600);
  64. $rs['runtimes'] = $time;
  65. $rs['runtime'] = strtotime($time);
  66. $runa[] = $rs;
  67. }
  68. }
  69. //每月
  70. if($type=='m'){
  71. $time = date('Y-m-'.$atime.'');
  72. $rs['runtimes'] = $time;
  73. $rs['runtime'] = strtotime($time);
  74. $runa[] = $rs;
  75. }
  76. //周
  77. if($type=='w' && $jg==$w){
  78. $time = date('Y-m-d '.$atime.'');
  79. $rs['runtimes'] = $time;
  80. $rs['runtime'] = strtotime($time);
  81. $runa[] = $rs;
  82. }
  83. //每年
  84. if($type=='y'){
  85. $time = date('Y-'.$atime.'');
  86. $rs['runtimes'] = $time;
  87. $rs['runtime'] = strtotime($time);
  88. $runa[] = $rs;
  89. }
  90. }
  91. $brun = array();
  92. foreach($runa as $k=>$rs){
  93. $_runti = $rs['runtime'];
  94. if($_runti >= $ntime && $_runti<=$edts)$brun[]=$rs;
  95. }
  96. $brun = c('array')->order($brun, 'runtime','asc');
  97. return $brun;
  98. }
  99. //$lx=2必须使用本地地址
  100. private function gettaskurl($lx=0)
  101. {
  102. $turl = getconfig('taskurl');
  103. if($turl=='' || $lx==2)$turl = getconfig('localurl', URL);
  104. return $turl;
  105. }
  106. //判断设置本地地址是否可以使用
  107. public function pdlocal($urla='')
  108. {
  109. if($urla=='')$urla= $this->gettaskurl();
  110. $url = $urla.'task.php?m=day|runt&a=getitle';
  111. if($urla != URL){
  112. if(c('curl')->getcurl($url) != TITLE)return returnerror('设置的本地地址“'.$urla.'”不能使用,请到[系统→系统工具→系统设置]下重新设置');
  113. }
  114. return returnsuccess();
  115. }
  116. //获取运行url,$lx=2必须使用本地地址
  117. private function showgeurl($url, $id, $lx=0)
  118. {
  119. if(contain($url, 'http://') || contain($url, 'https://')){
  120. }else{
  121. $aurl = explode(',', $url);
  122. $turl = $this->gettaskurl($lx);
  123. $yurl = 'task.php?m='.$aurl[0].'|runt&a='.arrvalue($aurl, 1, 'run').'&runid='.$id.'';
  124. $url = ''.$turl.''.$yurl.'';
  125. }
  126. return $url;
  127. }
  128. //基本运行 弃用
  129. public function baserun($mid=0,$gtimes='')
  130. {
  131. $where = '';
  132. if($mid>0)$where='and id='.$mid.'';
  133. $rows = $this->getrows('`status`=1 '.$where.' order by `sort`','`id`,`url`,`type`,`time`,`lastdt`');
  134. $dt = $this->rock->date;
  135. if($gtimes=='')$gtimes = date('H:i:s');
  136. $time = strtotime($dt.' '.$gtimes);
  137. $jgs = 5;
  138. foreach($rows as $k=>$rs){
  139. $ybbo = false;
  140. $stime = $this->getruntime($dt, $rs['type'], $rs['time'], $rs['lastdt']);
  141. if($stime > 0){
  142. if($time>=$stime-$jgs && $time<=$stime+$jgs){
  143. $ybbo = true;
  144. }
  145. }
  146. if($ybbo || $mid>0){
  147. $state = '2';
  148. $url = $this->showgeurl($rs['url'], $rs['id']);
  149. $cont = c('curl')->getcurl($url);
  150. if($cont=='success')$state='1';
  151. $this->update(array(
  152. 'state' => $state,
  153. 'lastdt'=> $this->rock->now,
  154. 'lastcont' => $cont
  155. ), $rs['id']);
  156. }
  157. }
  158. }
  159. private function getruntimes($dt, $type, $time, $lastdt)
  160. {
  161. $stime = 0;$sj = '';
  162. if($type=='d'){
  163. $sj = $dt.' '.$time;
  164. }
  165. if($type=='h'){
  166. }
  167. if($type=='i'){
  168. }
  169. if($sj!=''){
  170. $stime = strtotime($sj);
  171. }
  172. return $stime;
  173. }
  174. private function getruntime($dt, $type, $time, $lastdt)
  175. {
  176. $stime = 0;
  177. if(!$this->isempt($type) && !$this->isempt($time)){
  178. $atype = explode(',', $type);
  179. $atime = explode(',', $time);
  180. $len1 = count($atype);
  181. $len2 = count($atime);
  182. if($len1 == $len2){
  183. for($i=0; $i<$len1; $i++){
  184. $stime = $this->getruntimes($dt, $atype[$i], $atime[$i], $lastdt);
  185. if($stime>0)break;
  186. }
  187. }
  188. }
  189. return $stime;
  190. }
  191. private function tasklistpath($lx=0)
  192. {
  193. $str = ''.UPDIR.'/logs/tasklist.json';
  194. if($lx==1)return $str;
  195. return ''.ROOT_PATH.'/'.$str.'';
  196. }
  197. /**
  198. * 清空
  199. */
  200. public function cleartask()
  201. {
  202. @unlink($this->tasklistpath());
  203. }
  204. //读取下一个5分钟时间
  205. private function getnextfz()
  206. {
  207. $time = time();
  208. $ni = date('i', $time);
  209. $tar = array(0,5,10,15,20,25,30,35,40,45,50,55,60);
  210. $gi = 0;
  211. for($i=0;$i<count($tar)-1;$i++){
  212. $i1 = $tar[$i];
  213. $i2 = $tar[$i+1];
  214. if($ni>=$i1 && $ni<$i2){
  215. $gi = $i2;
  216. break;
  217. }
  218. }
  219. if($gi==60){
  220. $date = date('Y-m-d H:00:00', $time+600);
  221. }else{
  222. $date = date('Y-m-d H:'.$gi.':00', $time);
  223. }
  224. return strtotime($date);
  225. }
  226. //开启发送运行任务
  227. public function sendstarttask()
  228. {
  229. $turl = $this->gettaskurl();
  230. $option = m('option');
  231. $this->reimtype = $option->getval('reimservertype');
  232. //node版本
  233. if($this->reimtype=='1'){
  234. $url = ''.$turl.'task.php?m=runt&a=task';
  235. $runtime= $this->getnextfz();
  236. $reim = m('reim');
  237. if(!isempt(getconfig('phppath')) && contain($reim->serverpushurl, '127.0.0.1')){
  238. $url= 'runt,task';
  239. }
  240. $recID = $option->getval('reimrecidsystem','rockxinhu');
  241. $keynum = 'service_'.$recID.'';
  242. $len = (int)$option->getval($keynum,'0');
  243. if($len<=0){
  244. $len = strlen($url)+rand(1000,9999);
  245. $option->setval($keynum, $len);
  246. }
  247. $barr = c('rockqueue')->push($url, array('rtype'=>'queue','dwnum'=>'dev','nolog'=>'1','runtime'=>$runtime), $runtime, $len);
  248. if(!COMPANYNUM && getconfig('platdwnum')){
  249. $creaar = m('company')->getall('`iscreate`=1');
  250. foreach($creaar as $k=>$rs){
  251. $url1 = $url;
  252. if(substr($url,0,4)=='http' && !isempt($rs['yuming']))$url1 = $this->tihuanurl($url1, $rs['yuming']);
  253. $barr = c('rockqueue')->push($url1, array('dwnum'=>$rs['num'],'nolog'=>'1','runtime'=>$runtime), $runtime, $len+1+$k);
  254. }
  255. }
  256. }else{
  257. $url = ''.$turl.'task.php?m=runt&a=getlist';
  258. $barr = m('reim')->pushserver('starttask', array(
  259. 'url' => $url
  260. ));
  261. }
  262. return $barr;
  263. }
  264. private function tihuanurl($url1, $yj)
  265. {
  266. $str = str_replace('/','@',str_replace('//', '@', $url1));
  267. $urla = explode('@', $str);
  268. $nhur = $urla[1];
  269. $url1 = str_replace('/'.$nhur.'/', '/'.$yj.'/', $url1);
  270. return $url1;
  271. }
  272. /**
  273. * 开启计划任务(自己服务端)
  274. */
  275. public function starttask()
  276. {
  277. $barr = $this->sendstarttask();
  278. if($this->reimtype=='1'){
  279. $recID = m('option')->getval('reimrecidsystem','rockxinhu');
  280. c('rockqueue')->pushtype('starturl',''.$this->gettaskurl().'task.php?m=runt&a=taskget', array(
  281. 'recid' => $recID
  282. ));
  283. }
  284. $this->cleartask();
  285. return $barr;
  286. }
  287. //创建json数组
  288. public function createjson($time)
  289. {
  290. $barr = $this->getrunlist($this->rock->date, 2, $time);
  291. $this->rock->createtxt($this->tasklistpath(1), json_encode($barr));
  292. return $barr;
  293. }
  294. /**
  295. * cli 运行每5分钟运行的,运行curl的
  296. */
  297. public function runjsonlist($time)
  298. {
  299. $barr = array();
  300. $fstr = '';
  301. $fpath = $this->tasklistpath();
  302. $dt = date('Y-m-d', $time);
  303. if(file_exists($fpath)){
  304. $lastdt = date('Y-m-d H:i:s',filemtime($fpath));//最后修改的时间
  305. $editdt = date('Y-m-d H:i:s',filectime($fpath));//上次修改时间
  306. if(contain($lastdt, $dt) && contain($editdt, $dt))$fstr = @file_get_contents($fpath);
  307. }
  308. if(isempt($fstr)){
  309. $barr = $this->createjson($time);
  310. m('option')->setval('systaskrun', $this->rock->now);//记录运行时间
  311. }else{
  312. $barr = json_decode($fstr, true);
  313. }
  314. $ntime = strtotime(date('Y-m-d H:i:00', $time));
  315. $yunarr = array();
  316. foreach($barr as $k=>$rs){
  317. if($rs['runtime']==$ntime){
  318. $yunarr[] = $rs;
  319. }
  320. }
  321. return $yunarr;
  322. }
  323. //获取运行列表
  324. public function getlistrun($dt)
  325. {
  326. $brun = $this->getrunlist($dt);
  327. $this->sbarr = array();
  328. foreach($brun as $k=>$rs){
  329. $key = $this->bsdeitmes($rs['runtime'],0);
  330. unset($rs['id']);
  331. unset($rs['time']);
  332. unset($rs['type']);
  333. unset($rs['runtimes']);
  334. unset($rs['runtime']);
  335. $rs['from'] = '[recid]';
  336. $this->sbarr[$key] = $rs;
  337. }
  338. $barr = array();
  339. foreach($this->sbarr as $key=>$rs){
  340. $barr[$key] = json_encode($rs);
  341. }
  342. return $barr;
  343. }
  344. private function bsdeitmes($time, $oi)
  345. {
  346. $xu = $time+$oi;
  347. $key= '[recid]'.$xu.'';
  348. if(isset($this->sbarr[$key])){
  349. $key = $this->bsdeitmes($time, $oi+1);
  350. }
  351. return $key;
  352. }
  353. }
粤ICP备19079148号