Skip to content

Commit 2ebddbb

Browse files
committed
use launcher script
Original version use python module to call R code. This would lock all python threads resulting in missing heartbeats.
1 parent 2735f79 commit 2ebddbb

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

sample-extractors/simple-r-extractor/Dockerfile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
FROM clowder/pyclowder
1+
FROM clowder/pyclowder:latest
22

33
ENV R_SCRIPT="" \
44
R_FUNCTION=""
55

6-
RUN apt-get -q -q update \
7-
&& apt-get -y install --no-install-recommends r-base-core r-base-dev python-dev \
6+
RUN echo "deb http://cran.rstudio.com/bin/linux/ubuntu xenial/" > /etc/apt/sources.list.d/R.list \
7+
&& apt-key adv --keyserver keyserver.ubuntu.com --recv E084DAB9 \
8+
&& apt-get -q -q update \
9+
&& apt-get -y install --no-install-recommends r-base-core r-base-dev \
810
&& rm -rf /var/lib/apt/lists/* \
9-
&& pip install rpy2==2.8.6
10-
RUN Rscript --vanilla -e "install.packages('jsonlite', repos='http://cran.rstudio.com/'); print(system.file(package = 'jsonlite')); q(status=as.integer(system.file(package = 'jsonlite') == ''))"
11+
&& Rscript --vanilla -e "install.packages('jsonlite', repos='http://cran.rstudio.com/'); print(system.file(package = 'jsonlite')); q(status=as.integer(system.file(package = 'jsonlite') == ''))"
1112

12-
COPY r_extractor.py /home/clowder/
13+
COPY r_extractor.py launcher.R /home/clowder/
1314

1415
# install any packages
1516
ONBUILD COPY packages.* Dockerfile /home/clowder/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env Rscript
2+
3+
# get environment variables
4+
r_script <- Sys.getenv('R_SCRIPT')
5+
r_function <- Sys.getenv('R_FUNCTION')
6+
if (r_function == '') {
7+
stop("Need a function to call.")
8+
}
9+
10+
# command line arguments
11+
args <- commandArgs(trailingOnly=TRUE)
12+
if (length(args) != 2) {
13+
stop("Need 2 arguments (input_file, json_file)")
14+
}
15+
input_file <- args[1]
16+
json_file <- args[2]
17+
18+
# source script file
19+
if (r_script != '') {
20+
source(r_script)
21+
}
22+
23+
# call function
24+
result <- do.call(r_function, list(input_file))
25+
26+
# write result as json
27+
write(jsonlite::toJSON(result, auto_unbox=TRUE), json_file)
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
#!/usr/bin/env python
22

33
import json
4-
import os
4+
import subprocess
5+
import tempfile
56

67
from pyclowder.extractors import SimpleExtractor
7-
import rpy2.robjects as robjects
8-
9-
r_script = os.getenv("R_SCRIPT")
10-
r_function = os.getenv("R_FUNCTION")
118

129

1310
class RExtractor(SimpleExtractor):
1411
def process_file(self, input_file):
15-
r_result = robjects.r('''
16-
if ("%s" != "") {
17-
source("%s")
18-
}
19-
result <- do.call("%s", list("%s"))
20-
jsonlite::toJSON(result, auto_unbox=TRUE)
21-
''' % (r_script, r_script, r_function, input_file))
22-
return json.loads(str(r_result))
12+
with tempfile.NamedTemporaryFile(suffix=".json") as json_file:
13+
subprocess.check_call(['/home/clowder/launcher.R', input_file, json_file.name])
14+
return json.load(json_file.file)
2315

2416

2517
RExtractor().start()

0 commit comments

Comments
 (0)