From ba7f58b9a2988ae5d8f79a46b1a43e1ac6f1158b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A4=8D?= Date: Tue, 24 Nov 2015 17:17:52 +0800 Subject: [PATCH 1/2] Modify : add run --- keeper.go | 11 +++++++++++ templates/homepage.html | 8 +++++++- web.go | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/keeper.go b/keeper.go index ea58d10..e59cbe6 100644 --- a/keeper.go +++ b/keeper.go @@ -230,6 +230,17 @@ func (k *Keeper) DelTask(name string) error { return k.save() } +func (k *Keeper) RunTask(name string) error { + k.tkmu.Lock() + defer k.tkmu.Unlock() + delete(k.tasks, name) + if tk, ok := k.tasks[name]; ok { + tk.Run(TRIGGER_MANUAL) + return nil + } + return errors.New("Can't run tasks") +} + func (k *Keeper) PutTask(name string, t Task) error { k.tkmu.Lock() defer k.tkmu.Unlock() diff --git a/templates/homepage.html b/templates/homepage.html index 8006c33..792f190 100644 --- a/templates/homepage.html +++ b/templates/homepage.html @@ -15,6 +15,12 @@ } var top = 400; var duration = 2000; + $scope.runTask = function(taskName){ + console.log(taskName); + $http.post("/Run/",{'name':taskName}); + + return false; + }; }); @@ -82,7 +88,7 @@

-

{{current.name}} #{{current.index}}Setting

+

{{current.name}} #{{current.index}}SettingRun

diff --git a/web.go b/web.go index 58e416d..bd0f082 100644 --- a/web.go +++ b/web.go @@ -95,6 +95,20 @@ func initRoutes() { ctx.HTML(200, "settings") }) + m.Post("/Run/", func(ctx *macaron.Context) { + type T struct { + Name string + } + var t T + dec := json.NewDecoder(ctx.Req.Body().ReadCloser()) + if err := dec.Decode(&t); err != nil { + ctx.Error(500, err.Error()) + return + } + log.Println(t.Name) + keeper.RunTask(t.Name) + }) + m.Get("/api/records/:name/index/:index", func(ctx *macaron.Context) { name := ctx.Params(":name") index := ctx.ParamsInt(":index") From dc37a15a986a309a584ea6e7099de193c126e6f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A4=8D?= Date: Wed, 25 Nov 2015 14:56:11 +0800 Subject: [PATCH 2/2] add runTask --- keeper.go | 5 ++--- templates/homepage.html | 13 ++++++++++--- web.go | 9 +++++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/keeper.go b/keeper.go index e59cbe6..a806acd 100644 --- a/keeper.go +++ b/keeper.go @@ -231,9 +231,8 @@ func (k *Keeper) DelTask(name string) error { } func (k *Keeper) RunTask(name string) error { - k.tkmu.Lock() - defer k.tkmu.Unlock() - delete(k.tasks, name) + fmt.Println("==========runtask==========") + if tk, ok := k.tasks[name]; ok { tk.Run(TRIGGER_MANUAL) return nil diff --git a/templates/homepage.html b/templates/homepage.html index 792f190..c672a0b 100644 --- a/templates/homepage.html +++ b/templates/homepage.html @@ -17,8 +17,14 @@ var duration = 2000; $scope.runTask = function(taskName){ console.log(taskName); - $http.post("/Run/",{'name':taskName}); - + $http.post("/run/",{'name':taskName}) + .success(function(res){ + console.log("return"); + }) + .error(function(res){ + console.log("err"); + }); + window.location="/"+taskName return false; }; @@ -53,6 +59,7 @@

+ #{{t.index}} @@ -88,7 +95,7 @@

-

{{current.name}} #{{current.index}}SettingRun

+

{{current.name}} #{{current.index}}Setting

diff --git a/web.go b/web.go index bd0f082..b682933 100644 --- a/web.go +++ b/web.go @@ -95,7 +95,7 @@ func initRoutes() { ctx.HTML(200, "settings") }) - m.Post("/Run/", func(ctx *macaron.Context) { + m.Post("/run/", func(ctx *macaron.Context) { type T struct { Name string } @@ -106,7 +106,12 @@ func initRoutes() { return } log.Println(t.Name) - keeper.RunTask(t.Name) + if err := keeper.RunTask(t.Name); err != nil { + ctx.Error(500, err.Error()) + return + } + + ctx.JSON(200, "success") }) m.Get("/api/records/:name/index/:index", func(ctx *macaron.Context) {