Skip to content

Commit 07111c0

Browse files
authored
feat(serverless_jobs): add run options when starting a job (#421)
1 parent 38f409d commit 07111c0

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

scaleway-async/scaleway_async/jobs/v1alpha1/api.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
UpdateJobDefinitionRequestCronScheduleConfig,
2424
CreateJobDefinitionRequest,
2525
UpdateJobDefinitionRequest,
26+
StartJobDefinitionRequest,
2627
)
2728
from .marshalling import (
2829
marshal_CreateJobDefinitionRequest,
30+
marshal_StartJobDefinitionRequest,
2931
marshal_UpdateJobDefinitionRequest,
3032
unmarshal_JobDefinition,
3133
unmarshal_JobRun,
@@ -327,11 +329,21 @@ async def start_job_definition(
327329
*,
328330
job_definition_id: str,
329331
region: Optional[Region] = None,
332+
command: Optional[str] = None,
333+
environment_variables: Optional[Dict[str, str]] = None,
334+
replicas: Optional[int] = None,
330335
) -> JobRun:
331336
"""
332337
Run an existing job definition by its unique identifier. This will create a new job run.
333338
:param region: Region to target. If none is passed will use default region from the config.
334339
:param job_definition_id: UUID of the job definition to start.
340+
:param command: Contextual startup command for this specific job run.
341+
342+
One-of ('_command'): at most one of 'command' could be set.
343+
:param environment_variables: Contextual environment variables for this specific job run.
344+
:param replicas: Number of jobs to run.
345+
346+
One-of ('_replicas'): at most one of 'replicas' could be set.
335347
:return: :class:`JobRun <JobRun>`
336348
337349
Usage:
@@ -350,6 +362,16 @@ async def start_job_definition(
350362
res = self._request(
351363
"POST",
352364
f"/serverless-jobs/v1alpha1/regions/{param_region}/job-definitions/{param_job_definition_id}/start",
365+
body=marshal_StartJobDefinitionRequest(
366+
StartJobDefinitionRequest(
367+
job_definition_id=job_definition_id,
368+
region=region,
369+
command=command,
370+
environment_variables=environment_variables,
371+
replicas=replicas,
372+
),
373+
self.client,
374+
),
353375
)
354376

355377
self._throw_on_error(res)

scaleway-async/scaleway_async/jobs/v1alpha1/marshalling.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
UpdateJobDefinitionRequestCronScheduleConfig,
2020
CreateJobDefinitionRequest,
2121
UpdateJobDefinitionRequest,
22+
StartJobDefinitionRequest,
2223
)
2324

2425

@@ -100,12 +101,18 @@ def unmarshal_JobRun(data: Any) -> JobRun:
100101

101102
args: Dict[str, Any] = {}
102103

104+
field = data.get("command", None)
105+
args["command"] = field
106+
103107
field = data.get("cpu_limit", None)
104108
args["cpu_limit"] = field
105109

106110
field = data.get("created_at", None)
107111
args["created_at"] = parser.isoparse(field) if type(field) is str else field
108112

113+
field = data.get("environment_variables", None)
114+
args["environment_variables"] = field
115+
109116
field = data.get("error_message", None)
110117
args["error_message"] = field
111118

@@ -262,6 +269,37 @@ def marshal_CreateJobDefinitionRequest(
262269
return output
263270

264271

272+
def marshal_StartJobDefinitionRequest(
273+
request: StartJobDefinitionRequest,
274+
defaults: ProfileDefaults,
275+
) -> Dict[str, Any]:
276+
output: Dict[str, Any] = {}
277+
output.update(
278+
resolve_one_of(
279+
[
280+
OneOfPossibility(
281+
"command", request.command if request.command is not None else None
282+
),
283+
]
284+
),
285+
)
286+
output.update(
287+
resolve_one_of(
288+
[
289+
OneOfPossibility(
290+
"replicas",
291+
request.replicas if request.replicas is not None else None,
292+
),
293+
]
294+
),
295+
)
296+
297+
if request.environment_variables is not None:
298+
output["environment_variables"] = request.environment_variables
299+
300+
return output
301+
302+
265303
def marshal_UpdateJobDefinitionRequest(
266304
request: UpdateJobDefinitionRequest,
267305
defaults: ProfileDefaults,

scaleway-async/scaleway_async/jobs/v1alpha1/types.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ class JobRun:
117117

118118
memory_limit: int
119119

120+
command: str
121+
122+
environment_variables: Dict[str, str]
123+
120124
region: Region
121125

122126

@@ -311,6 +315,25 @@ class StartJobDefinitionRequest:
311315
UUID of the job definition to start.
312316
"""
313317

318+
command: Optional[str]
319+
"""
320+
Contextual startup command for this specific job run.
321+
322+
One-of ('_command'): at most one of 'command' could be set.
323+
"""
324+
325+
environment_variables: Optional[Dict[str, str]]
326+
"""
327+
Contextual environment variables for this specific job run.
328+
"""
329+
330+
replicas: Optional[int]
331+
"""
332+
Number of jobs to run.
333+
334+
One-of ('_replicas'): at most one of 'replicas' could be set.
335+
"""
336+
314337

315338
@dataclass
316339
class GetJobRunRequest:

scaleway/scaleway/jobs/v1alpha1/api.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
UpdateJobDefinitionRequestCronScheduleConfig,
2424
CreateJobDefinitionRequest,
2525
UpdateJobDefinitionRequest,
26+
StartJobDefinitionRequest,
2627
)
2728
from .marshalling import (
2829
marshal_CreateJobDefinitionRequest,
30+
marshal_StartJobDefinitionRequest,
2931
marshal_UpdateJobDefinitionRequest,
3032
unmarshal_JobDefinition,
3133
unmarshal_JobRun,
@@ -327,11 +329,21 @@ def start_job_definition(
327329
*,
328330
job_definition_id: str,
329331
region: Optional[Region] = None,
332+
command: Optional[str] = None,
333+
environment_variables: Optional[Dict[str, str]] = None,
334+
replicas: Optional[int] = None,
330335
) -> JobRun:
331336
"""
332337
Run an existing job definition by its unique identifier. This will create a new job run.
333338
:param region: Region to target. If none is passed will use default region from the config.
334339
:param job_definition_id: UUID of the job definition to start.
340+
:param command: Contextual startup command for this specific job run.
341+
342+
One-of ('_command'): at most one of 'command' could be set.
343+
:param environment_variables: Contextual environment variables for this specific job run.
344+
:param replicas: Number of jobs to run.
345+
346+
One-of ('_replicas'): at most one of 'replicas' could be set.
335347
:return: :class:`JobRun <JobRun>`
336348
337349
Usage:
@@ -350,6 +362,16 @@ def start_job_definition(
350362
res = self._request(
351363
"POST",
352364
f"/serverless-jobs/v1alpha1/regions/{param_region}/job-definitions/{param_job_definition_id}/start",
365+
body=marshal_StartJobDefinitionRequest(
366+
StartJobDefinitionRequest(
367+
job_definition_id=job_definition_id,
368+
region=region,
369+
command=command,
370+
environment_variables=environment_variables,
371+
replicas=replicas,
372+
),
373+
self.client,
374+
),
353375
)
354376

355377
self._throw_on_error(res)

scaleway/scaleway/jobs/v1alpha1/marshalling.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
UpdateJobDefinitionRequestCronScheduleConfig,
2020
CreateJobDefinitionRequest,
2121
UpdateJobDefinitionRequest,
22+
StartJobDefinitionRequest,
2223
)
2324

2425

@@ -100,12 +101,18 @@ def unmarshal_JobRun(data: Any) -> JobRun:
100101

101102
args: Dict[str, Any] = {}
102103

104+
field = data.get("command", None)
105+
args["command"] = field
106+
103107
field = data.get("cpu_limit", None)
104108
args["cpu_limit"] = field
105109

106110
field = data.get("created_at", None)
107111
args["created_at"] = parser.isoparse(field) if type(field) is str else field
108112

113+
field = data.get("environment_variables", None)
114+
args["environment_variables"] = field
115+
109116
field = data.get("error_message", None)
110117
args["error_message"] = field
111118

@@ -262,6 +269,37 @@ def marshal_CreateJobDefinitionRequest(
262269
return output
263270

264271

272+
def marshal_StartJobDefinitionRequest(
273+
request: StartJobDefinitionRequest,
274+
defaults: ProfileDefaults,
275+
) -> Dict[str, Any]:
276+
output: Dict[str, Any] = {}
277+
output.update(
278+
resolve_one_of(
279+
[
280+
OneOfPossibility(
281+
"command", request.command if request.command is not None else None
282+
),
283+
]
284+
),
285+
)
286+
output.update(
287+
resolve_one_of(
288+
[
289+
OneOfPossibility(
290+
"replicas",
291+
request.replicas if request.replicas is not None else None,
292+
),
293+
]
294+
),
295+
)
296+
297+
if request.environment_variables is not None:
298+
output["environment_variables"] = request.environment_variables
299+
300+
return output
301+
302+
265303
def marshal_UpdateJobDefinitionRequest(
266304
request: UpdateJobDefinitionRequest,
267305
defaults: ProfileDefaults,

scaleway/scaleway/jobs/v1alpha1/types.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ class JobRun:
117117

118118
memory_limit: int
119119

120+
command: str
121+
122+
environment_variables: Dict[str, str]
123+
120124
region: Region
121125

122126

@@ -311,6 +315,25 @@ class StartJobDefinitionRequest:
311315
UUID of the job definition to start.
312316
"""
313317

318+
command: Optional[str]
319+
"""
320+
Contextual startup command for this specific job run.
321+
322+
One-of ('_command'): at most one of 'command' could be set.
323+
"""
324+
325+
environment_variables: Optional[Dict[str, str]]
326+
"""
327+
Contextual environment variables for this specific job run.
328+
"""
329+
330+
replicas: Optional[int]
331+
"""
332+
Number of jobs to run.
333+
334+
One-of ('_replicas'): at most one of 'replicas' could be set.
335+
"""
336+
314337

315338
@dataclass
316339
class GetJobRunRequest:

0 commit comments

Comments
 (0)