//--- Methods --- public async Task CaptureEvent(CaptureEventRequest request) { _logger.LogInfo($"received EventType: {request.EventType} = {request.Data}"); // artificial delay to show that end-points without a response don't wait for the invocation to complete await Task.Delay(TimeSpan.FromSeconds(3)); }
//--- Methods --- public async Task <GetBuildResponse> GetBuild(GetBuildRequest request) { var stopwatch = System.Diagnostics.Stopwatch.StartNew(); try { var getBuildTask = _lambdaClient.InvokeAsync(new InvokeRequest { Payload = JsonSerializer.Serialize(new BotRequest { Command = BotCommand.GetBuild, Session = request.Session, Bot = request.Bot }), FunctionName = _lambdaArn, InvocationType = InvocationType.RequestResponse }); // check if lambda responds within time limit if (await Task.WhenAny(getBuildTask, Task.Delay(_requestTimeout)) != getBuildTask) { _logger?.LogInfo($"Bot {_botId} GetBuild timed out after {stopwatch.Elapsed.TotalSeconds:N2}s"); return(null); } var response = Encoding.UTF8.GetString(getBuildTask.Result.Payload.ToArray()); var result = JsonSerializer.Deserialize <BotResponse>(response); _logger?.LogInfo($"Bot {_botId} GetBuild responded in {stopwatch.Elapsed.TotalSeconds:N2}s:\n{response}"); return(result.BotBuild); } catch (Exception e) { _logger?.LogErrorAsInfo(e, $"Bot {_botId} GetBuild failed (arn: {_lambdaArn ?? "<NULL>"})"); return(null); } }
public async Task <Response <S3WriterResourceAttribute> > Delete(S3WriterResourceProperties properties) { if (properties.Enabled == false) { // don't do anything if disabled return(new Response <S3WriterResourceAttribute>()); } var bucketName = properties.BucketName; _logger.LogInfo($"emptying bucket: {bucketName}"); // enumerate all S3 objects var request = new ListObjectsV2Request { BucketName = bucketName }; var counter = 0; var deletions = new List <Task>(); do { var response = await _s3Client.ListObjectsV2Async(request); // delete any objects found if (response.S3Objects.Any()) { deletions.Add(_s3Client.DeleteObjectsAsync(new DeleteObjectsRequest { BucketName = bucketName, Objects = response.S3Objects.Select(s3 => new KeyVersion { Key = s3.Key }).ToList(), Quiet = true })); counter += response.S3Objects.Count; } // continue until no more objects can be fetched request.ContinuationToken = response.NextContinuationToken; } while(request.ContinuationToken != null); // wait for all deletions to complete await Task.WhenAll(deletions); _logger.LogInfo($"deleted {counter:N0} objects"); return(new Response <S3WriterResourceAttribute>()); }
//--- Methods --- public async Task <Response <S3WriterResourceAttribute> > Create(S3WriterResourceProperties properties) { _logger.LogInfo($"writing JSON file to s3://{properties.BucketName}/{properties.Key}"); var contents = Serialize(properties.Contents); await _s3Client.PutObjectAsync(new PutObjectRequest { BucketName = properties.BucketName, ContentBody = contents, ContentType = "application/json", Key = properties.Key }); _logger.LogInfo($"JSON file written ({Encoding.UTF8.GetByteCount(contents):N0} bytes)"); return(new Response <S3WriterResourceAttribute> { PhysicalResourceId = $"s3writejson:{properties.BucketName}:{properties.Key}", Attributes = new S3WriterResourceAttribute { Url = $"s3://{properties.BucketName}/{properties.Key}" } }); }
//--- Methods --- public async Task <Response <S3WriterResourceAttribute> > Create(S3WriterResourceProperties properties) { _logger.LogInfo($"copying package s3://{properties.SourceBucketName}/{properties.SourceKey} to S3 bucket {properties.DestinationBucketName}"); // download package and copy all files to destination bucket var fileEntries = new Dictionary <string, string>(); if (!await ProcessZipFileItemsAsync(properties.SourceBucketName, properties.SourceKey, async entry => { await UploadEntry(entry, properties); fileEntries.Add(entry.FullName, ComputeEntryHash(entry, properties)); })) { throw new FileNotFoundException("Unable to download source package"); } // create package manifest for future deletion _logger.LogInfo($"uploaded {fileEntries.Count:N0} files"); await WriteManifest(properties, fileEntries); return(new Response <S3WriterResourceAttribute> { PhysicalResourceId = $"s3unzip:{properties.DestinationBucketName}:{properties.DestinationKey}", Attributes = new S3WriterResourceAttribute { Url = $"s3://{properties.DestinationBucketName}/{properties.DestinationKey}" } }); }