Skip to content

Commit

Permalink
Move error handler inside forked process
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed Mar 4, 2018
1 parent 2efdd2c commit 6b3355d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 29 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Expand Up @@ -6,7 +6,6 @@ Authors@R: person("Jeroen", "Ooms", role = c("aut", "cre"), email = "jeroen@berk
License: Apache License 2.0
Encoding: UTF-8
URL: https://www.opencpu.org (website)
https://opencpu.github.io (documentation)
https://github.com/opencpu/opencpu#readme (devel)
BugReports: https://github.com/opencpu/opencpu/issues
Depends:
Expand Down Expand Up @@ -43,5 +42,5 @@ Description: A system for embedded scientific computing and reproducible researc
The entire system is fully open source and permissively licensed. The OpenCPU website
has detailed documentation and example apps.
LazyData: yes
RoxygenNote: 6.0.1
RoxygenNote: 6.0.1.9000
Roxygen: list(markdown = TRUE)
4 changes: 4 additions & 0 deletions NEWS
@@ -1,3 +1,7 @@
2.0.7
- Internally switch to condition system to raise ocpu-success


2.0.6
- Support auth_token in webhook (either set /etc/opencpu/secret.conf or GITHUB_PAT in /etc/opencpu/Renviron)
- Pass down 'rapache' option to webhook installer process to load proper config files
Expand Down
16 changes: 7 additions & 9 deletions R/request.R
@@ -1,9 +1,9 @@
## do req is the main function
## it should never actually return
## functions should always call respond()
request <- function(expr){
request <- function(REQDATA){
tryCatch({
eval(expr);
main(REQDATA);
respond(503L, write_to_file("function returned without calling respond"));
}, ocpu_response = success_handler, error = error_handler);
}
Expand All @@ -26,13 +26,13 @@ respond <- function(status = 503L, body=NULL, headers=list()){
message = "ocpu success",
call = NULL
),
class=c("ocpu_response", "error", "condition"),
class=c("ocpu_response", "condition"),
status = status,
body = body,
headers = headers
);

base::stop(e)
base::signalCondition(e)
}

success_handler <- function(e){
Expand Down Expand Up @@ -70,13 +70,11 @@ respond_data <- function(response){
response$headers[["X-ocpu-version"]] = as.character(utils::packageVersion(packagename));

#reset req/res state
res$reset();
req$reset();
res$reset()
req$reset()

# close open files? Disabled: this is very slow.
# gc()

#return
return(response);
return(response)
}

34 changes: 16 additions & 18 deletions R/serve.R
Expand Up @@ -5,7 +5,7 @@ serve <- function(REQDATA, run_worker = NULL){
if(REQDATA$METHOD %in% c("HEAD", "GET", "OPTIONS")){
pwd <- getwd()
on.exit(setwd(pwd), add = TRUE)
return(request(main(REQDATA)));
return(request(REQDATA))
} else {
hash <- generate_hash()
tmp <- file.path(ocpu_temp(), hash)
Expand All @@ -19,7 +19,7 @@ serve <- function(REQDATA, run_worker = NULL){
on.exit(unlink(mytmp, recursive = TRUE), add = TRUE)
expr <- c(
call("Sys.setenv", OCPU_SESSION_DIR = mytmp),
parse(text = "opencpu:::request(opencpu:::main(REQDATA))")
parse(text = "opencpu:::request(REQDATA)")
)
return(tryCatch({
run_worker(eval, expr = expr, envir = list(REQDATA = REQDATA), timeout = config("timelimit.post"))
Expand Down Expand Up @@ -52,20 +52,18 @@ serve <- function(REQDATA, run_worker = NULL){
}

# RApache (cloud server) runs request in a fork, saves workding dir and wipes tmpdir afterwards
request({
hash <- generate_hash()
tmp <- file.path(ocpu_temp(), hash)
if(!dir.create(tmp))
stop(sprintf("Failed to create tempdir %s. Check directory permissions.", tmp))
mytmp <- normalizePath(tmp)
if(REQDATA$METHOD == "POST"){
on.exit({
if(file.exists(file.path(mytmp, "workspace")))
file_move(file.path(mytmp, "workspace"), sessiondir(hash))
}, add = TRUE)
}
on.exit(unlink(mytmp, recursive = TRUE), add = TRUE)
sys::eval_safe(main(REQDATA), tmp = mytmp, timeout = as.numeric(timeout), profile = profile,
rlimits = limits, device = ocpu_grdev)
})
hash <- generate_hash()
tmp <- file.path(ocpu_temp(), hash)
if(!dir.create(tmp))
stop(sprintf("Failed to create tempdir %s. Check directory permissions.", tmp))
mytmp <- normalizePath(tmp)
if(REQDATA$METHOD == "POST"){
on.exit({
if(file.exists(file.path(mytmp, "workspace")))
file_move(file.path(mytmp, "workspace"), sessiondir(hash))
}, add = TRUE)
}
on.exit(unlink(mytmp, recursive = TRUE), add = TRUE)
sys::eval_safe(request(REQDATA), tmp = mytmp, timeout = as.numeric(timeout),
profile = profile, rlimits = limits, device = ocpu_grdev)
}
1 change: 1 addition & 0 deletions man/apps.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/server.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b3355d

Please sign in to comment.