{
  "Comment": "crm-datafetch root job",
  "StartAt": "params",
  "States": {
    "params": {
      "Comment": "パラメータ設定",
      "Type": "Pass",
      "Parameters": {
        "sns": {
          "TopicArn": "arn:aws:sns:#{REGION_AP_NORTHEAST_1}:#{AWS_ACCOUNT_ID}:nds-notice-#{ENV_NAME}"
        },
        "ecs": {
          "Cluster": "arn:aws:ecs:#{REGION_AP_NORTHEAST_1}:#{AWS_ACCOUNT_ID}:cluster/mbj-newdwh2021-#{ENV_NAME}-crm-ecs",
          "LaunchType": "FARGATE",
          "NetworkConfiguration": {
            "AwsvpcConfiguration": {
              "Subnets": [
                "#{SUBNET_PRI_1A}",
                "#{SUBNET_PRI_1D}"
              ],
              "SecurityGroups": [
                "#{SG_ECS_ALL}",
                "#{SG_CRM_DATAFETCH}"
              ],
              "AssignPublicIp": "DISABLED"
            }
          }
        }
      },
      "ResultPath": "$.params",
      "Next": "crm-datafetch-diff"
    },
    "crm-datafetch-diff": {
      "Comment": "CRMデータ取得",
      "Type": "Task",
      "Resource": "arn:aws:states:::ecs:runTask.sync",
      "Parameters": {
        "Cluster.$": "$.params.ecs.Cluster",
        "LaunchType.$": "$.params.ecs.LaunchType",
        "TaskDefinition": "arn:aws:ecs:#{REGION_AP_NORTHEAST_1}:#{AWS_ACCOUNT_ID}:task-definition/mbj-newdwh2021-#{ENV_NAME}-task-crm-datafetch",
        "NetworkConfiguration.$": "$.params.ecs.NetworkConfiguration"
      },
      "Retry": [
        {
          "ErrorEquals": ["States.ALL"],
          "BackoffRate": 2,
          "IntervalSeconds": 3,
          "MaxAttempts": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": ["States.ALL"],
          "ResultPath": "$.result",
          "Next": "ErrorEnd"
        }
      ],
      "ResultPath": "$.result",
      "Next": "Wait"
    },
    "Wait": {
      "Type": "Wait",
      "Seconds": 5,
      "Next": "crm-datafetch-all"
    },
    "crm-datafetch-all": {
      "Comment": "CRMデータ全量取得",
      "Type": "Task",
      "Resource": "arn:aws:states:::ecs:runTask.sync",
      "Parameters": {
        "Cluster.$": "$.params.ecs.Cluster",
        "LaunchType.$": "$.params.ecs.LaunchType",
        "TaskDefinition": "arn:aws:ecs:#{REGION_AP_NORTHEAST_1}:#{AWS_ACCOUNT_ID}:task-definition/mbj-newdwh2021-#{ENV_NAME}-task-crm-datafetch",
        "NetworkConfiguration.$": "$.params.ecs.NetworkConfiguration",
        "Overrides": {
          "ContainerOverrides": [
            {
              "Name": "mbj-newdwh2021-#{ENV_NAME}-container-crm-datafetch",
              "Environment": [
                {
                  "Name": "OBJECT_INFO_FILENAME",
                  "Value": "crm_object_list_all.json"
                }
              ]
            }
          ]
        }
      },
      "Retry": [
        {
          "ErrorEquals": ["States.ALL"],
          "BackoffRate": 2,
          "IntervalSeconds": 3,
          "MaxAttempts": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": ["States.ALL"],
          "ResultPath": "$.result",
          "Next": "ErrorEnd"
        }
      ],
      "ResultPath": "$.result",
      "Next": "SuccessNotice"
    },
    "SuccessNotice": {
      "Comment": "正常終了通知",
      "Type": "Task",
      "Resource": "arn:aws:states:::sns:publish",
      "Parameters": {
        "TopicArn.$": "$.params.sns.TopicArn",
        "Subject": "StepFunctions正常終了通知",
        "Message.$": "States.Format('CRMデータ取得処理が完了しました。\n\n 対象のステートマシン：{} \n 実行ID：{}', $$.StateMachine.Id, $$.Execution.Name)"
      },
      "Next": "NormalEnd"
    },
    "NormalEnd": {
      "Comment": "正常終了",
      "Type": "Succeed"
    },
    "ErrorEnd": {
      "Comment": "異常終了",
      "Type": "Fail",
      "Error": "StatesError",
      "Cause": "StepFunctions ErrorEnd"
    }
  }
}
