Skip to content

Commit 433e32e

Browse files
ahmedwonoloaas47
authored andcommitted
Update dispatch_jobs to support idempotency_token and IdPrefixTemplate
1 parent 060ced3 commit 433e32e

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
fail-fast: false
2424
matrix:
2525
python-version: ['3.7', '3.12'] # the oldest and newest support versions
26-
nomad-version: ['1.2.16', '1.3.16', '1.4.14', '1.5.17', '1.6.10', '1.7.7']
26+
nomad-version: ['1.4.14', '1.5.17', '1.6.10', '1.7.7', '1.8.4', '1.9.5']
2727
steps:
2828
- uses: actions/checkout@v4
2929
- name: Set up Python ${{ matrix.python-version }}
@@ -60,4 +60,4 @@ jobs:
6060
run: |
6161
./run_tests.sh
6262
- name: Upload coverage to Codecov
63-
uses: codecov/codecov-action@v4
63+
uses: codecov/codecov-action@v4

nomad/api/job.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def periodic_job(self, id_):
245245
"""
246246
return self.request(id_, "periodic", "force", method="post").json()
247247

248-
def dispatch_job(self, id_, payload=None, meta=None):
248+
def dispatch_job(self, id_, payload=None, meta=None, id_prefix_template=None, idempotency_token=None):
249249
"""Dispatches a new instance of a parameterized job.
250250
251251
https://www.nomadproject.io/docs/http/job.html
@@ -254,12 +254,14 @@ def dispatch_job(self, id_, payload=None, meta=None):
254254
- id_
255255
- payload
256256
- meta
257+
- id_prefix_template
258+
- idempotency_token
257259
returns: dict
258260
raises:
259261
- nomad.api.exceptions.BaseNomadException
260262
- nomad.api.exceptions.URLNotFoundNomadException
261263
"""
262-
dispatch_json = {"Meta": meta, "Payload": payload}
264+
dispatch_json = {"Meta": meta, "Payload": payload, "idempotency_token": idempotency_token, "IdPrefixTemplate": id_prefix_template}
263265
return self.request(id_, "dispatch", json=dispatch_json, method="post").json()
264266

265267
def revert_job(self, id_, version, enforce_prior_version=None):

tests/test_job.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,34 @@ def test_dispatch_job(nomad_setup):
133133
job = json.loads(fh.read())
134134
nomad_setup.job.register_job("example-batch", job)
135135
try:
136-
nomad_setup.job.dispatch_job("example-batch", meta={"time": "500"})
136+
nomad_setup.job.dispatch_job("example-batch", meta={"time": "500"}, id_prefix_template="run1")
137137
except (exceptions.URLNotFoundNomadException, exceptions.BaseNomadException) as e:
138138
print(e.nomad_resp.text)
139139
raise e
140140
assert "example-batch" in nomad_setup.job
141141

142142

143+
@flaky(max_runs=5, min_passes=1)
144+
@pytest.mark.skipif(
145+
tuple(int(i) for i in os.environ.get("NOMAD_VERSION").split(".")) < (0, 5, 3), reason="Nomad dispatch not supported"
146+
)
147+
def test_dispatch_job_idempotency(nomad_setup):
148+
with open("example_batch_parameterized.json") as fh:
149+
job = json.loads(fh.read())
150+
nomad_setup.job.register_job("example-batch-idempotent", job)
151+
152+
# First dispatch should succeed
153+
try:
154+
nomad_setup.job.dispatch_job("example-batch-idempotent", meta={"time": "500"}, id_prefix_template="run1", idempotency_token="737ae8cd-f237-43a5-8fad-0e6a3f94ad55")
155+
except (exceptions.URLNotFoundNomadException, exceptions.BaseNomadException) as e:
156+
print(e.nomad_resp.text)
157+
raise e
158+
assert "example-batch-idempotent" in nomad_setup.job
159+
160+
# Second dispatch with the same idempotency token should fail
161+
with pytest.raises(exceptions.BaseNomadException):
162+
nomad_setup.job.dispatch_job("example-batch-idempotent", meta={"time": "500"}, id_prefix_template="run2", idempotency_token="737ae8cd-f237-43a5-8fad-0e6a3f94ad55")
163+
143164
@pytest.mark.skipif(
144165
tuple(int(i) for i in os.environ.get("NOMAD_VERSION").split(".")) < (0, 5, 3), reason="Nomad dispatch not supported"
145166
)

0 commit comments

Comments
 (0)