diff --git a/.gitignore b/.gitignore index 0d330a8..c4daf32 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor/ -composer.lock \ No newline at end of file +composer.lock +.idea/ diff --git a/composer.json b/composer.json index a62cfe6..2837828 100644 --- a/composer.json +++ b/composer.json @@ -18,20 +18,20 @@ "require": { "php": ">=5.4.0", "ext-posix": "*", - "jenner/simple_fork": "1.1.2", - "monolog/monolog": "1.17.1", - "react/http": "v0.4.1", - "symfony/process": "v2.7.5" + "jenner/simple_fork": "1.1.*", + "monolog/monolog": "1.17.*", + "react/http": "v0.4.*", + "symfony/process": "v2.7.*" }, "require-dev": { - "guzzle/guzzle": "v3.9.3", + "guzzle/guzzle": "v3.9.*", "ext-redis": "*" }, "suggest": { - "ext-libevent": ">=0.1.0", + "ext-libevent": "0.1.*", "ext-event": "~1.0", "ext-libev": "*", - "guzzle/guzzle": "v3.9.3", + "guzzle/guzzle": "v3.9.*", "ext-redis": "*" }, "autoload": { diff --git a/src/Jenner/Crontab/Daemon.php b/src/Jenner/Crontab/Daemon.php index e62a9dd..916d0a6 100644 --- a/src/Jenner/Crontab/Daemon.php +++ b/src/Jenner/Crontab/Daemon.php @@ -62,18 +62,6 @@ public function start() $crontab = $this->createCrontab(); $loop = Factory::create(); - // add task loader timer is exists. - if (!empty($this->task_loader) && is_callable($this->task_loader)) { - $loop->addPeriodicTimer(60, function () { - $start_time = time(); - $this->task_loader = call_user_func($this->task_loader); - $execution_time = time() - $start_time; - if ($execution_time > 60) { - $this->logger->warning("task loader's execution time is more than 60 seconds."); - } - }); - } - // add periodic timer $loop->addPeriodicTimer(60, function () use ($crontab, $loop) { $loop->addTimer(60 - time() % 60, function () use ($crontab) { @@ -100,14 +88,6 @@ public function start() $loop->run(); } - public function registerTaskLoader($loader) - { - if (!is_callable($loader)) { - throw new \InvalidArgumentException("task loader is not callable"); - } - $this->task_loader = $loader; - } - /** * create crontab object * @@ -118,14 +98,14 @@ protected function createCrontab() $tasks = $this->formatTasks(); $missions = array(); foreach ($tasks as $task) { - if($task['out'] instanceof LoggerInterface) { + if ($task['out'] instanceof LoggerInterface) { $out = $task['out']; - }else{ + } else { $out = MissionLoggerFactory::create($task['out']); } - if($task['err'] instanceof LoggerInterface) { + if ($task['err'] instanceof LoggerInterface) { $err = $task['err']; - }else{ + } else { $err = MissionLoggerFactory::create($task['err']); } @@ -164,18 +144,27 @@ protected function formatTasks() /** * @param $tasks */ - public function setTasks($tasks) + public function addTasks($tasks) { $must = array('name', 'cmd', 'time'); foreach ($tasks as $task) { - foreach ($must as $key) { - if (!array_key_exists($key, $task)) { - $message = "task must have a {$key} value"; - throw new \InvalidArgumentException($message); - } - } + $this->addTask($task); + } + } - $this->tasks[$task['name']] = $task; + /** + * @param $task + */ + public function addTask($task) + { + $must = array('name', 'cmd', 'time'); + foreach ($must as $key) { + if (!array_key_exists($key, $task)) { + $message = "task must have a {$key} value"; + throw new \InvalidArgumentException($message); + } } + + $this->tasks[$task['name']] = $task; } } \ No newline at end of file diff --git a/src/Jenner/Crontab/Mission.php b/src/Jenner/Crontab/Mission.php index be208e8..293d38a 100644 --- a/src/Jenner/Crontab/Mission.php +++ b/src/Jenner/Crontab/Mission.php @@ -95,109 +95,129 @@ public function __construct( } /** - * get or set name - * - * @param null $name * @return string */ - public function name($name = null) + public function getName() { - if (!is_null($name)) { - $this->name = $name; - } else { - return $this->name; - } + return $this->name; + } + + /** + * @param string $name + */ + public function setName($name) + { + $this->name = $name; } /** - * get or set cmd - * - * @param null $cmd * @return string */ - public function cmd($cmd = null) + public function getCmd() { - if (!is_null($cmd)) { - $this->cmd = $cmd; - } else { - return $this->cmd; - } + return $this->cmd; } + /** + * @param string $cmd + */ + public function setCmd($cmd) + { + $this->cmd = $cmd; + } /** - * get or set time - * - * @param null $time - * @return null + * @return LoggerInterface */ - public function time($time = null) + public function getOut() { - if (!is_null($time)) { - $this->time = $time; - } else { - return $this->time; - } + return $this->out; } /** - * get or set out - * * @param LoggerInterface $out - * @return null|string */ - public function out(LoggerInterface $out = null) + public function setOut(LoggerInterface $out) { - if (!is_null($out)) { - $this->out = $out; - } else { - return $this->out; - } + $this->out = $out; + } + + /** + * @return LoggerInterface + */ + public function getErr() + { + return $this->err; } /** - * get or set err - * * @param LoggerInterface $err - * @return null|string */ - public function err(LoggerInterface $err = null) + public function setErr(LoggerInterface $err) { - if (!is_null($err)) { - $this->err = $err; - } else { - return $this->err; - } + $this->err = $err; } /** - * get or set user - * - * @param null $user - * @return null|string + * @return string */ - public function user($user = null) + public function getUser() { - if (!is_null($user)) { - $this->user = $user; - } else { - return $this->user; - } + return $this->user; + } + + /** + * @param string $user + */ + public function setUser($user) + { + $this->user = $user; + } + + /** + * @return string + */ + public function getGroup() + { + return $this->group; + } + + /** + * @param string $group + */ + public function setGroup($group) + { + $this->group = $group; } + /** + * @return string + */ + public function getComment() + { + return $this->comment; + } /** - * get or set group + * @param string $comment + */ + public function setComment($comment) + { + $this->comment = $comment; + } + + /** + * get or set err * - * @param null $group + * @param LoggerInterface $err * @return null|string */ - public function group($group = null) + public function err(LoggerInterface $err = null) { - if (!is_null($group)) { - $this->group = $group; + if (!is_null($err)) { + $this->err = $err; } else { - return $this->group; + return $this->err; } } @@ -209,12 +229,28 @@ public function group($group = null) */ public function needRun($time) { - if ($time - CrontabParse::parse($this->time(), $time) == 0) { + if ($time - CrontabParse::parse($this->getTime(), $time) == 0) { return true; } return false; } + /** + * @return string + */ + public function getTime() + { + return $this->time; + } + + /** + * @param string $time + */ + public function setTime($time) + { + $this->time = $time; + } + /** * @return array */ @@ -258,7 +294,7 @@ protected function setUserAndGroup() { if (!is_null($this->user)) { if (!is_null($this->group)) { - $group_info = posix_getgrnam($this->user); + $group_info = posix_getgrnam($this->group); $group_id = $group_info['gid']; if (!posix_setgid($group_id)) { throw new \RuntimeException("set group failed");