How to run a task and let it finish

Hi,

I would like to run a service in local machine and let it finish completely.

I started nomad in local machine with -dev flag.

Required state:

  1. Start the process
  2. Finish the process
  3. Restart only if error occurs

Current state:

  1. Start process
  2. Finish Process
  3. Process restarts
  4. Docker images are missing(I am not sure why it is getting deleted in local machine)
  5. Process in pending state due to non availability of the docker.

job "python-app" {
  datacenters = ["dc1"]  
  group "python" {
    count = 2
    
    task "server" {
      driver = "docker"
      config {
        image = "python-app:local"
      }      
      
      service {
        name = "python-flask"
        tags = [
          "linux",
        ]        

      }
    }
  }
}

This is very small app for testing purpose.
app.py

import time

for each in range(100):
    print(each)
    time.sleep(3)

Is the issue something related to Restart and Reschedule stanzas?

From the docs for service :

The service stanza instructs Nomad to register a service with Consul.

It looks like you are running a task which should use the batch scheduler.

Since you are declaring a service, when the process exits, Nomad will automatically restart it, because it thinks that it has died.

Try changing the job type to batch

job "python-app" {
  datacenters = ["dc1"]
  type = "batch"
  group "python" {
    count = 2
    
    task "server" {
      driver = "docker"
      config {
        image = "python-app:local"
      }      
   }
}

Thank you for the fast response.
But when I send this HCL to v1/jobs/parse endpoint to convert it to json for API, I get this error

‘input.hcl:14,1-1: Argument or block definition required; An argument or block definition is required here.’

my apologies - I meant only to indicate how the job spec could be modified, not to give a complete working example.

1 Like

Thank you. It worked.

1 Like