Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
b9fb88f5
authored
2013-11-20 23:53:54 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
add test for task
1 parent
54185df4
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
77 additions
and
18 deletions
admin.go
toolbox/task.go
toolbox/task_test.go
admin.go
View file @
b9fb88f
...
...
@@ -65,6 +65,9 @@ func (admin *AdminApp) Route(pattern string, f http.HandlerFunc) {
}
func
(
admin
*
AdminApp
)
Run
()
{
if
len
(
toolbox
.
AdminTaskList
)
>
0
{
toolbox
.
StartTask
()
}
addr
:=
AdminHttpAddr
if
AdminHttpPort
!=
0
{
...
...
toolbox/task.go
View file @
b9fb88f
...
...
@@ -126,6 +126,13 @@ func (tk *Task) SetNext(now time.Time) {
func
(
tk
*
Task
)
GetNext
()
time
.
Time
{
return
tk
.
Next
}
func
(
tk
*
Task
)
SetPrev
(
now
time
.
Time
)
{
tk
.
Prev
=
now
}
func
(
tk
*
Task
)
GetPrev
()
time
.
Time
{
return
tk
.
Prev
}
//前6个字段分别表示:
// 秒钟:0-59
...
...
@@ -141,24 +148,24 @@ func (tk *Task) GetNext() time.Time {
// -:表示一个段,如第三端里: 1-5,就表示1到5点
// /n : 表示每个n的单位执行一次,如第三段里,*/1, 就表示每隔1个小时执行一次命令。也可以写成1-23/1.
/////////////////////////////////////////////////////////
//
*
/30 * * * * * 每30秒 执行
//
*
43 21 * * * 21:43 执行
//
*
15 05 * * * 05:15 执行
//
*
0 17 * * * 17:00 执行
//
*
0 17 * * 1 每周一的 17:00 执行
//
*
0,10 17 * * 0,2,3 每周日,周二,周三的 17:00和 17:10 执行
//
*
0-10 17 1 * * 毎月1日从 17:00到7:10 毎隔1分钟 执行
//
*
0 0 1,15 * 1 毎月1日和 15日和 一日的 0:00 执行
//
*
42 4 1 * * 毎月1日的 4:42分 执行
//
*
0 21 * * 1-6 周一到周六 21:00 执行
//
*
0,10,20,30,40,50 * * * * 每隔10分 执行
//
*
*/10 * * * * 每隔10分 执行
//
*
* 1 * * * 从1:0到1:59 每隔1分钟 执行
//
*
0 1 * * * 1:00 执行
//
*
0 */1 * * * 毎时0分 每隔1小时 执行
//
*
0 * * * * 毎时0分 每隔1小时 执行
//
*
2 8-20/3 * * * 8:02,11:02,14:02,17:02,20:02 执行
//
*
30 5 1,15 * * 1日 和 15日的 5:30 执行
//
0
/30 * * * * * 每30秒 执行
//
0
43 21 * * * 21:43 执行
//
0
15 05 * * * 05:15 执行
//
0
0 17 * * * 17:00 执行
//
0
0 17 * * 1 每周一的 17:00 执行
//
0
0,10 17 * * 0,2,3 每周日,周二,周三的 17:00和 17:10 执行
//
0
0-10 17 1 * * 毎月1日从 17:00到7:10 毎隔1分钟 执行
//
0
0 0 1,15 * 1 毎月1日和 15日和 一日的 0:00 执行
//
0
42 4 1 * * 毎月1日的 4:42分 执行
//
0
0 21 * * 1-6 周一到周六 21:00 执行
//
0
0,10,20,30,40,50 * * * * 每隔10分 执行
//
0
*/10 * * * * 每隔10分 执行
//
0
* 1 * * * 从1:0到1:59 每隔1分钟 执行
//
0
0 1 * * * 1:00 执行
//
0
0 */1 * * * 毎时0分 每隔1小时 执行
//
0
0 * * * * 毎时0分 每隔1小时 执行
//
0
2 8-20/3 * * * 8:02,11:02,14:02,17:02,20:02 执行
//
0
30 5 1,15 * * 1日 和 15日的 5:30 执行
func
(
t
*
Task
)
SetCron
(
spec
string
)
{
t
.
Spec
=
t
.
parse
(
spec
)
}
...
...
@@ -561,4 +568,5 @@ func all(r bounds) uint64 {
func
init
()
{
AdminTaskList
=
make
(
map
[
string
]
Tasker
)
stop
=
make
(
chan
bool
)
}
...
...
toolbox/task_test.go
View file @
b9fb88f
package
toolbox
import
(
"fmt"
"sync"
"testing"
"time"
)
func
TestParse
(
t
*
testing
.
T
)
{
tk
:=
NewTask
(
"taska"
,
"0/30 * * * * *"
,
func
()
error
{
fmt
.
Println
(
"hello world"
);
return
nil
})
err
:=
tk
.
Run
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
AddTask
(
"taska"
,
tk
)
StartTask
()
time
.
Sleep
(
6
*
time
.
Second
)
StopTask
()
}
func
TestSpec
(
t
*
testing
.
T
)
{
wg
:=
&
sync
.
WaitGroup
{}
wg
.
Add
(
2
)
tk1
:=
NewTask
(
"tk1"
,
"0 12 * * * *"
,
func
()
error
{
fmt
.
Println
(
"tk1"
);
return
nil
})
tk2
:=
NewTask
(
"tk2"
,
"0,10,20 * * * * *"
,
func
()
error
{
fmt
.
Println
(
"tk2"
);
wg
.
Done
();
return
nil
})
tk3
:=
NewTask
(
"tk3"
,
"0 10 * * * *"
,
func
()
error
{
fmt
.
Println
(
"tk3"
);
wg
.
Done
();
return
nil
})
AddTask
(
"tk1"
,
tk1
)
AddTask
(
"tk2"
,
tk2
)
AddTask
(
"tk3"
,
tk3
)
StartTask
()
defer
StopTask
()
select
{
case
<-
time
.
After
(
200
*
time
.
Second
)
:
t
.
FailNow
()
case
<-
wait
(
wg
)
:
}
}
func
wait
(
wg
*
sync
.
WaitGroup
)
chan
bool
{
ch
:=
make
(
chan
bool
)
go
func
()
{
wg
.
Wait
()
ch
<-
true
}()
return
ch
}
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment