{
  "Comment": "Insights ISRC availability workflow",
  "StartAt": "Invoke qa-lambda-get-isrc-batches-from-snowflake",
  "States": {
    "Invoke qa-lambda-get-isrc-batches-from-snowflake": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "${lambda_get_isrc_batches_from_snowflake_arn}",
        "Payload.$": "$"
      },
      "Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 1,
          "MaxAttempts": 3,
          "BackoffRate": 2,
          "JitterStrategy": "FULL"
        }
      ],
      "Next": "Search isrcs in insights in parallel",
      "ResultSelector": {
        "total_isrcs.$": "$.Payload.total_isrcs",
        "num_of_batches.$": "$.Payload.num_of_batches",
        "run_number": 0,
        "s3_bucket.$": "$.Payload.s3_bucket",
        "s3_key.$": "$.Payload.s3_key"
      }
    },
    "Search isrcs in insights in parallel": {
      "Type": "Map",
      "ItemProcessor": {
        "ProcessorConfig": {
          "Mode": "DISTRIBUTED",
          "ExecutionType": "STANDARD"
        },
        "StartAt": "Invoke qa-lambda-sme-isrc-finder",
        "States": {
          "Invoke qa-lambda-sme-isrc-finder": {
            "Type": "Task",
            "Resource": "arn:aws:states:::lambda:invoke",
            "OutputPath": "$.Payload",
            "Parameters": {
              "Payload.$": "$",
              "FunctionName": "${lambda_sme_isrc_finder_arn}"
            },
            "Retry": [
              {
                "ErrorEquals": [
                  "Lambda.ServiceException",
                  "Lambda.AWSLambdaException",
                  "Lambda.SdkClientException",
                  "Lambda.TooManyRequestsException"
                ],
                "IntervalSeconds": 1,
                "MaxAttempts": 3,
                "BackoffRate": 2,
                "JitterStrategy": "FULL"
              }
            ],
            "Next": "Check for missing isrcs"
          },
          "Check for missing isrcs": {
            "Type": "Choice",
            "Choices": [
              {
                "Variable": "$.number_of_missing_isrcs",
                "NumericEquals": 0,
                "Next": "Success"
              },
              {
                "Variable": "$.run_number",
                "NumericGreaterThanEquals": ${maximum_lambda_runs},
                "Next": "Success"
              }
            ],
            "Default": "Wait",
            "Assign": {}
          },
          "Success": {
            "Type": "Succeed"
          },
          "Wait": {
            "Type": "Wait",
            "Seconds": ${wait_time_between_runs},
            "Next": "Invoke qa-lambda-sme-isrc-finder"
          }
        }
      },
      "ItemSelector": {
        "isrcs.$": "$$.Map.Item.Value",
        "run_number.$": "$.run_number"
      },
      "Label": "Searchisrcsininsightsinparallel",
      "MaxConcurrency": 1000,
      "ItemReader": {
        "Resource": "arn:aws:states:::s3:getObject",
        "ReaderConfig": {
          "InputType": "JSON"
        },
        "Parameters": {
          "Bucket.$": "$.s3_bucket",
          "Key.$": "$.s3_key"
        }
      },
      "End": true,
      "ResultWriter": {
        "Resource": "arn:aws:states:::s3:putObject",
        "Parameters": {
          "Bucket": "${environment}-${bucket_name}",
          "Prefix": "map_output"
        }
      }
    }
  }
}
