locals {
  SUBDOMAIN = "jellyfin." // End with dot or leave blamk for root domain
  DOMAIN = "example.com"
  TRAEFIK_DOMAIN = "${local.SUBDOMAIN}${local.DOMAIN}"
}

job "jellyfin" {
  datacenters = ["dc1"]
  type = "service"

    group "application" {
    count = 1

    network {
      mode = "host"
      port "httpIngress" { static = 8096 }
      port "serviceDiscovery" { static = 1900 }
      port "clientDiscovery" { static = 7359 }
    }

    volume "jellyfin-cache" {
      type      = "host"
      source    = "jellyfinCache"
    }

    volume "jellyfin-config" {
      type      = "host"
      source    = "jellyfinConfig"
    }

    volume "jellyfin-data" {
      type      = "host"
      source    = "media"
    }

    service {
      name = "jellyfin"
      port = "httpIngress"

      tags = [
        "traefik.enable=true",
        "traefik.http.routers.jellyfin.tls=true",
        "traefik.http.routers.jellyfin.entrypoints=websecure",
        "traefik.http.routers.jellyfin.rule=Host(`${local.TRAEFIK_DOMAIN}`)"
      ]

      check {
        type = "http"
        path = "/health"
        interval = "10s"
        timeout = "2s"
      }
    }

    task "jellyfin" {
      driver = "docker"

      config {
        image = "jellyfin/jellyfin:2024080505"
        ports = ["httpIngress", "serviceDiscovery", "clientDiscovery"]
      }

      env = {
          JELLYFIN_PublishedServerUrl="${local.TRAEFIK_DOMAIN}"
      }

      volume_mount {
        volume      = "jellyfin-cache"
        destination = "/cache"
      }

      volume_mount {
        volume      = "jellyfin-config"
        destination = "/config"
      }

      volume_mount {
        volume      = "jellyfin-data"
        destination = "/media"
      }

      resources {
        cpu = 2000
        memory = 1024
        memory_max = 2048
      }
    }
  }
}