{
  "StartAt": "SetupWorkflow",
  "States": {
    "SetupWorkflow": {
      "Type": "Pass",
      "Parameters": {
        "note": "State machine workflow setup",
        "start_time.$": "$$.Execution.StartTime",
        "execution_id.$": "$$.Execution.Id",
        "execution_name.$": "$$.Execution.Name",
        "status": "OK",
        "slack_channel": "#{slzNotificationSlackChannel}"
      },
      "ResultPath": "$.info",
      "Next": "IsReprocessingStart"
    },
    "IsReprocessingStart": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.is_reprocessing",
          "BooleanEquals": true,
          "Next": "CleanUpTask"
        }
      ],
      "Default": "AppleMusicPipelineConfigurator"
    },
    "CleanUpTask": {
      "Type": "Task",
      "Resource": "#{slzCleanUpJobs_arn}",
      "ResultPath": "$.cleanUpResult",
      "Catch": [
        {
          "ErrorEquals": [ "States.ALL" ],
          "ResultPath": "$.info.error",
          "Next": "MarkAsFailed"
        }
      ],
      "Retry": [
        {
          "ErrorEquals": ["States.TaskFailed"],
          "IntervalSeconds": 3,
          "MaxAttempts": 3,
          "BackoffRate": 2
        }
      ],
      "Next": "AppleMusicPipelineConfigurator"
    },
    "AppleMusicPipelineConfigurator": {
      "Type": "Task",
      "Resource": "#{slzAppleMusicChartsPipelineConfigurator_arn}",
      "Parameters": {
        "uow_id.$": "$.uow_id",
        "dsp.$": "$.dsp",
        "report_type.$": "$.report_type",
        "subtype.$": "$.subtype",
        "version.$": "$.version",
        "report_date.$": "$.report_date",
        "report_date_nodash.$": "$.report_date_nodash",
        "licensor.$": "$.licensor",
        "extension.$": "$.extension",
        "config_bucket.$": "$.config_bucket",
        "priority.$": "$.priority",
        "contexts.$": "$.contexts",
        "trace_id.$": "$.trace_id",
        "job_id.$": "$$.Execution.Name",
        "validation_thresholds.$": "$.validation_thresholds",
        "optional_config.$": "$.optional_config",
        "info.$": "$.info"
      },
      "ResultPath": "$.scrapper_config",
      "Next": "AppleMusicChartsScraper",
      "Retry": [
        {
          "ErrorEquals": ["States.TaskFailed"],
          "IntervalSeconds": 3,
          "MaxAttempts": 3,
          "BackoffRate": 2
        }
      ],
      "Catch": [
        {
          "ErrorEquals": ["States.ALL"],
          "ResultPath": "$.info.error",
          "Next": "MarkAsFailed"
        }
      ]
    },
    "AppleMusicChartsScraper": {
      "Type": "Task",
      "Resource": "arn:aws:states:::batch:submitJob.sync",
      "Next": "HaveUpdatedContextsChoice",
      "ResultPath": "$.result.scrapper",
      "TimeoutSeconds": 3600,
      "Parameters": {
        "JobName.$": "$.uow_id",
        "JobQueue.$": "$.scrapper_config.apple_music_charts_scrapper.JobQueue",
        "JobDefinition.$": "$.scrapper_config.apple_music_charts_scrapper.JobDefinition",
        "ContainerOverrides": {
          "Command.$": "$.scrapper_config.apple_music_charts_scrapper.Command",
          "Environment": [
            {
              "Name": "SFN_TASK_TOKEN",
              "Value.$": "$$.Task.Token"
            }
          ]
        }
      },
      "Catch": [
        {
          "ErrorEquals": ["States.ALL"],
          "ResultPath": "$.info.error",
          "Next": "MarkAsFailed"
        }
      ]
    },
    "HaveUpdatedContextsChoice": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.result.scrapper.meta.has_changed",
          "BooleanEquals": true,
          "Next": "AppleMusicChartsDataProcessor"
        }
      ],
      "Default": "SetUoWStatusNotInProgress"
    },
    "AppleMusicChartsDataProcessor": {
      "Type": "Task",
      "Resource": "#{slzAppleMusicChartsDataProcessor_arn}",
      "Parameters": {
        "report_date.$": "$.report_date",
        "report_date_nodash.$": "$.report_date_nodash",
        "job_id.$": "$$.Execution.Name",
        "trace_id.$": "$.trace_id",
        "dsp.$": "$.dsp",
        "licensor.$": "$.licensor",
        "version.$": "$.version",
        "report_type.$": "$.report_type",
        "info.$": "$.info"
      },
      "ResultPath": "$.result.data_processor",
      "Next": "SetUoWStatusNotInProgress",
      "Retry": [
        {
          "ErrorEquals": ["States.TaskFailed"],
          "IntervalSeconds": 3,
          "MaxAttempts": 3,
          "BackoffRate": 2
        }
      ],
      "Catch": [
        {
          "ErrorEquals": ["States.ALL"],
          "ResultPath": "$.info.error",
          "Next": "MarkAsFailed"
        }
      ]
    },
    "MarkAsFailed": {
      "Type": "Pass",
      "Result": "FAIL",
      "ResultPath": "$.info.status",
      "Next": "SetUoWStatusNotInProgress"
    },
    "SetUoWStatusNotInProgress": {
      "Type": "Task",
      "Resource": "#{slzUoWReset_arn}",
      "Parameters": {
        "uow_id.$": "$.uow_id",
        "trace_id.$": "$.trace_id",
        "optional_config.$": "$.optional_config"
      },
      "ResultPath": "$.result",
      "Next": "CheckJobStatus",
      "Catch": [
        {
          "ErrorEquals": ["States.ALL"],
          "ResultPath": "$.info.error",
          "Next": "FailureSlackNotify"
        }
      ],
      "Retry": [
        {
          "ErrorEquals": ["States.TaskFailed"],
          "IntervalSeconds": 3,
          "MaxAttempts": 3,
          "BackoffRate": 2
        }
      ]
    },
    "CheckJobStatus": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.info.status",
          "StringEquals": "FAIL",
          "Next": "FailureSlackNotify"
        }
      ],
      "Default": "Success"
    },
    "FailureSlackNotify": {
      "Type": "Task",
      "Resource": "#{slzSlackNotify_arn}",
      "Parameters": {
        "uow_id.$": "$.uow_id",
        "channel.$": "$.info.slack_channel",
        "message.$": "$.info"
      },
      "Catch": [
        {
          "ErrorEquals": ["States.ALL"],
          "ResultPath": "$.info.error",
          "Next": "Failure"
        }
      ],
      "Retry": [
        {
          "ErrorEquals": ["States.TaskFailed"],
          "IntervalSeconds": 3,
          "MaxAttempts": 3,
          "BackoffRate": 2
        }
      ],
      "Next": "Failure"
    },
    "Failure": {
      "Type": "Fail"
    },
    "Success": {
      "Type": "Succeed"
    }
  }
}
