Skip to the content.

Working with jobs from your code

What the README’s quick start leaves out: every call of ISyncLane, what a result means, how a handler says more than “it failed”, who asked for a job, priorities, and rate budgets in a hand-written handler.

Calls

ISyncLane is the only service you inject. Every call returns a result instead of throwing for problems you can expect:

Call Returns What it is for
PlanAsync(queue, owner) SyncResult<int> Plan one owner now, for example right after an account connects.
EnqueueAsync(newJob) SyncResult<long> Put one job in by hand, outside the plan. The value is the new job’s id.
EnqueueOrReplaceAsync(newJob) SyncResult<EnqueuedJob> The same, but a job with the same owner, type and dedup key takes the new request’s place instead of refusing it: the latest request for a key wins. Replaced says whether one was there.
CompleteAsync(queue, owner, type, keys) SyncResult<int> Close jobs by their dedup keys without running them, when the work was done some other way. The value is how many were closed.
GetJobAsync(id) SyncResult<Job> One job with its arguments, which a list leaves out.
RequeueAsync(queue, owner?) SyncResult<int> Give failed, deferred and abandoned jobs fresh attempts. Cursors are kept.
CancelAsync(queue, owner, reason) SyncResult<int> Abandon what has not started, for example when an account disconnects.
GetJobsAsync(query) SyncListResult<Job> One page of jobs by queue, owner, status or type, newest first, or of the owners whose name or key contains Search (“acme” finds every store of “Acme Toys”). Follow NextCursor for the next page; set IncludeTotal for a count.
GetProgressAsync(queue, owner) SyncResult<OwnerProgress> How far the first load of an owner is. Use it to show “connecting” without storing a status of your own.
MarkLoadedAsync(queue, ranges) SyncResult<int> Moving from another system: record the history it already loaded, so only what is older and what came after is fetched.
ForgetOwnerAsync(queue, owner) SyncResult<int> Delete everything about an owner that is gone for good. If it comes back, its history loads again.
var result = await syncLane.EnqueueAsync(new NewJob { Queue = "shops", Owner = owner, Type = "orders", DedupKey = "resync-2026-10" });

if (!result.Succeeded)
    return result.Error!.Code == SyncErrorCodes.JobAlreadyExists
        ? Results.Ok("Already queued")
        : Results.Problem(result.Error.Message, statusCode: result.Error.Kind.ToHttpStatusCode());

return Results.Ok(result.Value);

Who asked

NewJob.RequestedBy says who or what asked for a job, such as a user’s id or scheduler: the handler reads it as job.RequestedBy, for example to send it along, and the monitor shows it. A sync-now records its caller there. It is the caller’s data, never logged.

Arguments

NewJob.Arguments is what the handler needs for this one job and cannot look up, such as the body of a delivery as it was when the user sent it. It is text of your choosing, JSON for example, up to 262,144 characters; SyncLane stores it with the job and hands it back unread as job.Arguments.

The latest request wins

A delivery that failed and is waiting for its next attempt is out of date as soon as the user sends a newer one. EnqueueOrReplaceAsync puts the new request in the old one’s place: the same job, the same id and place in an ordered lane, with the new arguments, priority and requester and a fresh set of attempts; its window and lane stay as they were. A job that completed has no place left to keep: it is deleted, and a new job with a new id joins its lane at the back, as any new job would. A planned job is never replaced.

A job that is running stays running. Its lease is revoked, so whatever its attempt reports is refused, and it runs again with the new arguments when that lease runs out, at most LeaseDuration later. Its lane stays closed until then.

When the work got done some other way, for example a direct call that went through while the job was waiting, CompleteAsync closes the job by its key without running it. A running job is closed too and its lane is free at once; the attempt under way may still be inside your handler until its next page or heartbeat, and whatever it reports is refused. Nothing is counted as work done.

Priority

Jobs that are due run in this order: Urgent jobs, then the owners an operator moved forward with PrioritizeOwnerAsync, then High, Normal and Low. Within a level, types run in the order you declared them. A job takes its level from NewJob.Priority, or else from its owner:

// A stock change that must reach the marketplace before anything else in line.
await syncLane.EnqueueAsync(new NewJob { Queue = "shops", Owner = owner, Type = "inventory", Priority = JobPriority.Urgent });

// A new customer whose history should load before the others: give it from your owner source.
return accounts
    .Select(account => new JobOwner(account.Id, Name: account.Name) { Priority = account.IsNew ? JobPriority.High : JobPriority.Normal })
    .ToList();

An owner’s priority applies to the jobs created after it changes; jobs already waiting keep theirs. Priority never cuts a running job: one account’s jobs share a lane, so an urgent job of that account runs as soon as the job in its lane ends. Job.PriorityLevel and Job.Prioritized show where a job stands.

Results and errors

A failed result always means the call was understood and the answer is no: an undeclared queue, a duplicate job, a page size out of range. It never hides an outage; a database that cannot be reached still throws. Every error has a stable code such as SL3101, listed with its cause and fix in docs/errors.md. Call ThrowIfFailed() or GetValueOrThrow() when you would rather have an exception.

Three exceptions let a handler say more than “it failed”:

Rate budgets in a handler

An API’s limit belongs to an account, not to a job. Declare it once, and every job that calls the account draws from it:

builder.Services.AddSyncLane(lane =>
{
    lane.AddBudget("example-shop", limit: 2, per: TimeSpan.FromSeconds(1), burst: 40);
    lane.AddQueue("shops", queue => { /* ... */ });
});

A recipe draws from it with "rate": { "budget": "example-shop" }. A hand-written handler calls RateBudgets before each request, and hands the permit back when the API answered 429:

var permit = await budgets.WaitAsync("example-shop", job.Owner, cancellationToken);
// ... send the request ...
if (response.StatusCode == HttpStatusCode.TooManyRequests)
    await budgets.ThrottledAsync(permit, response.Headers.RetryAfter?.Delta, cancellationToken);