Search code examples
dockersidekiqamazon-ecs

Name Sidekiq worker process with ECS task ID


We use ECS for our applications, but the processes in the WebUI get rather useless names like 66ef25674344. I'd prefer if they would be named after the ECS task ID they run in.


Solution

  • I figured it out myself. Sidekiq uses for the process name either the DYNO environment variable (probably a Heroku thing) or the hostname of the server it's running in (see source). In Docker, the hostname is set per default to the container ID, which doesn't really reflect anything we can see in the ECS UI.

    If we set the DYNO envvar before the process starts, it'll be used as the process name. This can be still done in the config/initializers/sidekiq.rb, so we don't need to parse JSON in a shell script or so:

    if ENV.key?("ECS_CONTAINER_METADATA_FILE")
      ENV["DYNO"] = JSON.parse(
        File.read(ENV["ECS_CONTAINER_METADATA_FILE"])
      )["TaskARN"].split("/").last
    end
    

    Note that the ECS_CONTAINER_METADATA_FILE envvar and file it refers to are disabled by default. Here the guide to enable the metadata file.