module "cron_jobs" {
  for_each = local.cron_jobs
  source   = "git@github.com:theorchard/terraform-fargate.git//?ref=6.5.0"

  providers = {
    aws.dns = aws.networking
  }

  environment                        = var.environment
  service_name                       = each.value["service_name"]
  aws_region                         = var.aws_region
  application_family                 = var.application_family
  additional_tags                    = { map-migrated = var.map_migrated_tag }
  non_ecr_image                      = "${var.shared_account_id}.dkr.ecr.${var.aws_region}.amazonaws.com/${var.config_service_name}:latest"
  task_type                          = "worker"
  health_check_command               = "pgrep php"
  deployment_minimum_healthy_percent = "100"
  autoscaling_cpu_policy_enabled     = false
  autoscaling_memory_policy_enabled  = false
  desired_task_count                 = 0
  minimum_capacity                   = 0
  maximum_capacity                   = 1
  task_cpu                           = 2048
  task_memory                        = 8192
  task_protection_policy_enabled     = true
  container_port                     = 8080
  health_check_grace_period_seconds  = 300
  container_start_period_seconds     = 300
  target_deregistration_delay        = 300
  splitio_enabled                    = false
  ows_machine_to_machine_enabled     = false
  cloudwatch_event_enabled           = true
  cloudwatch_event_schedule          = each.value["schedule"]
  vpc_id                             = module.vpc_info.vpc_id
  fargate_service_subnets            = module.vpc_info.default_private_subnet_ids
  stopped_task_monitoring_enabled    = true

  iam_managed_policy_attachments = [
    aws_iam_policy.s3_configs_read_only_policy.arn,
    aws_iam_policy.orchard_repo_cron_worker_kms_policy.arn,
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      APPLICATION_ENV = "production"
    },
    {
      DD_TRACE_ANALYTICS_ENABLED = "true"
    },
    {
      DD_TRACE_APP_NAME = each.value["service_name"]
    },
    {
      DD_TRACE_GLOBAL_TAGS = "env:${var.environment}"
    },
    {
      PATH_FROM_REPO_ROOT_OF_PHP_SCRIPT_TO_RUN = each.value["script_to_run"]
    },
    {
      PHP_DISPLAY_ERRORS = "false"
    },
    {
      SENTRY_DSN = "https://c0e8157de78d448ca46774ca98f7c094@o22178.ingest.us.sentry.io/67618"
    },
    {
      PHP_MEMORY_LIMIT = "7680M"
    },
    {
      SCRIPT_ARGUMENTS = each.value["script_arguments"]
    },
    {
      S3_CONFIG_LOCATION = "${var.config_s3_bucket}/${var.config_service_name}/${var.environment}"
    },
    {
      LC_ALL = "en_US.UTF-8"
    },
    {
      LANG = "en_US.UTF-8"
    },
    {
      LANGUAGE = "en_US.UTF-8"
    },
    {
      SPLITIO_REDIS_HOST = data.aws_elasticache_replication_group.split_synchronizer_redis_cache.primary_endpoint_address
    },
    {
      SPLITIO_REDIS_PORT = data.aws_elasticache_replication_group.split_synchronizer_redis_cache.port
    },
  ]
  secrets = [
    {
      SPLITIO_API_KEY = "${var.environment}/split/API_KEY"
    },
  ]
}
