Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
ni-meister-gedi-biomass-global
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ian
ni-meister-gedi-biomass-global
Commits
f253ebf8
Commit
f253ebf8
authored
1 month ago
by
Ian
Committed by
Ian
1 month ago
Browse files
Options
Downloads
Patches
Plain Diff
refactor: wrap MAAP API calls in Job in _safe_request
parent
c0e8dadf
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
maap_utils/Job.py
+27
-11
27 additions, 11 deletions
maap_utils/Job.py
with
27 additions
and
11 deletions
maap_utils/Job.py
+
27
−
11
View file @
f253ebf8
import
logging
from
maap.maap
import
MAAP
from
typing
import
Optional
,
Dict
from
typing
import
Optional
,
Dict
,
Any
,
Callable
maap
=
MAAP
(
maap_host
=
"
api.maap-project.org
"
)
...
...
@@ -10,6 +11,8 @@ class Job:
def
__init__
(
self
,
kwargs
:
Dict
):
self
.
_kwargs
=
kwargs
self
.
_job_id
:
Optional
[
str
]
=
None
self
.
_status
:
Optional
[
str
]
=
None
self
.
_result_url
:
Optional
[
str
]
=
None
@property
def
kwargs
(
self
)
->
Dict
:
...
...
@@ -22,28 +25,41 @@ class Job:
return
self
.
_job_id
def
__repr__
(
self
)
->
str
:
return
f
"
Job(job_id=
{
self
.
_job_id
}
)
"
self
.
_status
:
Optional
[
str
]
=
None
self
.
_result_url
:
Optional
[
str
]
=
None
return
f
"
Job(job_id=
{
self
.
_job_id
}
, status=
{
self
.
_status
}
)
"
def
_safe_request
(
self
,
fn
:
Callable
[...,
Any
],
*
args
,
**
kwargs
)
->
Any
:
"""
Call a MAAP function, catch and log exceptions.
"""
try
:
return
fn
(
*
args
,
**
kwargs
)
except
Exception
as
e
:
logging
.
error
(
f
"
MAAP API error in
{
fn
.
__name__
}
:
{
e
}
"
)
return
None
def
submit
(
self
)
->
None
:
"""
Submit this job to MAAP
"""
job
=
maap
.
submitJob
(
**
self
.
kwargs
)
self
.
_job_id
=
job
.
id
job_obj
=
self
.
_safe_request
(
maap
.
submitJob
,
**
self
.
kwargs
)
if
job_obj
and
hasattr
(
job_obj
,
"
id
"
):
self
.
_job_id
=
job_obj
.
id
else
:
raise
RuntimeError
(
"
Failed to submit job—no job ID returned
"
)
def
get_status
(
self
)
->
str
:
def
get_status
(
self
)
->
Optional
[
str
]
:
"""
Get current job status from MAAP API
"""
self
.
_status
=
maap
.
getJobStatus
(
self
.
job_id
)
status
=
self
.
_safe_request
(
maap
.
getJobStatus
,
self
.
job_id
)
if
status
is
not
None
:
self
.
_status
=
status
return
self
.
_status
def
get_result
(
self
)
->
str
:
def
get_result
(
self
)
->
Optional
[
str
]
:
"""
Get job result URL from MAAP API
"""
self
.
_result_url
=
maap
.
getJobResult
(
self
.
job_id
)[
0
]
result
=
self
.
_safe_request
(
maap
.
getJobResult
,
self
.
job_id
)
if
result
:
self
.
_result_url
=
result
[
0
]
return
self
.
_result_url
def
cancel
(
self
)
->
None
:
"""
Cancel this job
"""
maap
.
cancelJob
(
self
.
job_id
)
self
.
_safe_request
(
maap
.
cancelJob
,
self
.
job_id
)
def
__hash__
(
self
)
->
int
:
"""
Hash function for the job object
"""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment