Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return jobdir for started async jobs #131

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions nb2workflow/nbadapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,23 @@ def as_dict(self):
class NotebookAdapter:
limit_output_attachment_file = None

def __init__(self, notebook_fn):
def __init__(self, notebook_fn, tempdir_cache=None):
self.notebook_fn = os.path.abspath(notebook_fn)
self.name = notebook_short_name(notebook_fn)
self.tempdir_cache = tempdir_cache
logger.debug("notebook adapter for %s", self.notebook_fn)
logger.debug(self.extract_parameters())

def new_tmpdir(self):
def new_tmpdir(self, cache_key=None):
logger.debug("tmpdir was "+getattr(self,'_tmpdir','unset'))
self._tmpdir = None
logger.debug("tmpdir became %s", self._tmpdir)

return self.tmpdir
newdir = self.tmpdir
if ( self.tempdir_cache is not None ) and ( cache_key is not None ):
self.tempdir_cache[cache_key] = newdir

return newdir

@property
def tmpdir(self):
Expand Down Expand Up @@ -407,12 +412,12 @@ def update_summary(self, **d):



def execute(self, parameters, progress_bar = True, log_output = True, inplace=False):
def execute(self, parameters, progress_bar = True, log_output = True, inplace=False, tmpdir_key=None):
t0 = time.time()
logstasher.log(dict(origin="nb2workflow.execute", event="starting", parameters=parameters, workflow_name=notebook_short_name(self.notebook_fn), health=current_health()))

logger.info("starting job")
exceptions = self._execute(parameters, progress_bar, log_output, inplace)
exceptions = self._execute(parameters, progress_bar, log_output, inplace, tmpdir_key)

tspent = time.time() - t0
logstasher.log(dict(origin="nb2workflow.execute",
Expand All @@ -425,10 +430,10 @@ def execute(self, parameters, progress_bar = True, log_output = True, inplace=Fa

return exceptions

def _execute(self, parameters, progress_bar = True, log_output = True, inplace=False):
def _execute(self, parameters, progress_bar = True, log_output = True, inplace=False, tmpdir_key=None):

if not inplace :
tmpdir = self.new_tmpdir()
tmpdir = self.new_tmpdir(tmpdir_key)
logger.info("new tmpdir: %s", tmpdir)

try:
Expand Down
37 changes: 27 additions & 10 deletions nb2workflow/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def create_app():
app = create_app()

app.async_workflows = dict()
app.async_workflow_jobdirs = dict()
app.started_at = datetime.datetime.now()


Expand Down Expand Up @@ -172,10 +173,11 @@ def _run(self):

template_nba = app.notebook_adapters.get(self.target)

nba = NotebookAdapter(template_nba.notebook_fn)
nba = NotebookAdapter(template_nba.notebook_fn, tempdir_cache=app.async_workflow_jobdirs)

try:
exceptions = nba.execute(self.params['request_parameters'])
exceptions = nba.execute(self.params['request_parameters'], tmpdir_key=self.key)

except PapermillWorkflowIncomplete as e:
logger.info("found incomplete workflow: %s, rescheduling", repr(e))

Expand Down Expand Up @@ -286,19 +288,34 @@ def workflow(target, background=False, async_request=False):
print('cache key/value', key, value)

if value is None:
async_task = AsyncWorkflow(
key=key, target=target, params=interpreted_parameters, callback=async_request_callback)

async_queue.put(async_task)
async_task = AsyncWorkflow(key=key,
target=target,
params=interpreted_parameters,
callback=async_request_callback)

app.async_workflows[key] = 'submitted'
return make_response(jsonify(workflow_status="submitted", comment="task created"), 201)
async_queue.put(async_task)

elif value in ['started', 'submitted']:
return make_response(jsonify(workflow_status=value, comment="task is "+value), 201)
return make_response(jsonify(workflow_status="submitted",
comment="task created"),
201)

elif value == 'submitted':
return make_response(jsonify(workflow_status=value,
comment="task is "+value),
201)

elif value == 'started':
return make_response(jsonify(workflow_status=value,
comment="task is "+value,
jobdir=app.async_workflow_jobdirs.get(key)),
201)

else:
return make_response(jsonify(workflow_status="done", data=value, comment=""), 200)
return make_response(jsonify(workflow_status="done",
data=value,
comment=""),
200)

if len(issues) > 0:
return make_response(jsonify(issues=issues), 400)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def test_worker_run():

logger.info('service returns %s %s', r, r.json)

if r.json['workflow_status'] == 'started':
assert 'jobdir' in r.json
logger.info('jobdir is reported as %s', r.json['jobdir'])

if r.json['workflow_status'] == 'done':
logger.info('workflow done!')
break
Expand Down
Loading