GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — workflow/closure.go at main · ubgo/workflow Expand file tree/ Copy pathclosure.goMore file actions 121 lines (106 loc) · 4.29 KB / Copy pathclosure.goFile metadata and controls121 lines (106 loc) · 4.29 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 package workflow import ( "context" "time" ) // ClosureWorkflow is the workflow name a closure run carries in RunInfo, // RunEvent and StepInfo, so run listings, run-finished observers and // middleware see closures as runs of one name. Validate refuses it as the // name of a registered workflow. The closure's own name is in the // LabelClosure label. // //ds:def id=closureworkflow-5ad4abw7 owner=@khanakia stability=stable const ClosureWorkflow = "ubgo.workflow.closure" // LabelClosure is the label holding a closure run's ClosureJob.Name. It // sits under the reserved internal prefix, so app labels cannot forge it. // //ds:def id=labelclosure-ruy9jt45 owner=@khanakia stability=stable const LabelClosure = LabelPrefixInternal + "closure" // ClosureStep is the name of a closure run's one step. // //ds:def id=closurestep-hvxugm4k owner=@khanakia stability=stable const ClosureStep = "closure" // ClosureJob is a Go function run as a job in this process, for small // fire-and-forget work that does not deserve a registered workflow (send // a log line elsewhere, warm a cache). // // A closure cannot be stored or sent: it lives only in the memory of the // process that enqueued it, is lost on a restart or crash, and never runs // on another machine. Anything that must survive a restart is a // registered workflow with a payload. Only an in-process driver declares // CapClosure; durable drivers and engines refuse closures. // //ds:def id=closurejob-ghxxzktz owner=@khanakia stability=stable type ClosureJob struct { // Name says what the closure is, for logs and run listings (the // LabelClosure label). Required. Name string // Fn is the work. Its ctx carries StepInfo and is cancelled when the // run is cancelled or the client stops. Returning an error retries it // under Retry. Required. Fn func(ctx context.Context) error // Queue is the queue it runs on; empty means DefaultQueue. Queue string // RunAt delays the start; zero means now. RunAt time.Time // Labels are copied onto the run, beside LabelClosure. Labels map[string]string // Priority orders it among waiting runs, as Job.Priority. Priority int // Retry is the retry policy; nil means no retry. Since the closure // never leaves the process, NoRetryOn and DelayFor are allowed. Retry *RetryPolicy // Timeout bounds each attempt; zero means none. Timeout time.Duration } // ValidateClosure checks c: a Name, a Fn, a valid queue and retry policy, // a Timeout that is not negative, and no label under a reserved prefix. // Errors wrap ErrInvalid. // //ds:def id=validateclosure-hd4ctss3 owner=@khanakia stability=stable func ValidateClosure(c ClosureJob) error { if c.Name == "" { return invalidf("closure has no Name") } if c.Fn == nil { return invalidf("closure %q has no Fn", c.Name) } if c.Queue != "" { if err := ValidateQueueName(c.Queue); err != nil { return err } } if c.Retry != nil { if err := validateRetry(*c.Retry); err != nil { return invalidf("closure %q: %v", c.Name, err) } } if c.Timeout < 0 { return invalidf("closure %q: Timeout is negative", c.Name) } return ValidateLabels(c.Labels) } // ClosureEnqueuer runs closures. A client implements it when it declares // CapClosure. EnqueueClosure validates c (ValidateClosure), then starts a // run of ClosureWorkflow whose one step (ClosureStep) calls c.Fn; the // RunRef works with Status, Cancel and the run query family as any // other. Before Start it returns ErrNotStarted. A closure run is never // matched by an idempotency key and is never chained. // //ds:def id=closureenqueuer-amwdzqnj owner=@khanakia stability=stable type ClosureEnqueuer interface { EnqueueClosure(ctx context.Context, c ClosureJob) (RunRef, error) } // EnqueueClosure runs c on client when it implements ClosureEnqueuer and // declares CapClosure, and otherwise returns an UnsupportedError naming // CapClosure, never silently dropping the work. // //ds:def id=enqueueclosure-ngugsmdf owner=@khanakia stability=stable func EnqueueClosure(ctx context.Context, client Client, c ClosureJob) (RunRef, error) { ce, ok := client.(ClosureEnqueuer) if !ok || !client.Capabilities().Has(CapClosure) { return RunRef{}, &UnsupportedError{Field: "EnqueueClosure", Missing: CapClosure, Reason: "closures run only on an in-process driver; register a workflow instead"} } return ce.EnqueueClosure(ctx, c) } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |