Introduce Task Results

* Added Retention Option to specify retention TTL for tasks
* Added ResultWriter as a client interface to write result data for the associated task
This commit is contained in:
Ken Hibino
2021-11-05 16:52:54 -07:00
parent 4638405cbd
commit f4ddac4dcc
33 changed files with 2099 additions and 846 deletions

View File

@@ -46,6 +46,7 @@ const (
ProcessAtOpt
ProcessInOpt
TaskIDOpt
RetentionOpt
)
// Option specifies the task processing behavior.
@@ -70,6 +71,7 @@ type (
uniqueOption time.Duration
processAtOption time.Time
processInOption time.Duration
retentionOption time.Duration
)
// MaxRetry returns an option to specify the max number of times
@@ -178,6 +180,17 @@ func (d processInOption) String() string { return fmt.Sprintf("ProcessIn(%v)
func (d processInOption) Type() OptionType { return ProcessInOpt }
func (d processInOption) Value() interface{} { return time.Duration(d) }
// Retention returns an option to specify the duration of retention period for the task.
// If this option is provided, the task will be stored as a completed task after successful processing.
// A completed task will be deleted after the specified duration elapses.
func Retention(d time.Duration) Option {
return retentionOption(d)
}
func (ttl retentionOption) String() string { return fmt.Sprintf("Retention(%v)", time.Duration(ttl)) }
func (ttl retentionOption) Type() OptionType { return RetentionOpt }
func (ttl retentionOption) Value() interface{} { return time.Duration(ttl) }
// ErrDuplicateTask indicates that the given task could not be enqueued since it's a duplicate of another task.
//
// ErrDuplicateTask error only applies to tasks enqueued with a Unique option.
@@ -196,6 +209,7 @@ type option struct {
deadline time.Time
uniqueTTL time.Duration
processAt time.Time
retention time.Duration
}
// composeOptions merges user provided options into the default options
@@ -237,6 +251,8 @@ func composeOptions(opts ...Option) (option, error) {
res.processAt = time.Time(opt)
case processInOption:
res.processAt = time.Now().Add(time.Duration(opt))
case retentionOption:
res.retention = time.Duration(opt)
default:
// ignore unexpected option
}
@@ -316,6 +332,7 @@ func (c *Client) Enqueue(task *Task, opts ...Option) (*TaskInfo, error) {
Deadline: deadline.Unix(),
Timeout: int64(timeout.Seconds()),
UniqueKey: uniqueKey,
Retention: int64(opt.retention.Seconds()),
}
now := time.Now()
var state base.TaskState
@@ -335,7 +352,7 @@ func (c *Client) Enqueue(task *Task, opts ...Option) (*TaskInfo, error) {
case err != nil:
return nil, err
}
return newTaskInfo(msg, state, opt.processAt), nil
return newTaskInfo(msg, state, opt.processAt, nil), nil
}
func (c *Client) enqueue(msg *base.TaskMessage, uniqueTTL time.Duration) error {