Skip to content

Commit 9c47e04

Browse files
committed
Code refactoring
1 parent 4829dd7 commit 9c47e04

File tree

4 files changed

+129
-103
lines changed

4 files changed

+129
-103
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor/
2-
composer.lock
2+
composer.lock
3+
.idea/

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818
"require": {
1919
"php": ">=5.4.0",
2020
"ext-posix": "*",
21-
"jenner/simple_fork": "1.1.2",
22-
"monolog/monolog": "1.17.1",
23-
"react/http": "v0.4.1",
24-
"symfony/process": "v2.7.5"
21+
"jenner/simple_fork": "1.1.*",
22+
"monolog/monolog": "1.17.*",
23+
"react/http": "v0.4.*",
24+
"symfony/process": "v2.7.*"
2525
},
2626
"require-dev": {
27-
"guzzle/guzzle": "v3.9.3",
27+
"guzzle/guzzle": "v3.9.*",
2828
"ext-redis": "*"
2929
},
3030
"suggest": {
31-
"ext-libevent": ">=0.1.0",
31+
"ext-libevent": "0.1.*",
3232
"ext-event": "~1.0",
3333
"ext-libev": "*",
34-
"guzzle/guzzle": "v3.9.3",
34+
"guzzle/guzzle": "v3.9.*",
3535
"ext-redis": "*"
3636
},
3737
"autoload": {

src/Jenner/Crontab/Daemon.php

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,6 @@ public function start()
6262
$crontab = $this->createCrontab();
6363
$loop = Factory::create();
6464

65-
// add task loader timer is exists.
66-
if (!empty($this->task_loader) && is_callable($this->task_loader)) {
67-
$loop->addPeriodicTimer(60, function () {
68-
$start_time = time();
69-
$this->task_loader = call_user_func($this->task_loader);
70-
$execution_time = time() - $start_time;
71-
if ($execution_time > 60) {
72-
$this->logger->warning("task loader's execution time is more than 60 seconds.");
73-
}
74-
});
75-
}
76-
7765
// add periodic timer
7866
$loop->addPeriodicTimer(60, function () use ($crontab, $loop) {
7967
$loop->addTimer(60 - time() % 60, function () use ($crontab) {
@@ -100,14 +88,6 @@ public function start()
10088
$loop->run();
10189
}
10290

103-
public function registerTaskLoader($loader)
104-
{
105-
if (!is_callable($loader)) {
106-
throw new \InvalidArgumentException("task loader is not callable");
107-
}
108-
$this->task_loader = $loader;
109-
}
110-
11191
/**
11292
* create crontab object
11393
*
@@ -118,14 +98,14 @@ protected function createCrontab()
11898
$tasks = $this->formatTasks();
11999
$missions = array();
120100
foreach ($tasks as $task) {
121-
if($task['out'] instanceof LoggerInterface) {
101+
if ($task['out'] instanceof LoggerInterface) {
122102
$out = $task['out'];
123-
}else{
103+
} else {
124104
$out = MissionLoggerFactory::create($task['out']);
125105
}
126-
if($task['err'] instanceof LoggerInterface) {
106+
if ($task['err'] instanceof LoggerInterface) {
127107
$err = $task['err'];
128-
}else{
108+
} else {
129109
$err = MissionLoggerFactory::create($task['err']);
130110
}
131111

@@ -164,18 +144,27 @@ protected function formatTasks()
164144
/**
165145
* @param $tasks
166146
*/
167-
public function setTasks($tasks)
147+
public function addTasks($tasks)
168148
{
169149
$must = array('name', 'cmd', 'time');
170150
foreach ($tasks as $task) {
171-
foreach ($must as $key) {
172-
if (!array_key_exists($key, $task)) {
173-
$message = "task must have a {$key} value";
174-
throw new \InvalidArgumentException($message);
175-
}
176-
}
151+
$this->addTask($task);
152+
}
153+
}
177154

178-
$this->tasks[$task['name']] = $task;
155+
/**
156+
* @param $task
157+
*/
158+
public function addTask($task)
159+
{
160+
$must = array('name', 'cmd', 'time');
161+
foreach ($must as $key) {
162+
if (!array_key_exists($key, $task)) {
163+
$message = "task must have a {$key} value";
164+
throw new \InvalidArgumentException($message);
165+
}
179166
}
167+
168+
$this->tasks[$task['name']] = $task;
180169
}
181170
}

src/Jenner/Crontab/Mission.php

Lines changed: 99 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -95,109 +95,129 @@ public function __construct(
9595
}
9696

9797
/**
98-
* get or set name
99-
*
100-
* @param null $name
10198
* @return string
10299
*/
103-
public function name($name = null)
100+
public function getName()
104101
{
105-
if (!is_null($name)) {
106-
$this->name = $name;
107-
} else {
108-
return $this->name;
109-
}
102+
return $this->name;
103+
}
104+
105+
/**
106+
* @param string $name
107+
*/
108+
public function setName($name)
109+
{
110+
$this->name = $name;
110111
}
111112

112113
/**
113-
* get or set cmd
114-
*
115-
* @param null $cmd
116114
* @return string
117115
*/
118-
public function cmd($cmd = null)
116+
public function getCmd()
119117
{
120-
if (!is_null($cmd)) {
121-
$this->cmd = $cmd;
122-
} else {
123-
return $this->cmd;
124-
}
118+
return $this->cmd;
125119
}
126120

121+
/**
122+
* @param string $cmd
123+
*/
124+
public function setCmd($cmd)
125+
{
126+
$this->cmd = $cmd;
127+
}
127128

128129
/**
129-
* get or set time
130-
*
131-
* @param null $time
132-
* @return null
130+
* @return LoggerInterface
133131
*/
134-
public function time($time = null)
132+
public function getOut()
135133
{
136-
if (!is_null($time)) {
137-
$this->time = $time;
138-
} else {
139-
return $this->time;
140-
}
134+
return $this->out;
141135
}
142136

143137
/**
144-
* get or set out
145-
*
146138
* @param LoggerInterface $out
147-
* @return null|string
148139
*/
149-
public function out(LoggerInterface $out = null)
140+
public function setOut(LoggerInterface $out)
150141
{
151-
if (!is_null($out)) {
152-
$this->out = $out;
153-
} else {
154-
return $this->out;
155-
}
142+
$this->out = $out;
143+
}
144+
145+
/**
146+
* @return LoggerInterface
147+
*/
148+
public function getErr()
149+
{
150+
return $this->err;
156151
}
157152

158153
/**
159-
* get or set err
160-
*
161154
* @param LoggerInterface $err
162-
* @return null|string
163155
*/
164-
public function err(LoggerInterface $err = null)
156+
public function setErr(LoggerInterface $err)
165157
{
166-
if (!is_null($err)) {
167-
$this->err = $err;
168-
} else {
169-
return $this->err;
170-
}
158+
$this->err = $err;
171159
}
172160

173161
/**
174-
* get or set user
175-
*
176-
* @param null $user
177-
* @return null|string
162+
* @return string
178163
*/
179-
public function user($user = null)
164+
public function getUser()
180165
{
181-
if (!is_null($user)) {
182-
$this->user = $user;
183-
} else {
184-
return $this->user;
185-
}
166+
return $this->user;
167+
}
168+
169+
/**
170+
* @param string $user
171+
*/
172+
public function setUser($user)
173+
{
174+
$this->user = $user;
175+
}
176+
177+
/**
178+
* @return string
179+
*/
180+
public function getGroup()
181+
{
182+
return $this->group;
183+
}
184+
185+
/**
186+
* @param string $group
187+
*/
188+
public function setGroup($group)
189+
{
190+
$this->group = $group;
186191
}
187192

193+
/**
194+
* @return string
195+
*/
196+
public function getComment()
197+
{
198+
return $this->comment;
199+
}
188200

189201
/**
190-
* get or set group
202+
* @param string $comment
203+
*/
204+
public function setComment($comment)
205+
{
206+
$this->comment = $comment;
207+
}
208+
209+
/**
210+
* get or set err
191211
*
192-
* @param null $group
212+
* @param LoggerInterface $err
193213
* @return null|string
194214
*/
195-
public function group($group = null)
215+
public function err(LoggerInterface $err = null)
196216
{
197-
if (!is_null($group)) {
198-
$this->group = $group;
217+
if (!is_null($err)) {
218+
$this->err = $err;
199219
} else {
200-
return $this->group;
220+
return $this->err;
201221
}
202222
}
203223

@@ -209,12 +229,28 @@ public function group($group = null)
209229
*/
210230
public function needRun($time)
211231
{
212-
if ($time - CrontabParse::parse($this->time(), $time) == 0) {
232+
if ($time - CrontabParse::parse($this->getTime(), $time) == 0) {
213233
return true;
214234
}
215235
return false;
216236
}
217237

238+
/**
239+
* @return string
240+
*/
241+
public function getTime()
242+
{
243+
return $this->time;
244+
}
245+
246+
/**
247+
* @param string $time
248+
*/
249+
public function setTime($time)
250+
{
251+
$this->time = $time;
252+
}
253+
218254
/**
219255
* @return array
220256
*/

0 commit comments

Comments
 (0)