-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
461 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
var runCmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "test-server", | ||
Long: "", | ||
} | ||
|
||
var ( | ||
debug bool | ||
serverPort string | ||
) | ||
|
||
func init() { | ||
runCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "debug mode") | ||
runCmd.PersistentFlags().StringVarP(&serverPort, "port", "p", "8080", "server port") | ||
runCmd.AddCommand(httpServerCmd()) | ||
} | ||
|
||
func Execute() { | ||
if err := runCmd.Execute(); err != nil { | ||
fmt.Printf("cmd err: %s\n", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/myconcurrencytools/workpoolframework/example/http_example/pkg/common" | ||
"github.com/myconcurrencytools/workpoolframework/example/http_example/pkg/server" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func httpServerCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "httpServer", | ||
Short: "run http server", | ||
Long: "", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cfg := &common.ServerConfig{ | ||
Debug: debug, | ||
Port: serverPort, | ||
} | ||
// 启动http server | ||
server.HttpServer(cfg) | ||
}, | ||
} | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "github.com/myconcurrencytools/workpoolframework/example/http_example/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package common | ||
|
||
type ServerConfig struct { | ||
Debug bool | ||
Port string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package scheduler | ||
|
||
import ( | ||
"github.com/myconcurrencytools/workpoolframework/pkg/workerpool" | ||
"log" | ||
) | ||
|
||
type Scheduler struct { | ||
pool *workerpool.Pool | ||
} | ||
|
||
func NewScheduler(workerNum int) *Scheduler { | ||
s := &Scheduler{ | ||
pool: workerpool.NewPool(workerNum), | ||
} | ||
return s | ||
} | ||
|
||
func (s *Scheduler) Start() { | ||
go func() { | ||
log.Printf("start scheduler...") | ||
s.pool.RunBackground() | ||
}() | ||
} | ||
|
||
func (s *Scheduler) Stop() { | ||
s.pool.StopBackground() | ||
} | ||
|
||
func (s *Scheduler) AddTask(task workerpool.Task) { | ||
s.pool.AddTask(task) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package model | ||
|
||
type MockMap map[string]*MyTask | ||
|
||
var TaskMap MockMap | ||
|
||
func init() { | ||
TaskMap = MockMap{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package model | ||
|
||
import "fmt" | ||
|
||
const ( | ||
TaskRunning = "running" | ||
TaskFail = "fail" | ||
TaskSuccess = "success" | ||
) | ||
|
||
type MyTask struct { | ||
TaskName string `json:"taskName"` | ||
TaskType string `json:"taskType"` | ||
Input interface{} `json:"input"` | ||
f func(data interface{}) error | ||
Err error | ||
Status string | ||
} | ||
|
||
func (my *MyTask) ChooseTaskType() { | ||
if my.TaskType == "string" { | ||
my.f = func(data interface{}) error { | ||
fmt.Println("string task...", data) | ||
return nil | ||
} | ||
} else { | ||
my.f = func(data interface{}) error { | ||
fmt.Println("int task...", data) | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
func (my *MyTask) Execute() error { | ||
|
||
my.Status = TaskRunning | ||
|
||
if err := my.f(my.Input); err != nil { | ||
my.Err = err | ||
my.Status = TaskFail | ||
return err | ||
} | ||
my.Status = TaskSuccess | ||
return nil | ||
} | ||
|
||
func (my *MyTask) GetTaskName() string { | ||
return my.TaskName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
"github.com/myconcurrencytools/workpoolframework/example/http_example/pkg/common" | ||
"github.com/myconcurrencytools/workpoolframework/example/http_example/pkg/scheduler" | ||
"github.com/myconcurrencytools/workpoolframework/example/http_example/pkg/server/model" | ||
) | ||
|
||
func HttpServer(c *common.ServerConfig) { | ||
|
||
if !c.Debug { | ||
gin.SetMode(gin.ReleaseMode) | ||
} | ||
|
||
r := gin.New() | ||
|
||
// 启动调度器 | ||
s := scheduler.NewScheduler(6) | ||
s.Start() | ||
|
||
r.GET("/test", func(c *gin.Context) { | ||
c.String(200, "测试用") | ||
}) | ||
|
||
// 执行任务接口 | ||
/* | ||
url: http://127.0.0.1:8080/start | ||
body入参: | ||
{ | ||
"taskName": "tttt", | ||
"taskType": "int", | ||
"input": "aaaa" | ||
} | ||
*/ | ||
r.POST("/start", func(c *gin.Context) { | ||
var myTask model.MyTask | ||
if err := c.ShouldBindJSON(&myTask); err != nil { | ||
|
||
fmt.Errorf("do operation action error: %s", err) | ||
c.JSON(400, gin.H{"message": "start task error"}) | ||
return | ||
} | ||
myTask.ChooseTaskType() | ||
s.AddTask(&myTask) | ||
|
||
model.TaskMap[myTask.TaskName] = &myTask | ||
c.JSON(200, gin.H{"message": "start task success"}) | ||
}) | ||
|
||
// 查询任务状态接口 | ||
/* | ||
http://127.0.0.1:8080/task?taskName=tttt | ||
*/ | ||
r.GET("/task", func(c *gin.Context) { | ||
taskName := c.Query("taskName") | ||
my := model.TaskMap[taskName] | ||
c.JSON(200, gin.H{"message": my.Status}) | ||
}) | ||
|
||
err := r.Run(fmt.Sprintf(":%v", c.Port)) | ||
fmt.Println(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package scheduler | ||
|
||
import ( | ||
"github.com/myconcurrencytools/workpoolframework/pkg/workerpool" | ||
"log" | ||
) | ||
|
||
type Scheduler struct { | ||
pool *workerpool.Pool | ||
} | ||
|
||
func NewScheduler(workerNum int) *Scheduler { | ||
s := &Scheduler{ | ||
pool: workerpool.NewPool(workerNum), | ||
} | ||
return s | ||
} | ||
|
||
func (s *Scheduler) Start() { | ||
go func() { | ||
log.Printf("start scheduler...") | ||
s.pool.RunBackground() | ||
}() | ||
} | ||
|
||
func (s *Scheduler) Stop() { | ||
s.pool.StopBackground() | ||
} | ||
|
||
func (s *Scheduler) AddTask(task workerpool.Task) { | ||
s.pool.AddTask(task) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package scheduler | ||
|
||
import ( | ||
"fmt" | ||
"github.com/myconcurrencytools/workpoolframework/pkg/workerpool" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestScheduler(t *testing.T) { | ||
s := NewScheduler(5) | ||
|
||
s.Start() | ||
|
||
tsk := workerpool.NewTaskInstance("task1", "aaa", func(i interface{}) error { | ||
fmt.Println(i) | ||
return nil | ||
}) | ||
|
||
s.AddTask(tsk) | ||
|
||
<-time.After(time.Second * 60) | ||
s.Stop() | ||
} |
Oops, something went wrong.