-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
113 lines (90 loc) · 3.38 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
library(shiny)
library(plumber)
library(ggplot2)
if (!file.exists("logs/previous_inputs.log")) {
file.create("logs/previous_inputs.log")
}
fileReaderData <- reactiveFileReader(500, session = NULL, 'logs/previous_inputs.log', readLines)
fileReaderTimes <- reactiveFileReader(500, session = NULL, 'logs/stdoutFile.txt', readLines)
current_pid <- reactiveFileReader(1000, session = NULL, 'save_pid.txt', readLines)
pred_function <- "predict"
pred_file <- data.frame(value = "mean.R", stringsAsFactors = FALSE)
currently_deployed <- data.frame(value = pred_file$value, stringsAsFactors = FALSE)
system("pkill -f deploy.R")
system(paste0("nohup Rscript deploy.R ", pred_file$value, " & echo $! > save_pid.txt"))
shinyServer(function(input, output) {
##kill model function
## kills based on PID
observeEvent(input$kill,
{
system("kill -9 `cat save_pid.txt`")
})
## reload model function
## redeploy - display new PID
observeEvent(input$redeploy,
{
system("kill -9 `cat save_pid.txt`")
system(paste0("nohup Rscript deploy.R ", pred_file$value, " & echo $! > save_pid.txt"))
currently_deployed <<- reactiveValues(value = pred_file$value)
})
observeEvent(input$test,
{
if (file.exists("logs/stdoutFile.txt")) {
file.remove("logs/stdoutFile.txt")
}
systemCall <- paste0 ("function timeout() { perl -e 'alarm shift; exec @ARGV' \"$@\"; }; ",
"timeout 15 bash testReactiveFileReader.sh curl http://localhost:8001/",
pred_function, " &")
system(systemCall)
#system("nohup gtimeout 15s bash testReactiveFileReader.sh &")
})
observeEvent(input$prediction_file,
{
pred_file <<- reactiveValues(value = file.choose())
})
checkServerStatus <- reactiveTimer(2000)
output$serverStatusText <- renderText({
checkServerStatus()
serverStatusCall <- paste0("curl -s \"http://localhost:8001/",
pred_function,"\" >/dev/null; echo $? &")
serverStatus <- system(serverStatusCall, intern = TRUE)
if (serverStatus == 0) {
result <- c("Deployed")
} else {
result <- c("Not Deployed")
}
result
})
# output$pid_value <- renderText({
# paste0("PID = ",current_pid())
# })
output$fileReaderText <- renderText({
# Read the text, and make it a consistent number of lines so
# that the output box doesn't grow in height.
text <- fileReaderData()
text <- tail(text)
text[is.na(text)] <- ""
paste(text, collapse = '\n')
})
output$timeSummary <- renderPrint({
latencies <- as.numeric(fileReaderTimes())
summary(latencies)
})
output$timePlot <- renderPlot({
latencies <- as.numeric(fileReaderTimes())
ggplot2::qplot(latencies)
})
output$predLocation <- renderText({
checkServerStatus()
pred_file$value
})
output$currentPredLocation <- renderText({
checkServerStatus()
currently_deployed$value
})
})