public virtual async Task StartPipelineIfExists(GithubEvent payload) { try { var accountId = Environment.GetEnvironmentVariable("AWS_ACCOUNT_ID"); var region = Environment.GetEnvironmentVariable("AWS_REGION"); var pushEventInput = payload is PushEvent pushEvent?Serialize(pushEvent) : null; var prEventInput = payload is PullRequestEvent pullEvent?Serialize(pullEvent) : null; var input = pushEventInput ?? prEventInput; var stateMachineArn = $"arn:aws:states:{region}:{accountId}:stateMachine:{payload.Repository.Name}-cicd-pipeline"; var request = new StartExecutionRequest { StateMachineArn = stateMachineArn, Input = input }; if (payload.Ref.StartsWith("refs/heads/")) { request.Name = payload.HeadCommitId; } var response = await stepFunctionsClient.StartExecutionAsync(request); logger.LogInformation($"Received start execution response: {Serialize(response)}"); } catch (Exception e) { logger.LogError($"Got error trying to start pipeline: {e.Message}"); } }
private async Task StartExecutionAsync(PublishRequest request, StartExecutionRequest executionRequest, CancellationToken cancellationToken = default(CancellationToken)) { if (request.ShouldSuppressExecutionAlreadyExistsException()) { try { await _stepFunctions.StartExecutionAsync(executionRequest, cancellationToken); } catch (ExecutionAlreadyExistsException ex) { Console.WriteLine($"ExecutionAlreadyExistsException error for {request.GetExecutionName()}. Exception was: {ex.Message}"); } } else { await _stepFunctions.StartExecutionAsync(executionRequest, cancellationToken); } }
public async Task InvokeOddEvenStepFunction(Input state, ILambdaContext context) { // Start step function execution var execution = await _stepFunctionsClient.StartExecutionAsync(new StartExecutionRequest { StateMachineArn = _oddEvenStateMachineArnKey, Name = $"StepFunctionExecution-{context.AwsRequestId}", Input = JsonSerializer.Serialize(state) }); }
/// <summary> /// A simple function that takes a string and returns both the upper and lower case version of the string. /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public async Task FunctionHandler(S3Event evnt, ILambdaContext context) { var bucket = evnt.Records[0].S3.Bucket.Name; var key = WebUtility.UrlDecode(evnt.Records[0].S3.Object.Key); Console.WriteLine(bucket); Console.WriteLine(key); var photoData = key.Split("/").Reverse().Take(2).ToArray(); var photoId = photoData[0]; var userId = photoData[1]; Console.WriteLine(photoId); var input = new { Bucket = bucket, SourceKey = key, PhotoId = photoId, UserId = userId, TablePhoto = Environment.GetEnvironmentVariable(PHOTO_TABLE) }; var stepResponse = await _stepClient.StartExecutionAsync(new StartExecutionRequest { StateMachineArn = StateMachineArn, Name = $"{MakeSafeName(key, 80)}", Input = JsonConvert.SerializeObject(input) }).ConfigureAwait(false); var photo = new Photo { PhotoId = photoId, SfnExecutionArn = stepResponse.ExecutionArn, ProcessingStatus = ProcessingStatus.Running, UpdatedDate = DateTime.UtcNow }; await _ddbContext.SaveAsync(photo).ConfigureAwait(false); }
private Amazon.StepFunctions.Model.StartExecutionResponse CallAWSServiceOperation(IAmazonStepFunctions client, Amazon.StepFunctions.Model.StartExecutionRequest request) { Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "AWS Step Functions", "StartExecution"); try { #if DESKTOP return(client.StartExecution(request)); #elif CORECLR return(client.StartExecutionAsync(request).GetAwaiter().GetResult()); #else #error "Unknown build edition" #endif } catch (AmazonServiceException exc) { var webException = exc.InnerException as System.Net.WebException; if (webException != null) { throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException); } throw; } }
public async Task <StartGameResponse> StartGameAsync(StartGameRequest request) { LogInfo($"Starting a new game: ConnectionId = {CurrentRequest.RequestContext.ConnectionId}"); // create a new game var game = new Game { Id = Guid.NewGuid().ToString("N"), Status = GameStatus.Start, BoardWidth = request.BoardWidth ?? 1000.0, BoardHeight = request.BoardHeight ?? 1000.0, SecondsPerTurn = request.SecondsPerTurn ?? 0.5, MaxTurns = request.MaxTurns ?? 300, MaxBuildPoints = request.MaxBuildPoints ?? 8, DirectHitRange = request.DirectHitRange ?? 5.0, NearHitRange = request.NearHitRange ?? 20.0, FarHitRange = request.FarHitRange ?? 40.0, CollisionRange = request.CollisionRange ?? 8.0, MinRobotStartDistance = request.MinRobotStartDistance ?? 50.0, RobotTimeoutSeconds = request.RobotTimeoutSeconds ?? 15.0 }; var gameRecord = new GameRecord { PK = game.Id, Game = game, LambdaRobotArns = request.RobotArns, ConnectionId = CurrentRequest.RequestContext.ConnectionId }; // store game record await _table.CreateAsync(gameRecord); // dispatch game loop var gameTurnRequest = new { GameId = game.Id, Status = game.Status, GameLoopType = request.GameLoopType }; switch (request.GameLoopType) { case GameLoopType.Recursive: LogInfo($"Kicking off Game Turn lambda: Name = {_gameTurnFunctionArn}"); await _lambdaClient.InvokeAsync(new InvokeRequest { Payload = SerializeJson(gameTurnRequest), FunctionName = _gameTurnFunctionArn, InvocationType = InvocationType.Event }); break; case GameLoopType.StepFunction: // kick off game step function var startGameId = $"LambdaRobotsGame-{game.Id}"; LogInfo($"Kicking off Step Function: Name = {startGameId}"); var startGame = await _stepFunctionsClient.StartExecutionAsync(new StartExecutionRequest { StateMachineArn = _gameStateMachine, Name = startGameId, Input = SerializeJson(gameTurnRequest) }); // update execution ARN for game record await _table.UpdateAsync(new GameRecord { PK = game.Id, GameLoopArn = startGame.ExecutionArn }, new[] { nameof(GameRecord.GameLoopArn) }); break; default: throw new ApplicationException($"unsupported: GameLoopType = {request.GameLoopType}"); } // return with kicked off game return(new StartGameResponse { Game = game }); }
public override async Task <TContext> Execute(TContext context) { var maxMessages = context.JobProcessingCapacity < 10 ? context.JobProcessingCapacity : 10; var receiveMessageResult = await sqs.ReceiveMessageAsync(new ReceiveMessageRequest { MaxNumberOfMessages = maxMessages, QueueUrl = context.JobQueueUrl }); if (receiveMessageResult.Messages.Count > 0) { var tasks = new List <Task>(); foreach (var message in receiveMessageResult.Messages) { Task task = null; switch (context.MessageProcessorType) { case "Lambda": var lambdaArn = $"arn:aws:{context.RegionCode}:{context.AccountId}:function:{context.MessageProcessorName}"; task = lambda.InvokeAsync( new InvokeRequest { FunctionName = context.MessageProcessorName, Payload = message.Body, InvocationType = InvocationType.Event, ClientContext = GetType().Name }); break; case "StepFunction": task = stepFunctions.StartExecutionAsync( new StartExecutionRequest { Input = message.Body, StateMachineArn = $"arn:aws:states:{context.RegionCode}:{context.AccountId}:stateMachine:{context.MessageProcessorName}", Name = message.MessageId }); break; default: throw new Exception($"Unsupported MessageProcessorType: {context.MessageProcessorType}"); } tasks.Add(task); } await Task.WhenAll(tasks); var deleteResult = await sqs.DeleteMessageBatchAsync(new DeleteMessageBatchRequest { QueueUrl = context.JobQueueUrl, Entries = receiveMessageResult.Messages.Select(message => new DeleteMessageBatchRequestEntry { Id = message.MessageId, ReceiptHandle = message.ReceiptHandle }).ToList() }); } return(context); }