Reference

UseLambdaWorkers

Package Trax.Scheduler.LambdaNamespaces Trax.Scheduler.Lambda.Extensions, Trax.Scheduler.Lambda.ConfigurationSource LambdaSchedulerExtensions.cs, LambdaWorkerOptions.cs, LambdaRetryOptions.cs, LambdaJobSubmitter.cs

Routes chosen trains' queued runs to an AWS Lambda function by direct SDK invocation. No public endpoint is involved; access is governed by IAM. Trains not routed here keep running on the local workers.

Signature

public static SchedulerConfigurationBuilder UseLambdaWorkers(
    this SchedulerConfigurationBuilder builder,
    Action<LambdaWorkerOptions> configure,
    Action<SubmitterRouting>? routing = null
)

Parameters

ParameterTypeDescription
configureAction<LambdaWorkerOptions>Sets the function name, client and retry options
routingAction<SubmitterRouting>?Names the trains sent to this function with ForTrain<TTrain>(), which returns the routing for chaining. When omitted, no train is routed here by name; [TraxRemote] trains are, if this is the first UseRemoteWorkers, UseSqsWorkers or UseLambdaWorkers call. With none of the three, a [TraxRemote] train fails the scheduler's build

LambdaWorkerOptions

PropertyTypeDefaultDescription
FunctionNamestringrequiredThe function name, ARN, or partial ARN to invoke
ConfigureLambdaClientAction<AmazonLambdaConfig>?nullConfigures the AmazonLambdaConfig the client is built from: region, a LocalStack ServiceURL, and so on
RetryLambdaRetryOptionssee belowRetries for transient AWS failures
SigningKeybyte[]?nullThe key shared with the function's ConfigureRunner, at least 32 bytes. When set, each envelope carries a Signature over its PayloadJson. See Authorization Posture

LambdaRetryOptions

PropertyTypeDefaultDescription
MaxRetriesint5Retry attempts; 0 disables retrying
BaseDelayTimeSpan1 secondThe first delay, doubled on each attempt with plus or minus 25% jitter
MaxDelayTimeSpan30 secondsThe cap on any one delay

Retried: AWS status codes 429, 502, 503 and 504, and a network-level HttpRequestException. Not retried: ResourceNotFoundException, InvalidParameterValueException and function errors.

Returns

SchedulerConfigurationBuilder, for chaining.

Behavior

  • A routed entry is serialized as a RemoteJobRequest (the metadata id and the input, if any), wrapped in a LambdaEnvelope with Type = Execute, and sent with InvocationType.Event. A FunctionError on the response throws TrainException. The job id is "lambda-{guid}".
  • A train can be routed to one submitter only. Routing it to two throws InvalidOperationException when the scheduler is built.
  • UseLambdaWorkers() may be called once per function. Each call keeps its own LambdaWorkerOptions and Lambda client and invokes only the trains it routes.
  • An asynchronous invocation payload is at most 256 KB.
  • TraxLambdaFunction cancels a job TerminalWriteMargin before the function's own timeout, so the run can record its outcome.
  • A cancel reaches the function through the database, as for UseLambdaRun.
  • The scheduler process needs lambda:InvokeFunction on the function.

Registered services

ServiceLifetimeDescription
IAmazonLambda (keyed)SingletonThe call's own client, configured by its ConfigureLambdaClient
Lambda job submitterCreated per dispatchAn internal IJobSubmitter the JobDispatcher creates for each train routed to this call
LambdaWorkerOptions, IAmazonLambdaSingletonThe first call's options and client, also registered by type (UseLambdaRun() registers its own IAmazonLambda too)

The default IJobSubmitter is not replaced: local workers keep running the trains not routed here.

Example

using Trax.Scheduler.Lambda.Extensions;
 
services.AddTrax(trax => trax
    .AddEffects(effects => effects.UsePostgres(connectionString))
    .AddMediator(assembly)
    .AddScheduler(scheduler => scheduler
        .UseLambdaWorkers(
            lambda =>
            {
                lambda.FunctionName = "content-shield-runner";
                lambda.Retry.MaxRetries = 3;
            },
            routing => routing
                .ForTrain<IReviewContentTrain>()
                .ForTrain<ISendViolationNoticeTrain>())
        .Schedule<ISyncTrain>("sync", new SyncInput(), Every.Minutes(5))));

IReviewContentTrain and ISendViolationNoticeTrain run on the function; ISyncTrain runs on the local workers.

See also