public async Task <APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context) { var regionFromEnv = Environment.GetEnvironmentVariable("AWS_REGION"); Console.WriteLine($"System.Environment.GetEnvironmentVariable(\"AWS_REGION\") = {regionFromEnv}"); var region = RegionEndpoint.GetBySystemName(regionFromEnv); Console.WriteLine($"Detected region = {region}"); Console.WriteLine(JsonConvert.SerializeObject(apigProxyEvent)); IForgeUploadJobHanlder uploadJob; try { uploadJob = new S3ForgeUploadJobHandler(region, Environment.GetEnvironmentVariable("JOB_BUCKET")); } catch (Exception ex) { var response = new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.InternalServerError, Body = JsonConvert.SerializeObject(new { message = ex.Message }), Headers = new Dictionary <string, string> { { "Content-Type", "application/json" } } }; return(response); } try { bool.TryParse(apigProxyEvent.QueryStringParameters.GetValueOrDefault("warmup", "false"), out bool warmUp); if (warmUp) { Console.WriteLine("Warm up request. Writing something to S3..."); await uploadJob.CreateJob(new StartJobRequest { ForgeBucketKey = "StartJob", ForgeModelName = "StartJob", S3ObjectKey = "StartJob", S3BucketName = "StartJob", }); Console.WriteLine("Warm up request. Exiting..."); return(WarmUpResponse()); } string jobId = apigProxyEvent.QueryStringParameters["jobId"]; Console.WriteLine($"Getting status for Job with id: {jobId}..."); var job = await uploadJob.GetJob(jobId); Console.WriteLine($"Status is: {job.State}"); if (job.State == IForgeUploadJobHanlder.JobState.CONVERTING) { if (job.ConversionStartResult != null) { var urn = job.ConversionStartResult.urn.ToString(); Console.WriteLine($"urn = {urn}"); Console.WriteLine($"Retrieving conversion status from Forge..."); var conversionStatus = await ModelDerivative.CheckTranslationStatus(urn); Console.WriteLine($"Conversion status = {conversionStatus?.progress?.ToString()}"); if (conversionStatus?.progress?.ToString() == "complete") { job = await uploadJob.FromConvertingToCompleted(jobId, conversionStatus.ToJson()); } else { job = await uploadJob.UpdateConversionStatus(jobId, conversionStatus.ToJson()); } } } var response = new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.OK, Body = JsonConvert.SerializeObject(job), Headers = new Dictionary <string, string> { { "Content-Type", "application/json" } } }; return(response); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); var response = new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.InternalServerError, Body = JsonConvert.SerializeObject(new { message = ex.Message }), Headers = new Dictionary <string, string> { { "Content-Type", "application/json" } } }; return(response); } }
public async Task <APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context) { var regionFromEnv = System.Environment.GetEnvironmentVariable("AWS_REGION"); Console.WriteLine($"System.Environment.GetEnvironmentVariable(\"AWS_REGION\") = {regionFromEnv}"); var region = RegionEndpoint.GetBySystemName(regionFromEnv); IForgeUploadJobHanlder uploadJob; string jobId = ""; Console.WriteLine("Function started"); Console.WriteLine(JsonConvert.SerializeObject(apigProxyEvent)); try { uploadJob = new S3ForgeUploadJobHandler(region, System.Environment.GetEnvironmentVariable("JOB_BUCKET")); } catch (Exception ex) { var response = new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.InternalServerError, Body = JsonConvert.SerializeObject(new { message = ex.Message }), Headers = new Dictionary <string, string> { { "Content-Type", "application/json" } } }; return(response); } try { var uploadRequest = JsonConvert.DeserializeObject <UploadRequest>(apigProxyEvent.Body); if (uploadRequest.WarmUp) { Console.WriteLine("Warm up request. Writing something to S3..."); await uploadJob.CreateJob(new StartJobRequest { ForgeBucketKey = "StartJob", ForgeModelName = "StartJob", S3ObjectKey = "StartJob", S3BucketName = "StartJob", }); Console.WriteLine("Warm up request. Exiting..."); return(WarmUpResponse()); } var client = new AmazonLambdaClient(region); Console.WriteLine("Initializing job status on S3..."); jobId = await uploadJob.CreateJob(new StartJobRequest { ForgeBucketKey = uploadRequest.ForgeBucketKey, ForgeModelName = uploadRequest.ForgeModelName, S3ObjectKey = uploadRequest.S3ObjectKey, S3BucketName = uploadRequest.S3BucketName }); var lambdaRequest = new UploadRequest { S3ObjectKey = uploadRequest.S3ObjectKey, S3BucketName = uploadRequest.S3BucketName, ForgeBucketKey = uploadRequest.ForgeBucketKey, ForgeModelName = uploadRequest.ForgeModelName, JobId = jobId }; var request = new InvokeRequest { FunctionName = System.Environment.GetEnvironmentVariable("WORKER_FUNCTION"), InvocationType = InvocationType.Event, Payload = JsonConvert.SerializeObject(lambdaRequest) }; Console.WriteLine("Invoking worker lambda..."); var invokeResponse = await client.InvokeAsync(request); Console.WriteLine($"Job: {jobId}. Invoked {request.FunctionName} with payload {JsonConvert.SerializeObject(uploadRequest)}. StatusCode = {invokeResponse.StatusCode}."); var result = new { JobId = jobId }; var response = new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.OK, Body = JsonConvert.SerializeObject(result), Headers = new Dictionary <string, string> { { "Content-Type", "application/json" } } }; return(response); } catch (Exception ex) { try { if (!string.IsNullOrEmpty(jobId)) { await uploadJob.SetError(jobId, ex.Message); } } catch { }; var response = new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.InternalServerError, Body = JsonConvert.SerializeObject(new { message = ex.Message }), Headers = new Dictionary <string, string> { { "Content-Type", "application/json" } } }; return(response); } }