Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

客户端:http 客户端 #28

Open
flycash opened this issue Sep 10, 2024 · 5 comments
Open

客户端:http 客户端 #28

flycash opened this issue Sep 10, 2024 · 5 comments

Comments

@flycash
Copy link
Contributor

flycash commented Sep 10, 2024

在我们的设计里面,调度节点可以通过 HTTP 调用触发任务。那么如果我们要求用户自己手写 HTTP 接口也是可以的。但是很多用户可能会比较不耐烦,所以我们进一步提供客户端。

这个客户端提供以下接口:

  • 注册任务。注册之后,当收到 HTTP POST 请求的时候,客户端会直接的从注册的任务里面找到,执行调度;
  • 返回状态。如果后续收到 GET 请求的,客户端会找到注册的任务,返回执行的状态
  • 停止任务。收到 DELETE 请求,客户端会停下当次执行

你需要提供的基本实现是:

type Registry struct {
    Registry(t Task)
}
// 允许用户指定路径,比如说有些用户用 /task/$name
// 有些用户使用 /aaa/bbb/ccc/$name
// name 就是某个任务的名字
type HttpClient struct {
   Registry
// 这个是返回一个 mutex,这样的话用户可以拿去自己启动,决定监听的端口
// mutex := client.HttpMutex()
// http.ListenAndServe(":8080", mutex)
    HttpMutex() http.Mutex
}

type Task interface {
    Execute()
    Stop()
    Status()
    Name()
}
@flycash
Copy link
Contributor Author

flycash commented Sep 10, 2024

task 接口是给用户用的,他们提供实现

@flycash
Copy link
Contributor Author

flycash commented Sep 10, 2024

Mutex 里面注册 POST,GET 和 DELETE 三个路由。不能使用 GIN 之类的接口。

@flycash
Copy link
Contributor Author

flycash commented Sep 10, 2024

在根目录下新建一个 client 的包,项目结构是:
ecron/client/http

@Jared-lu
Copy link
Contributor

大体上是不是这样子?
`
// 我的实现
type Registry struct {
tasks []Task
}

func (s *Registry) Register(t Task) {
s.tasks = append(s.tasks, t)
}

type Client struct {
Registry
prefix string
}

func (c *Client) HttpMutex() http.Handler {
handler := http.NewServeMux()
handler.HandleFunc("/$prefix/$TaskName", func(w http.ResponseWriter, r *http.Request) {
// Post => t.Execute()
// GET => t.Status()
// DELETE => Stop()
})
return handler
}

// 用户使用
r:=new(Registry)
r.Register(t)
client:=NewHttpClient(r)
mutex:=client.HttpMutex()
http.ListenAndServe(":8080", mutex)
`

@flycash
Copy link
Contributor Author

flycash commented Sep 14, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants