{"modulePath":"github.com/worklydev/workly","version":"v0.5.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package dispatch\n\nPackage dispatch delivers due tasks over HTTP.\n\nExactly one dispatcher is active at a time (see store.Dispatch.TryLead); other replicas stand by and take over if the leader dies. The leader runs many deliveries concurrently. Delivery is at-least-once: a task whose delivery outcome is unknown (crash, shutdown) is delivered again.\n\n## Functions\n\n```go\nfunc Refused(err error) error\n```\nRefused marks an error from a DialControl as final: the connection would be refused again on a retry (a private address on a server that delivers only to the public internet, say), so the attempt fails its task or run at once rather than being retried.\n\n## Types\n\n```go\ntype DialControl = func(network, address string, c syscall.RawConn) error\n```\nDialControl vets a delivery connection's resolved address before it's made; see net.Dialer.Control.\n\n```go\ntype Dispatcher struct {\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc New(opts Options) *Dispatcher\n```\n\n```go\nfunc (d *Dispatcher) Run(ctx context.Context) error\n```\nRun dispatches while leader, and stands by otherwise, until ctx is cancelled. It then lets in-flight deliveries finish (up to ShutdownTimeout) before returning.\n\n```go\ntype Kind string\n```\nKind is what an execution delivered.\n\n```go\nconst (\n\tKindTask         Kind = \"task\"\n\tKindWorkflowStep Kind = \"workflow_step\"\n\tKindCallback     Kind = \"callback\"\n\tKindScheduleRun  Kind = \"schedule_run\"\n)\n```\n\n```go\ntype Options struct {\n\tStore         store.Store\n\tLogger        *slog.Logger\n\tSigningSecret []byte\n\t// PreviousSigningSecret, if set, also signs deliveries while endpoints\n\t// move to SigningSecret.\n\tPreviousSigningSecret []byte\n\t// SigningSecrets, if set, signs each workspace's deliveries with its own\n\t// secrets instead.\n\tSigningSecrets SecretsFunc\n\tUserAgent      string\n\n\t// MaxInFlight caps concurrent deliveries across all queues. Queues can\n\t// set lower limits of their own. Default 100.\n\tMaxInFlight int\n\t// WorkspaceMaxInFlight, if set, returns a workspace's own cap on\n\t// concurrent deliveries, within MaxInFlight; 0 means none.\n\tWorkspaceMaxInFlight func(ctx context.Context, ws workspace.ID) (int, error)\n\t// MaxRunsInFlight caps concurrent deliveries of schedule runs,\n\t// separately from MaxInFlight. Default 20.\n\tMaxRunsInFlight int\n\t// PollInterval is the longest the dispatcher sleeps without checking for\n\t// due tasks, as a fallback for missed wake-ups. Default 1s.\n\tPollInterval time.Duration\n\t// LeaseGrace is added to a task's timeout to form its lease. If the lease\n\t// expires the task is presumed lost and redelivered. A leader never reaps\n\t// its own deliveries, so this only needs to cover a previous leader\n\t// finishing up after losing leadership. Default 10s.\n\tLeaseGrace time.Duration\n\t// ReapInterval is how often expired leases are checked. Default 10s.\n\tReapInterval time.Duration\n\t// LeaderRetryInterval is how often a standby tries to take over. Default 5s.\n\tLeaderRetryInterval time.Duration\n\t// ShutdownTimeout bounds how long in-flight deliveries may finish after\n\t// shutdown starts; the rest are interrupted and redelivered. Default 25s.\n\tShutdownTimeout time.Duration\n\t// Retention is how long finished tasks (succeeded, failed or cancelled)\n\t// are kept before they are deleted with their attempts. 0 keeps them\n\t// forever.\n\tRetention time.Duration\n\t// RunRetention is how long finished schedule runs are kept; each\n\t// schedule's latest run is kept regardless. 0 keeps them forever.\n\tRunRetention time.Duration\n\t// WorkspaceRetention, if set, returns the workspaces whose finished\n\t// tasks, groups and runs are kept for their own duration instead (0\n\t// keeps them forever). Called once per sweep; if it fails, those\n\t// workspaces aren't known, so nothing is deleted that sweep.\n\tWorkspaceRetention func(ctx context.Context) (map[workspace.ID]time.Duration, error)\n\t// SweepInterval is how often finished tasks and runs past retention are\n\t// deleted. Default 1m.\n\tSweepInterval time.Duration\n\t// Client defaults to a client that doesn't follow redirects, and whose\n\t// connections DialControl vets.\n\tClient      *http.Client\n\tDialControl DialControl\n\t// Metrics defaults to a private set that nothing serves.\n\tMetrics *metrics.Workly\n\t// Alerts, if set, runs while this dispatcher leads, so alerts are sent\n\t// from one process.\n\tAlerts interface{ Run(ctx context.Context) }\n\t// Changed, if set, is told when a workspace's tasks or runs change\n\t// state, so dashboards can refresh. It must not block.\n\tChanged func(workspace.ID)\n\t// Executed, if set, is told of each execution: the first delivery\n\t// attempt of a task or schedule run since it was created or retried by\n\t// hand, once the attempt is done. A schedule run that starts a workflow\n\t// isn't one (its steps are). It must not block for long: it is called\n\t// from the delivery's goroutine.\n\tExecuted func(ctx context.Context, ws workspace.ID, kind Kind)\n\t// AutoPaused, if set, is told when a queue pauses itself after failures\n\t// in a row, and when it resumes after a success. It must not block.\n\tAutoPaused func(store.AutoPauseEvent)\n\t// FirstProbeDelay is how long after an auto-pause the first probe goes.\n\t// Each failed probe doubles it, up to MaxProbeDelay. Defaults 30s and\n\t// 10m.\n\tFirstProbeDelay time.Duration\n\tMaxProbeDelay   time.Duration\n}\n```\n\n```go\ntype SecretsFunc = func(ctx context.Context, ws workspace.ID) (current, previous []byte, err error)\n```\nSecretsFunc returns the secrets that sign a workspace's deliveries: the current one, and the previous one during a rotation (or nil).\n\n```go\ntype Signer struct {\n\t// contains filtered or unexported fields\n}\n```\nSigner sets the headers every delivery carries. The API uses it too, to show a task's delivery request exactly as the dispatcher sends it.\n\n```go\nfunc NewSigner(secret, previousSecret []byte, userAgent string, lookup SecretsFunc) Signer\n```\nNewSigner signs with secret and previousSecret, or, if lookup is set, with each workspace's own secrets.\n\n```go\nfunc (s Signer) Header(ctx context.Context, t store.Task, attempt int, now time.Time) (http.Header, error)\n```\nHeader returns the headers of attempt number of t, sent at now: the task's own, then Workly's.\n\n```go\nfunc (s Signer) RunHeader(ctx context.Context, r store.Run, attempt int, now time.Time) (http.Header, error)\n```\nRunHeader returns the headers of attempt number of a schedule run, sent at now: the run's own, then Workly's. Runs are told apart from tasks by Workly-Run-Id, and carry the slot they are for.\n\n","path":"github.com/worklydev/workly/internal/dispatch","name":"dispatch","synopsis":"Package dispatch delivers due tasks over HTTP.","isRedistributable":true}
