taskModel.php 9.7 KB

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