runtAction.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * 计划任务用的程序
  4. */
  5. class runtAction extends ActionNot
  6. {
  7. public $runid = 0;
  8. public $queuelogid = 0;
  9. public $runrs = array();
  10. public $splitlast = 0; //距离上次提醒秒数0上次没有运行
  11. public $todoarr = array();
  12. public function initAction()
  13. {
  14. $this->display = false;
  15. ob_start(); //打开缓冲区
  16. $this->runid = (int)$this->getparams('runid','0');
  17. $this->queuelogid = (int)$this->getparams('queuelogid','0');
  18. $this->initTask($this->runid);
  19. }
  20. public function initTask($runid)
  21. {
  22. if($runid==0)return;
  23. $this->runid = $runid;
  24. $this->runrs = m('task')->getone($this->runid);
  25. if($this->runrs && !isempt($this->runrs['lastdt'])){
  26. $this->splitlast = time() - strtotime($this->runrs['lastdt']);
  27. }
  28. }
  29. public function taskAfter()
  30. {
  31. //提醒的
  32. $todoid = arrvalue($this->runrs,'todoid');
  33. if(!isempt($todoid) && $this->todoarr){
  34. $modenum = arrvalue($this->todoarr, 'modenum');
  35. $agentname = arrvalue($this->todoarr, 'agentname');
  36. $title = arrvalue($this->todoarr, 'title');
  37. $cont = arrvalue($this->todoarr, 'cont');
  38. if(!isempt($modenum)){
  39. $flow = m('flow')->initflow($modenum);
  40. $flow->push($todoid, $agentname, $cont, $title);
  41. }else{
  42. m('todo')->add($todoid, $title, $cont);
  43. }
  44. }
  45. }
  46. //判断模块是否存在开启
  47. public function moderock($num)
  48. {
  49. $to = m('flow_set')->rows("`num`='$num' and `status`=1");
  50. return ($to>0);
  51. }
  52. /**
  53. * 运行完成后判断运行状态
  54. */
  55. public function afterAction()
  56. {
  57. if($this->runid > 0){
  58. $state = 2;
  59. $cont = ob_get_contents();
  60. if(contain($cont,'success'))$state=1;
  61. m('task')->update(array(
  62. 'lastdt' => $this->rock->now,
  63. 'lastcont' => $cont,
  64. 'state' => $state
  65. ), $this->runid);
  66. $this->taskAfter();
  67. }
  68. if($this->queuelogid > 0){
  69. $cont = ob_get_contents();
  70. m('log')->update(array('result' => $cont), $this->queuelogid);
  71. }
  72. }
  73. /**
  74. * 获取cli上参数格式:-key=val
  75. */
  76. public function getparams($key, $dev='')
  77. {
  78. if(PHP_SAPI != 'cli'){
  79. return $this->get($key, $dev);
  80. }
  81. $arr = arrvalue($GLOBALS, 'argv');
  82. $sss = '';
  83. if($arr)for($i=2;$i<count($arr);$i++){
  84. $str = $arr[$i];
  85. if(!isempt($str)){
  86. $stra = explode('=', $str);
  87. if($stra[0]=='-'.$key.''){
  88. $sss = arrvalue($stra, 1);
  89. break;
  90. }
  91. }
  92. }
  93. if(isempt($sss))$sss = $dev;
  94. return $sss;
  95. }
  96. }
  97. class runtClassAction extends runtAction
  98. {
  99. public function runAction()
  100. {
  101. $mid = (int)$this->get('mid','0');
  102. m('task')->baserun($mid);
  103. echo 'success';
  104. }
  105. public function getlistAction()
  106. {
  107. $dt = $this->get('dt', $this->date);
  108. $barr = m('task')->getlistrun($dt);
  109. $this->option->setval('systaskrun', $this->now);
  110. $this->returnjson($barr);
  111. }
  112. /**
  113. * 运行定时任务用于cli模式的,建每5分钟运行一次
  114. * Linux 使用crontab php task.php runt,task
  115. * win 使用计划任务 php task.php runt,task
  116. * 也可以每5分钟访问地址:http://127.0.0.1/app/xinhu/task.php?m=runt&a=task
  117. */
  118. public function taskAction()
  119. {
  120. $runtime = $this->getparams('runtime',time());
  121. $rtype = $this->getparams('rtype'); //运行类型
  122. $dbs = m('task');
  123. if($rtype=='queue')$dbs->sendstarttask();
  124. $yunarr = $dbs->runjsonlist($runtime);
  125. $oi = $cg = $sb = 0;
  126. foreach($yunarr as $k=>$rs){
  127. $urllu = $rs['urllu'];
  128. $taskid = (int)$rs['id'];
  129. $state = 2;
  130. $cont = '';
  131. $oi++;
  132. if(substr($urllu,0,4)=='http'){
  133. $cont = c('curl')->getcurl($urllu);
  134. }else{
  135. $urla = explode(',', $urllu);
  136. $path = ''.ROOT_PATH.'/'.P.'/task/runt/'.$urla[0].'Action.php';
  137. if(file_exists($path)){
  138. $act = arrvalue($urla, 1,'run').'Action';
  139. include_once($path);
  140. $class= ''.$urla[0].'ClassAction';
  141. $obj = new $class();
  142. $obj->initTask($taskid);
  143. $cont = $obj->$act();
  144. $obj->taskAfter();
  145. }else{
  146. $cont = ''.$urla[0].'Action.php not found';
  147. }
  148. }
  149. if(contain($cont,'success')){
  150. $state = 1;
  151. $cg++;
  152. }else{
  153. $sb++;
  154. }
  155. $dbs->update(array(
  156. 'lastdt' => $this->rock->now,
  157. 'lastcont' => $cont,
  158. 'state' => $state
  159. ), $taskid);
  160. }
  161. return 'runtask('.$oi.'),success('.$cg.'),fail('.$sb.')';
  162. }
  163. //新服务端加载计划任务
  164. public function taskgetAction()
  165. {
  166. m('task')->sendstarttask();
  167. return 'taskget.'.time().'';
  168. }
  169. /**
  170. * 初始化计划任务linux
  171. * php task.php runt,taskinit
  172. */
  173. public function taskinitAction()
  174. {
  175. $str1 = 'cd '.ROOT_PATH.''.chr(10).'php '.ROOT_PATH.'/task.php runt,task';
  176. $spath= ''.UPDIR.'/cli/xinhutaskrun.sh';
  177. file_put_contents($spath, $str1);
  178. if(function_exists('exec'))exec('chmod 777 '.$spath.'');
  179. echo 'xinhu taskinit success';
  180. }
  181. }
粤ICP备19079148号