Skip to content

Commit

Permalink
DSJobs get job status
Browse files Browse the repository at this point in the history
  • Loading branch information
kks32 committed Oct 17, 2023
0 parents commit 13add9e
Show file tree
Hide file tree
Showing 11 changed files with 863 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Python Tests with Poetry

on: [push, pull_request]
jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
# see details (matrix, python-version, python-version-file, etc.)
# https://github.com/actions/setup-python
- name: Install poetry
uses: abatilo/actions-poetry@v2
- name: Setup a local virtual environment (if no poetry.toml file)
run: |
poetry config virtualenvs.create true --local
poetry config virtualenvs.in-project true --local
- uses: actions/cache@v3
name: Define a cache for the virtual environment based on the dependencies lock file
with:
path: ./.venv
key: venv-${{ hashFiles('poetry.lock') }}
- name: Install the project dependencies
run: poetry install
- name: Run the automated tests (for example)
run: poetry run pytest -v
177 changes: 177 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# OS temp files
**/.DS_Store*
*~*
*#*

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/


# IntelliJ / PyCharm artifacts
.idea/

# VS Code artifacts
.vscode/

# Logs and databases
*.log
*.sqlite

4 changes: 4 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Authors

* Prof. Pedro Arduino, University of Washington
* Krishna Kumar, University of Texas at Austin
20 changes: 20 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# MIT License
> Copyright (c) [2023] [Authors]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# DesignSafe Jobs

[![PyTest](https://github.com/geoelements/dsapps/actions/workflows/pytest.yml/badge.svg)](https://github.com/geoelements/dsapps/actions/workflows/pytest.yml)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/geoelements/dsjobs/main/LICENSE.md)

`dsjobs` is a library that simplifies the process of submitting, running, and monitoring [TAPIS v2 / AgavePy](https://agavepy.readthedocs.io/en/latest/index.html) jobs on [DesignSafe](https://designsafe-ci.org) via [Jupyter Notebooks](https://jupyter.designsafe-ci.org).

## Installation

Install `dsjobs` via pip (**coming soon**)

```shell
pip3 install dsjobs
```

## Features

* Simplified TAPIS v2 Calls: No need to fiddle with complex API requests. `dsjobs` abstracts away the complexities.

* Seamless Integration with DesignSafe Jupyter Notebooks: Launch DesignSafe applications directly from the Jupyter environment.

## Support

For any questions, issues, or feedback submit an [issue](https://github.com/geoelements/dsjobs/issues/new)

## License

`dsjobs` is licensed under the [MIT License](LICENSE.md).

## Authors

* Prof. Pedro Arduino, University of Washington
* Krishna Kumar, University of Texas at Austin
1 change: 1 addition & 0 deletions dsjobs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .jobs import get_status
65 changes: 65 additions & 0 deletions dsjobs/jobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import time
from datetime import datetime, timedelta, timezone
from tqdm import tqdm

def get_status(ag, job_id, time_lapse=15):
"""
Retrieves and monitors the status of a job from Agave.
This function initially waits for the job to start, displaying its progress using
a tqdm progress bar. Once the job starts, it monitors the job's status up to
a maximum duration specified by the job's "maxHours". If the job completes or fails
before reaching this maximum duration, it returns the job's final status.
Args:
ag (object): The Agave job object used to interact with the job.
job_id (str): The unique identifier of the job to monitor.
time_lapse (int, optional): Time interval, in seconds, to wait between status
checks. Defaults to 15 seconds.
Returns:
str: The final status of the job. Typical values include "FINISHED", "FAILED",
and "STOPPED".
Raises:
No exceptions are explicitly raised, but potential exceptions raised by the Agave
job object or other called functions/methods will propagate.
"""

previous_status = None
# Initially check if the job is already running
status = ag.jobs.getStatus(jobId=job_id)["status"]

job_details = ag.jobs.get(jobId=job_id)
max_hours = job_details["maxHours"]

# Using tqdm to provide visual feedback while waiting for job to start
with tqdm(desc="Waiting for job to start", dynamic_ncols=True) as pbar:
while status not in ["RUNNING", "FINISHED", "FAILED", "STOPPED"]:
time.sleep(time_lapse)
status = ag.jobs.getStatus(jobId=job_id)["status"]
pbar.update(1)
pbar.set_postfix_str(f"Status: {status}")

# Once the job is running, monitor it for up to maxHours
max_iterations = int(max_hours * 3600 // time_lapse)

# Using tqdm for progress bar
for _ in tqdm(range(max_iterations), desc="Monitoring job", ncols=100):
status = ag.jobs.getStatus(jobId=job_id)["status"]

# Print status if it has changed
if status != previous_status:
tqdm.write(f"\tStatus: {status}")
previous_status = status

# Break the loop if job reaches one of these statuses
if status in ["FINISHED", "FAILED", "STOPPED"]:
break

time.sleep(time_lapse)
else:
# This block will execute if the for loop completes without a 'break'
print("Warning: Maximum monitoring time reached!")

return status
Loading

0 comments on commit 13add9e

Please sign in to comment.