feat: add durable schedule management and multi-node crash recovery
This commit is contained in:
83
sdk/go/schedules.go
Normal file
83
sdk/go/schedules.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package orbit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// CreateSchedule 使用稳定幂等键受理一个计划;回执不代表任何一次触发已经执行。
|
||||
func (c *Client) CreateSchedule(ctx context.Context, key string, in CreateScheduleRequest) (ScheduleResult, error) {
|
||||
var out ScheduleResult
|
||||
err := c.call(ctx, "POST", "/v1/schedules", key, in, &out)
|
||||
return out, err
|
||||
}
|
||||
func (c *Client) GetSchedule(ctx context.Context, id string) (Schedule, error) {
|
||||
var out Schedule
|
||||
if !validID(id) {
|
||||
return out, errors.New("invalid schedule id")
|
||||
}
|
||||
err := c.call(ctx, "GET", "/v1/schedules/"+id, "", nil, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
// PublishSchedule 发布完整新版本。保留原 key/revision 重报,不能重置原版本的积压身份。
|
||||
func (c *Client) PublishSchedule(ctx context.Context, id, key string, in PublishScheduleRequest) (ScheduleResult, error) {
|
||||
var out ScheduleResult
|
||||
if !validID(id) {
|
||||
return out, errors.New("invalid schedule id")
|
||||
}
|
||||
err := c.call(ctx, "PATCH", "/v1/schedules/"+id, key, in, &out)
|
||||
return out, err
|
||||
}
|
||||
func (c *Client) PauseSchedule(ctx context.Context, id, key string, in ControlScheduleRequest) (ScheduleResult, error) {
|
||||
return c.controlSchedule(ctx, id, key, in, "pause")
|
||||
}
|
||||
func (c *Client) ResumeSchedule(ctx context.Context, id, key string, in ControlScheduleRequest) (ScheduleResult, error) {
|
||||
return c.controlSchedule(ctx, id, key, in, "resume")
|
||||
}
|
||||
func (c *Client) controlSchedule(ctx context.Context, id, key string, in ControlScheduleRequest, action string) (ScheduleResult, error) {
|
||||
var out ScheduleResult
|
||||
if !validID(id) {
|
||||
return out, errors.New("invalid schedule id")
|
||||
}
|
||||
err := c.call(ctx, "POST", "/v1/schedules/"+id+":"+action, key, in, &out)
|
||||
return out, err
|
||||
}
|
||||
func (c *Client) ListSchedules(ctx context.Context, queue string, in SchedulePageRequest) (SchedulePage, error) {
|
||||
var out SchedulePage
|
||||
if !validID(queue) {
|
||||
return out, errors.New("invalid queue id")
|
||||
}
|
||||
q := scheduleQuery(in)
|
||||
q.Set("queue_id", queue)
|
||||
err := c.call(ctx, "GET", "/v1/schedules?"+q.Encode(), "", nil, &out)
|
||||
return out, err
|
||||
}
|
||||
func (c *Client) GetScheduleVersion(ctx context.Context, id string, version uint64) (ScheduleVersion, error) {
|
||||
var out ScheduleVersion
|
||||
if !validID(id) || version == 0 {
|
||||
return out, errors.New("invalid schedule id or version")
|
||||
}
|
||||
err := c.call(ctx, "GET", "/v1/schedules/"+id+"/versions/"+strconv.FormatUint(version, 10), "", nil, &out)
|
||||
return out, err
|
||||
}
|
||||
func (c *Client) ListScheduleOccurrences(ctx context.Context, id string, in SchedulePageRequest) (ScheduleOccurrencePage, error) {
|
||||
var out ScheduleOccurrencePage
|
||||
if !validID(id) {
|
||||
return out, errors.New("invalid schedule id")
|
||||
}
|
||||
err := c.call(ctx, "GET", "/v1/schedules/"+id+"/occurrences?"+scheduleQuery(in).Encode(), "", nil, &out)
|
||||
return out, err
|
||||
}
|
||||
func scheduleQuery(in SchedulePageRequest) url.Values {
|
||||
q := url.Values{}
|
||||
if in.Limit != 0 {
|
||||
q.Set("limit", strconv.Itoa(in.Limit))
|
||||
}
|
||||
if in.Cursor != "" {
|
||||
q.Set("cursor", in.Cursor)
|
||||
}
|
||||
return q
|
||||
}
|
||||
Reference in New Issue
Block a user