| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\task;
- use MGModule\DNSManager2\mgLibs\custom\manager\LogHelper;
- use MGModule\DNSManager2\models\custom\task;
- use MGModule\DNSManager2\mgLibs\custom\TaskManager;
- use MGModule\DNSManager2\mgLibs\custom\helpers\TimeDiffHelper;
- abstract class TaskAbstract
- {
- /** @var \MGModule\DNSManager2\models\custom\task\Task */
- protected $task;
- /** @var TaskAbstract */
- protected $parent;
- private $method;
-
- protected $abort_after_repeats = array();
- protected $force = false;
-
- protected $taskTypeCode;
- public function mainDescription()
- {
- return 'Task';
- }
- public function __construct($task, $method, $parent = false) {
- $this->method = $method;
- $this->task = $task;
-
- if($parent !== false) {
- $this->parent = $parent;
- }
-
- if($this->task->parentid && $parent === false) {
- $this->parent = TaskManager::getTaskObjectByID($this->task->parentid);
- }
- }
-
- public function description() {
- $method = $this->method . 'Description';
- try {
- $desc = method_exists($this, $method)?$this->$method():$this->task->name;
- } catch (\Exception $e) {
- $desc = $this->task->name;
- }
- return $desc;
- }
-
- public function run($force = false) {
- $this->force = $force;
- // if(in_array($this->task->status, array(task\TaskStatusEnum::ABORTED, task\TaskStatusEnum::CANCELED, task\TaskStatusEnum::FINISHED, task\TaskStatusEnum::ERROR)))
- // return ;
-
- $after = !isset($this->abort_after_repeats[$this->method])?3:$this->abort_after_repeats[$this->method];
- if($this->task->repeats + 1 > $after && $after != 0) {
- $this->setStatus(task\TaskStatusEnum::ABORTED);
- return ;
- }
-
- try { //TODO: tutaj albo w cronie pomyśleć
- $ret = $this->{$this->method}($this->task->params);
-
- if($ret !== true) {
- $this->task->lastrun = date('Y-m-d H:i:s');
- $this->task->repeats++;
- $this->task->save();
- }
- } catch (\Exception $e) {
- $this->setStatus(task\TaskStatusEnum::ERROR);
- $this->addResult(array('error' => $e->getMessage()));
- LogHelper::addFailLog($this->mainDescription(), $e->getMessage());
- }
- }
-
- public function runAfter($minutes = 0, $hours = 0, $days = 0) {
- $this->task->nextrun = date('Y-m-d H:i:s', strtotime("+$minutes minutes $hours hours $days days "));
- $this->task->save();
- }
-
- protected function diffFromLastRun() {
- return TimeDiffHelper::diff($this->task->lastrun?:'1969-07-20');
- }
-
- public function getStatus() {
- return $this->task->status;
- }
-
- protected function setStatus($status) {
- $this->task->status = $status;
- $this->task->save();
- }
-
-
- public function setFinishedStatusOnCronRun( $taskname, $cronLimit )
- {
- $allStartChildsCount = $this->getChildsCount($taskname, task\TaskStatusEnum::START);
- $allChildsCount = $this->getChildsCount($taskname);
- if( $allChildsCount > 0 && $allStartChildsCount < $cronLimit)
- {
- $this->setStatus(task\TaskStatusEnum::FINISHED);
- }
- }
-
-
- public function getParams($key = false) {
- return $key===false?$this->task->params:$this->task->params[$key];
- }
-
- /** @return TaskAbstract */
- public function getParent() {
- return $this->parent;
- }
-
- /** @return TaskAbstract */
- public function addChild($name, $params = array(), $clientId = false) {
- $name = $this->parseName($name);
- return TaskManager::getTaskObject(TaskManager::addTask($name, $params, $this->task->id, $clientId), $this);
- }
-
- public function getChilds() {
- return TaskManager::getXTasksObjects(0, false, $this->task->id, false);
- }
-
- public function getXChilds($x, $name = false, $status = task\TaskStatusEnum::IN_PROGRESS) {
- $out = array();
- foreach(TaskManager::getXTasks($x, $this->parseName($name), $this->task->id, $status) as $task)
- $out[] = TaskManager::getTaskObject($task, $this);
- return $out;
- }
-
-
- public function getChildsCount( $name = false, $status = false )
- {
- return TaskManager::countTasks($this->parseName($name), $this->task->id, $status);
- }
-
-
- final protected function parseName($name) {
- $class = get_class($this);
- if ($pos = strrpos($class, '\\'))
- $class = substr($class, $pos + 1);
- return $name!==false?$class . ':' . $name:false;
- }
- protected function addResult(array $data = array()) {
- TaskManager::addTaskResult($this->task->id, $data);
- }
-
- public function getResults($first = false) {
- $results = TaskManager::getTaskResults($this->task->id);
- return $first === false?$results:array_pop($results);
- }
-
- public function remove() {
- foreach($this->getChilds() as $child)
- $child->remove();
- foreach($this->getResults() as $result)
- $result->delete();
-
- $this->task->delete();
- }
-
- public function obj() {
- return $this->task;
- }
-
- public function isInCliMode()
- {
- $allowedModes = array('cli', 'cgi-fcgi');
-
- if(in_array(php_sapi_name(), $allowedModes))
- {
- return true;
- }
- return false;
- }
-
- public function getTaskTypeCode()
- {
- return $this->taskTypeCode;
- }
-
-
-
- public function isReadyToRun( $period )
- {
- if(($this->diffFromLastRun()->minutes < $period || $period == 0) && !$this->force)
- {
- return false;
- }
- return true;
- }
- }
|