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

Add Custom Timeout - Closes #8 #14

Merged
merged 2 commits into from Apr 22, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion nbflow/example/SConstruct
Expand Up @@ -2,4 +2,4 @@ import os
from nbflow.scons import setup

env = Environment(ENV=os.environ)
setup(env, ["analyses"])
setup(env, ["analyses"], ARGUMENTS)
10 changes: 10 additions & 0 deletions nbflow/example/analyses/analyze_data.ipynb
Expand Up @@ -23,6 +23,16 @@
"import json"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"time.sleep(5) # Simulate long running task"
]
},
{
"cell_type": "code",
"execution_count": 3,
Expand Down
19 changes: 13 additions & 6 deletions nbflow/scons.py
@@ -1,14 +1,16 @@
import json
import sys
import subprocess as sp
from functools import partial

import nbconvert

def build_cmd(notebook):

def build_cmd(notebook, timeout):
cmd = [
"jupyter", "nbconvert",
"--log-level=ERROR",
"--ExecutePreprocessor.timeout=120",
"--ExecutePreprocessor.timeout=" + timeout,
"--execute",
"--inplace",
"--to", "notebook"
Expand All @@ -20,9 +22,9 @@ def build_cmd(notebook):

return cmd

def build_notebook(target, source, env):
def build_notebook(target, source, env, timeout="120"):
notebook = str(source[0])
code = sp.call(build_cmd(notebook))
code = sp.call(build_cmd(notebook, timeout))
if code != 0:
raise RuntimeError("Error executing notebook")

Expand All @@ -49,14 +51,19 @@ def print_cmd_line(s, targets, sources, env):
sys.stdout.write("%s --> %s\n"% (str(sources[0]), str(target)))


def setup(env, directories):
def setup(env, directories, args):
env['PRINT_CMD_LINE_FUNC'] = print_cmd_line
env.Decider('timestamp-newer')
DEPENDENCIES = json.loads(sp.check_output([sys.executable, "-m", "nbflow"] + directories).decode('UTF-8'))
timeout = args.get('timeout', None)
if timeout is not None:
build_notebook_timeout = partial(build_notebook, timeout=str(timeout))
else:
build_notebook_timeout = build_notebook
for script in DEPENDENCIES:
deps = DEPENDENCIES[script]
if len(deps['targets']) == 0:
targets = ['.phony_{}'.format(script)]
else:
targets = deps['targets']
env.Command(targets, [script] + deps['sources'], build_notebook)
env.Command(targets, [script] + deps['sources'], build_notebook_timeout)
2 changes: 1 addition & 1 deletion nbflow/tests/conftest.py
Expand Up @@ -28,6 +28,6 @@ def sconstruct(temp_cwd):
from nbflow.scons import setup

env = Environment(ENV=os.environ)
setup(env, ["."])
setup(env, ["."], ARGUMENTS)
"""
))
9 changes: 9 additions & 0 deletions nbflow/tests/test_nbflow.py
Expand Up @@ -11,6 +11,15 @@ def test_nbflow_no_args(temp_cwd):
run_command([sys.executable, "-m", "nbflow"], retcode=1)


def test_notebook_long_excecution(temp_cwd, sconstruct):
# copy example files
root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "example"))
shutil.copytree(os.path.join(root, "analyses"), "analyses")
shutil.copy(os.path.join(root, "SConstruct"), "SConstruct")
clear_notebooks("analyses")

run_command(["scons","timeout=1"], retcode=2)

def test_example(temp_cwd):
# copy example files
root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "example"))
Expand Down