{
    "Comment": "Create a job and Claim a list of contributions for a CMO.",
    "StartAt": "CreateJobs",
    "States": {
      "CreateJobs": {
        "Type": "Task",
        "Resource": "${create_jobs_lambda_arn}",
        "ResultPath": "$.jobs",
        "Retry": [
          {
            "ErrorEquals": [
              "States.Timeout",
              "Lambda.ServiceException",
              "Lambda.AWSLambdaException",
              "Lambda.SdkClientException",
              "Lambda.Unknown",
              "Lambda.TooManyRequestsException"
            ],
            "BackoffRate": 2,
            "IntervalSeconds": 60,
            "MaxAttempts": 3,
            "Comment": "Retry on timeout"
          }
        ],
        "Catch": [
          {
            "ErrorEquals": [
              "States.ALL"
            ],
            "Next": "OrderFinalizer",
            "ResultPath": "$.jobErrors",
            "Comment": "Error after retry"
          }
        ],
        "Next": "ProcessEachJob"
      },
      "ProcessEachJob": {
        "Type": "Map",
        "MaxConcurrency": 30,
        "Next": "PutJobCombinerInput",
        "ItemsPath": "$.jobs",
        "Iterator": {
          "StartAt": "Claimorizer",
          "States": {
            "Claimorizer": {
              "Type": "Task",
              "Resource": "arn:aws:states:::ecs:runTask.waitForTaskToken",
              "TimeoutSeconds": 1800,
              "HeartbeatSeconds": 300,
              "Parameters": {
                "LaunchType": "FARGATE",
                "Cluster": "${fargate_claimorizer_arn}",
                "TaskDefinition": "${fargate_claimorizer_task_arn}",
                "NetworkConfiguration": {
                  "AwsvpcConfiguration": {
                    "Subnets": [
                      "${subnet_id0}",
                      "${subnet_id1}"
                    ],
                    "AssignPublicIp": "DISABLED"
                  }
                },
                "Overrides": {
                  "ContainerOverrides": [
                    {
                      "Name": "${fargate_claimorizer_name}",
                      "Environment": [
                        {
                          "Name": "TASK_TOKEN",
                          "Value.$": "$$.Task.Token"
                        },
                        {
                          "Name": "jobFile",
                          "Value.$": "$.jobFile"
                        }
                      ]
                    }
                  ]
                }
              },
              "Retry": [
                {
                  "ErrorEquals": [
                      "States.Timeout",
                      "States.TaskFailed"
                  ],
                  "BackoffRate": 2,
                  "IntervalSeconds": 60,
                  "MaxAttempts": 3,
                  "Comment": "retry on timeout"
                }
              ],
              "Next": "GetJobsObject",
              "ResultPath": null
            },
            "GetJobsObject": {
              "Type": "Task",
              "Next": "MetadataGenerator",
              "Parameters": {
                "Bucket": "${bucket_name}",
                "Key.$": "$.jobFile"
              },
              "Resource": "arn:aws:states:::aws-sdk:s3:getObject",
              "ResultSelector": {
                "Body.$": "States.StringToJson($.Body)"
              },
              "OutputPath": "$.Body",
              "Retry": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "BackoffRate": 2,
                  "IntervalSeconds": 60,
                  "MaxAttempts": 2
                }
              ]
            },
            "MetadataGenerator": {
              "Type": "Task",
              "Resource": "${metadata_generator_lambda_arn}",
              "ResultPath": "$.metadataResult",
              "Next": "JobFinalizer",
              "Retry": [
                {
                  "ErrorEquals": [
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.Unknown",
                    "Lambda.TooManyRequestsException"
                  ],
                  "BackoffRate": 2,
                  "IntervalSeconds": 60,
                  "MaxAttempts": 3,
                  "Comment": "retry on timeout"
                }
              ]
            },
            "JobFinalizer": {
              "Type": "Task",
              "Resource": "${job_finalizer_lambda_arn}",
              "End": true,
              "Retry": [
                {
                  "ErrorEquals": [
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.Unknown",
                    "Lambda.TooManyRequestsException"
                  ],
                  "BackoffRate": 2,
                  "IntervalSeconds": 60,
                  "MaxAttempts": 3,
                  "Comment": "retry on timeout"
                }
              ]
            }
          },
          "ProcessorConfig": {
            "Mode": "DISTRIBUTED",
            "ExecutionType": "STANDARD"
          }
        },
        "ResultPath": "$.jobs",
        "Catch": [
          {
            "ErrorEquals": [
              "States.ALL"
            ],
            "Next": "OrderFinalizer",
            "ResultPath": "$.jobErrors",
            "Comment": "For all job errors"
          }
        ],
        "Label": "ProcessEachJob"
      },
      "PutJobCombinerInput": {
        "Type": "Task",
        "Next": "JobCombiner",
        "Parameters": {
          "Body.$": "$",
          "Bucket": "${bucket_name}",
          "Key.$": "States.Format('delivery/orders/{}/jobs/job-combiner-input.json', $.orderId)"
        },
        "Resource": "arn:aws:states:::aws-sdk:s3:putObject",
        "ResultPath": null,
        "Retry": [
          {
            "ErrorEquals": [
              "States.ALL"
            ],
            "BackoffRate": 2,
            "IntervalSeconds": 60,
            "MaxAttempts": 2
          }
        ]
      },
      "JobCombiner": {
        "Type": "Task",
        "Resource": "arn:aws:states:::ecs:runTask.sync",
        "TimeoutSeconds": 1800,
        "HeartbeatSeconds": 300,
        "Parameters": {
          "LaunchType": "FARGATE",
          "Cluster": "${fargate_job_combiner_arn}",
          "TaskDefinition": "${fargate_job_combiner_task_arn}",
          "NetworkConfiguration": {
            "AwsvpcConfiguration": {
              "Subnets": [
                "${subnet_id0}",
                "${subnet_id1}"
              ],
              "AssignPublicIp": "DISABLED"
            }
          },
          "Overrides": {
            "ContainerOverrides": [
              {
                "Name": "${fargate_job_combiner_name}",
                "Environment": [
                  {
                    "Name": "TASK_TOKEN",
                    "Value.$": "$$.Task.Token"
                  },
                  {
                    "Name": "jobFile",
                    "Value.$": "States.Format('delivery/orders/{}/jobs/job-combiner-input.json', $.orderId)"
                  }
                ]
              }
            ]
          }
        },
        "Retry": [
          {
            "ErrorEquals": [
              "States.Timeout",
              "States.TaskFailed"
            ],
            "BackoffRate": 2,
            "IntervalSeconds": 60,
            "MaxAttempts": 3,
            "Comment": "retry on timeout"
          }
        ],
        "Next": "OrderFinalizer",
        "ResultPath": null,
        "Catch": [
          {
            "ErrorEquals": [
              "States.ALL"
            ],
            "Next": "OrderFinalizer",
            "ResultPath": "$.jobErrors",
            "Comment": "Error after retry"
          }
        ]
      },
      "OrderFinalizer": {
        "Type": "Task",
        "Resource": "${order_finalizer_lambda_arn}",
        "Retry": [
          {
            "ErrorEquals": [
              "States.Timeout",
              "Lambda.ServiceException",
              "Lambda.AWSLambdaException",
              "Lambda.SdkClientException",
              "Lambda.Unknown",
              "Lambda.TooManyRequestsException"
            ],
            "BackoffRate": 2,
            "IntervalSeconds": 60,
            "MaxAttempts": 3,
            "Comment": "Retry on timeout"
          }
        ],
        "End": true
      }
    }
  }
