Skip to content

Commit

Permalink
feat(serverless_jobs): add run options when starting a job (scaleway#421
Browse files Browse the repository at this point in the history
)
  • Loading branch information
scaleway-bot authored Jan 11, 2024
1 parent 38f409d commit 07111c0
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 0 deletions.
22 changes: 22 additions & 0 deletions scaleway-async/scaleway_async/jobs/v1alpha1/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
UpdateJobDefinitionRequestCronScheduleConfig,
CreateJobDefinitionRequest,
UpdateJobDefinitionRequest,
StartJobDefinitionRequest,
)
from .marshalling import (
marshal_CreateJobDefinitionRequest,
marshal_StartJobDefinitionRequest,
marshal_UpdateJobDefinitionRequest,
unmarshal_JobDefinition,
unmarshal_JobRun,
Expand Down Expand Up @@ -327,11 +329,21 @@ async def start_job_definition(
*,
job_definition_id: str,
region: Optional[Region] = None,
command: Optional[str] = None,
environment_variables: Optional[Dict[str, str]] = None,
replicas: Optional[int] = None,
) -> JobRun:
"""
Run an existing job definition by its unique identifier. This will create a new job run.
:param region: Region to target. If none is passed will use default region from the config.
:param job_definition_id: UUID of the job definition to start.
:param command: Contextual startup command for this specific job run.
One-of ('_command'): at most one of 'command' could be set.
:param environment_variables: Contextual environment variables for this specific job run.
:param replicas: Number of jobs to run.
One-of ('_replicas'): at most one of 'replicas' could be set.
:return: :class:`JobRun <JobRun>`
Usage:
Expand All @@ -350,6 +362,16 @@ async def start_job_definition(
res = self._request(
"POST",
f"/serverless-jobs/v1alpha1/regions/{param_region}/job-definitions/{param_job_definition_id}/start",
body=marshal_StartJobDefinitionRequest(
StartJobDefinitionRequest(
job_definition_id=job_definition_id,
region=region,
command=command,
environment_variables=environment_variables,
replicas=replicas,
),
self.client,
),
)

self._throw_on_error(res)
Expand Down
38 changes: 38 additions & 0 deletions scaleway-async/scaleway_async/jobs/v1alpha1/marshalling.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
UpdateJobDefinitionRequestCronScheduleConfig,
CreateJobDefinitionRequest,
UpdateJobDefinitionRequest,
StartJobDefinitionRequest,
)


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

args: Dict[str, Any] = {}

field = data.get("command", None)
args["command"] = field

field = data.get("cpu_limit", None)
args["cpu_limit"] = field

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

field = data.get("environment_variables", None)
args["environment_variables"] = field

field = data.get("error_message", None)
args["error_message"] = field

Expand Down Expand Up @@ -262,6 +269,37 @@ def marshal_CreateJobDefinitionRequest(
return output


def marshal_StartJobDefinitionRequest(
request: StartJobDefinitionRequest,
defaults: ProfileDefaults,
) -> Dict[str, Any]:
output: Dict[str, Any] = {}
output.update(
resolve_one_of(
[
OneOfPossibility(
"command", request.command if request.command is not None else None
),
]
),
)
output.update(
resolve_one_of(
[
OneOfPossibility(
"replicas",
request.replicas if request.replicas is not None else None,
),
]
),
)

if request.environment_variables is not None:
output["environment_variables"] = request.environment_variables

return output


def marshal_UpdateJobDefinitionRequest(
request: UpdateJobDefinitionRequest,
defaults: ProfileDefaults,
Expand Down
23 changes: 23 additions & 0 deletions scaleway-async/scaleway_async/jobs/v1alpha1/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ class JobRun:

memory_limit: int

command: str

environment_variables: Dict[str, str]

region: Region


Expand Down Expand Up @@ -311,6 +315,25 @@ class StartJobDefinitionRequest:
UUID of the job definition to start.
"""

command: Optional[str]
"""
Contextual startup command for this specific job run.
One-of ('_command'): at most one of 'command' could be set.
"""

environment_variables: Optional[Dict[str, str]]
"""
Contextual environment variables for this specific job run.
"""

replicas: Optional[int]
"""
Number of jobs to run.
One-of ('_replicas'): at most one of 'replicas' could be set.
"""


@dataclass
class GetJobRunRequest:
Expand Down
22 changes: 22 additions & 0 deletions scaleway/scaleway/jobs/v1alpha1/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
UpdateJobDefinitionRequestCronScheduleConfig,
CreateJobDefinitionRequest,
UpdateJobDefinitionRequest,
StartJobDefinitionRequest,
)
from .marshalling import (
marshal_CreateJobDefinitionRequest,
marshal_StartJobDefinitionRequest,
marshal_UpdateJobDefinitionRequest,
unmarshal_JobDefinition,
unmarshal_JobRun,
Expand Down Expand Up @@ -327,11 +329,21 @@ def start_job_definition(
*,
job_definition_id: str,
region: Optional[Region] = None,
command: Optional[str] = None,
environment_variables: Optional[Dict[str, str]] = None,
replicas: Optional[int] = None,
) -> JobRun:
"""
Run an existing job definition by its unique identifier. This will create a new job run.
:param region: Region to target. If none is passed will use default region from the config.
:param job_definition_id: UUID of the job definition to start.
:param command: Contextual startup command for this specific job run.
One-of ('_command'): at most one of 'command' could be set.
:param environment_variables: Contextual environment variables for this specific job run.
:param replicas: Number of jobs to run.
One-of ('_replicas'): at most one of 'replicas' could be set.
:return: :class:`JobRun <JobRun>`
Usage:
Expand All @@ -350,6 +362,16 @@ def start_job_definition(
res = self._request(
"POST",
f"/serverless-jobs/v1alpha1/regions/{param_region}/job-definitions/{param_job_definition_id}/start",
body=marshal_StartJobDefinitionRequest(
StartJobDefinitionRequest(
job_definition_id=job_definition_id,
region=region,
command=command,
environment_variables=environment_variables,
replicas=replicas,
),
self.client,
),
)

self._throw_on_error(res)
Expand Down
38 changes: 38 additions & 0 deletions scaleway/scaleway/jobs/v1alpha1/marshalling.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
UpdateJobDefinitionRequestCronScheduleConfig,
CreateJobDefinitionRequest,
UpdateJobDefinitionRequest,
StartJobDefinitionRequest,
)


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

args: Dict[str, Any] = {}

field = data.get("command", None)
args["command"] = field

field = data.get("cpu_limit", None)
args["cpu_limit"] = field

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

field = data.get("environment_variables", None)
args["environment_variables"] = field

field = data.get("error_message", None)
args["error_message"] = field

Expand Down Expand Up @@ -262,6 +269,37 @@ def marshal_CreateJobDefinitionRequest(
return output


def marshal_StartJobDefinitionRequest(
request: StartJobDefinitionRequest,
defaults: ProfileDefaults,
) -> Dict[str, Any]:
output: Dict[str, Any] = {}
output.update(
resolve_one_of(
[
OneOfPossibility(
"command", request.command if request.command is not None else None
),
]
),
)
output.update(
resolve_one_of(
[
OneOfPossibility(
"replicas",
request.replicas if request.replicas is not None else None,
),
]
),
)

if request.environment_variables is not None:
output["environment_variables"] = request.environment_variables

return output


def marshal_UpdateJobDefinitionRequest(
request: UpdateJobDefinitionRequest,
defaults: ProfileDefaults,
Expand Down
23 changes: 23 additions & 0 deletions scaleway/scaleway/jobs/v1alpha1/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ class JobRun:

memory_limit: int

command: str

environment_variables: Dict[str, str]

region: Region


Expand Down Expand Up @@ -311,6 +315,25 @@ class StartJobDefinitionRequest:
UUID of the job definition to start.
"""

command: Optional[str]
"""
Contextual startup command for this specific job run.
One-of ('_command'): at most one of 'command' could be set.
"""

environment_variables: Optional[Dict[str, str]]
"""
Contextual environment variables for this specific job run.
"""

replicas: Optional[int]
"""
Number of jobs to run.
One-of ('_replicas'): at most one of 'replicas' could be set.
"""


@dataclass
class GetJobRunRequest:
Expand Down

0 comments on commit 07111c0

Please sign in to comment.