context取消任务demo

一个基于 gin 的demo ,支持

创建任务,

停止(取消)任务 -》基于 context cancle,

查询任务

传递任务id  基于 context value

 

package main

import (
	"fmt"
	"math/rand"
	"strconv"

	"context"
	"time"

	"github.com/gin-gonic/gin"
)

type ReqTask struct {
	TaskID string
}

type Task struct {
	status string
	cancel context.CancelFunc
}

func generate_taskid() int {
	rand.Seed(time.Now().UnixNano())
	return rand.Intn(100000)

}

func runtask(ctx context.Context) {
	var tmptaskid interface{}
	if tmptaskid = ctx.Value("TASKID"); tmptaskid != nil {
		fmt.Println("found value:", tmptaskid)
	}
	var taskid = fmt.Sprintf("%v", tmptaskid)
	fmt.Println(taskid)

	for {
		select {
		default:
			fmt.Printf("Task %s is running \n", taskid)
			time.Sleep(time.Duration(1) * time.Second)
		case <-ctx.Done():
			fmt.Printf("Task %s is stoped \n", taskid)
			return
		}
	}
}

func main() {
	r := gin.Default()
	alltaskinfo := make(map[string]*Task)

	r.POST("/startask", func(c *gin.Context) {
		var (
			task Task
			ctx  context.Context
		)
		taskid := strconv.Itoa(generate_taskid())
		task.status = "running"
		ctx, task.cancel = context.WithCancel(context.Background())
		ctx = context.WithValue(ctx, "TASKID", taskid)

		alltaskinfo[taskid] = &task
		go runtask(ctx)
		c.JSON(200, gin.H{
			"message": "task " + taskid + " started",
		})
		return
	})

	r.POST("/querytask", func(c *gin.Context) {
		var req ReqTask
		if err := c.ShouldBind(&req); err != nil {
			c.JSON(500, gin.H{"error": err.Error()})
			return
		}
		taskid := req.TaskID
		task, ok := alltaskinfo[taskid]
		if ok {
			c.JSON(200, gin.H{
				"message": "task " + taskid + " status is " + task.status,
			})
		} else {
			c.JSON(404, gin.H{
				"message": "task " + taskid + " not found",
			})
		}
	})

	r.POST("/stoptask", func(c *gin.Context) {
		var req ReqTask
		if err := c.ShouldBind(&req); err != nil {
			c.JSON(500, gin.H{"error": err.Error()})
			return
		}
		taskid := req.TaskID
		task, ok := alltaskinfo[taskid]
		if ok {
			task.cancel()
			task.status = "stoped"
			c.JSON(200, gin.H{
				"message": "task " + taskid + " stoped",
			})
		} else {
			c.JSON(404, gin.H{
				"message": "task " + taskid + " not found",
			})
		}

	})

	r.Run(":8081") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

  


import ("fmt""math/rand""strconv"
"context""time"
"github.com/gin-gonic/gin")
type ReqTask struct {TaskID string}
type Task struct {status stringcancel context.CancelFunc}
func generate_taskid() int {rand.Seed(time.Now().UnixNano())return rand.Intn(100000)
}
func runtask(ctx context.Context) {var tmptaskid interface{}if tmptaskid = ctx.Value("TASKID"); tmptaskid != nil {fmt.Println("found value:", tmptaskid)}var taskid = fmt.Sprintf("%v", tmptaskid)fmt.Println(taskid)
for {select {default:fmt.Printf("Task %s is running \n", taskid)time.Sleep(time.Duration(1) * time.Second)case <-ctx.Done():fmt.Printf("Task %s is stoped \n", taskid)return}}}
func main() {r := gin.Default()alltaskinfo := make(map[string]*Task)
r.POST("/startask", func(c *gin.Context) {var (task Taskctx  context.Context)taskid := strconv.Itoa(generate_taskid())task.status = "running"ctx, task.cancel = context.WithCancel(context.Background())ctx = context.WithValue(ctx, "TASKID", taskid)
alltaskinfo[taskid] = &taskgo runtask(ctx)c.JSON(200, gin.H{"message": "task " + taskid + " started",})return})
r.POST("/querytask", func(c *gin.Context) {var req ReqTaskif err := c.ShouldBind(&req); err != nil {c.JSON(500, gin.H{"error": err.Error()})return}taskid := req.TaskIDtask, ok := alltaskinfo[taskid]if ok {c.JSON(200, gin.H{"message": "task " + taskid + " status is " + task.status,})} else {c.JSON(404, gin.H{"message": "task " + taskid + " not found",})}})
r.POST("/stoptask", func(c *gin.Context) {var req ReqTaskif err := c.ShouldBind(&req); err != nil {c.JSON(500, gin.H{"error": err.Error()})return}taskid := req.TaskIDtask, ok := alltaskinfo[taskid]if ok {task.cancel()task.status = "stoped"c.JSON(200, gin.H{"message": "task " + taskid + " stoped",})} else {c.JSON(404, gin.H{"message": "task " + taskid + " not found",})}
})
r.Run(":8081") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")}
 

posted on 2022-07-10 21:58  思此狂  阅读(127)  评论(0编辑  收藏  举报

导航