Add helper functions to extract task metadata from context

This commit is contained in:
Ken Hibino
2020-05-28 06:28:14 -07:00
parent 39188fe930
commit 23f46dde52
5 changed files with 246 additions and 100 deletions

View File

@@ -405,20 +405,3 @@ func gcd(xs ...int) int {
}
return res
}
// createContext returns a context and cancel function for a given task message.
func createContext(msg *base.TaskMessage) (ctx context.Context, cancel context.CancelFunc) {
ctx = context.Background()
timeout, err := time.ParseDuration(msg.Timeout)
if err == nil && timeout != 0 {
ctx, cancel = context.WithTimeout(ctx, timeout)
}
deadline, err := time.Parse(time.RFC3339, msg.Deadline)
if err == nil && !deadline.IsZero() {
ctx, cancel = context.WithDeadline(ctx, deadline)
}
if cancel == nil {
ctx, cancel = context.WithCancel(ctx)
}
return ctx, cancel
}