/** * stops a timer */ public static function stop($tid) { if (!($tid = VarUid::sanitize($tid, $err))) { return false; } $filter = "task_id='{$tid}' AND stop='0000-00-00 00:00:00'"; $obj = new TimerModel(); $obj->connectDb(); if (!$obj->load($filter)) { return false; } $obj->set('stop', APP_SQL_NOW); $obj->set('spent', strtotime($obj->get('stop')) - strtotime($obj->get('start'))); $obj->fields('stop,spent'); return $obj->update($filter); }
public function pushByDb() { $perpage = $this->config['max_length'] - $this->count(); if (1 > $perpage) { return; } $taskPaginator = TimerModel::getTasks(1, $perpage, ['exec_time' => 'up', 'id' => 'up']); $tasks = $taskPaginator->items; // echo "get task $taskPaginator->total_items 个".__LINE__.PHP_EOL; // echo " pushdb ".json_encode($tasks).__LINE__.PHP_EOL; if (!$tasks) { echo "数据库无数据" . PHP_EOL; return; } foreach ($tasks as $v) { // echo "this->q 入队 ".__LINE__.json_encode($v).PHP_EOL; $this->insert($v); } $this->log->info("数据库加载完毕:" . $this->count() . "条"); }
/** * change a task status (start, stop, close) or save a new one * will only update the timer panel and reload the task list * (method called by ajax request only) */ public function timerReaction() { $this->jsCode = ''; if ($id = $this->fc->getReqVar('id')) { // start / stop timer $obj = new TaskSummary(); $obj->connectDb(); $obj->setUid($id); $obj->load(); // what action then ? $action = $this->fc->chkReqVar('pause,resume,start,stop,close'); FC::log_debug('loaded task ID=' . $obj->getUid()); switch ($action) { case 'pause': // try to pause if ($this->current && $this->current->getUid() == $id) { // ok, task is actually running TimerModel::stop($id); $this->current->set('stop', APP_SQL_NOW); $this->jsCode .= "clockreport('{$cid}');clockstatus('paused');"; } else { // nope, requested task is not running, show error $this->jsCode = "alert('" . TR::get('error', 'action_failed') . "')"; FC::log_debug('error trying to pause non running task'); } break; case 'resume': case 'start': TimerModel::start($id); $this->current = TaskSummary::loadCurrent(); $this->jsCode = "clockstart();"; break; case 'stop': if (TimerModel::stop($id)) { $this->jsCode = "clockstatus();"; } else { $this->jsCode = "alert('" . TR::get('error', 'action_failed') . "');"; } $this->current = false; break; case 'close': if (TimerModel::stop($id)) { $this->jsCode = "clockstatus();"; $this->current->updateStatus(1); // mark as done $this->current = false; } break; } } else { if ($title = $this->fc->getReqVar('title')) { // creating a new task ? $obj = TaskModel::parse($title, $def, $dte); $obj->connectDb(); if ($this->fc->getReqVar('start')) { $obj->set('deadline', APP_SQL_TODAY); } if ($obj->check($this->switch_id)) { $obj->insert(); } if ($this->fc->chkReqVar('start')) { TimerModel::start($obj->getUid()); $this->current = TaskSummary::loadCurrent(); $this->jsCode = "clockstart();"; } else { $this->current = false; $this->jsCode = "clockstatus();"; } } } $this->jsCode .= 'reloadList();'; $this->setView('include/timer'); $this->view(); return false; }
/** * change task status */ protected function _taskStatus() { $this->current = TaskSummary::loadCurrent(); $arrReload = array(); if ($id = $this->fc->getReqVar('id')) { if ($this->current && $this->current->getUid() != $id) { // we have a running task, but it's not the requested one // so first, we need to stop it. $cid = $this->current->getUid(); TimerModel::stop($cid); // and we'll have to reload the page details later $arrReload[] = $cid; } else { $this->current = new TaskSummary(); $this->current->connectDb(); $this->current->setUid($id); $this->current->load(); } if ($this->current->getUid()) { // -TODO- check rights switch ($this->fc->getReqVar('action')) { case 'pause': // try to pause if ($this->current && $this->current->getUid() == $id) { // ok, task is actually running TimerModel::stop($id); $this->current->set('stop', APP_SQL_NOW); $this->jsCode .= ""; } else { // nope, requested task is not running, show error FC::log_debug('error trying to pause non running task'); header('HTTP/1.0 403 Forbidden'); exit; } $this->setView('iphone/inc_timer'); $this->viewRaw(); break; case 'resume': case 'start': TimerModel::start($id); $this->current = TaskSummary::loadCurrent(); $this->jsCode = "clockstart();"; $this->setView('iphone/inc_timer'); $this->viewRaw(); break; case 'stop': if (TimerModel::stop($id)) { $this->jsCode = "clockstatus();"; } else { $this->jsCode = "alert('" . TR::get('error', 'action_failed') . "');"; } $this->current = false; break; case 'close': if (TimerModel::stop($id)) { $this->jsCode = "clockstatus();"; $this->current->updateStatus(1); // mark as done $this->current = false; } break; } // need to reload newly modified task details $arrReload[] = $id; } return false; } // reaching this point means an error has occured header('HTTP/1.0 404 Not Found'); exit; }
private function execTask(TimerModel $task) { $params = json_decode($task->params); $className = $task->class; $methodName = $task->method; if (method_exists($className, $methodName)) { Utils::call_func($className, $methodName, $params); // TimerModel::deleteTask($task->id); $task->delete(); } echo "timer called " . $methodName . '计划时间:' . date('Y-m-d H:i:s', $task->exec_time) . ' 执行时间:' . date('Y-m-d H:i:s') . PHP_EOL; $this->executor = null; $this->setExecutor(); }