-
Notifications
You must be signed in to change notification settings - Fork 1
/
cron.R
237 lines (199 loc) · 7.79 KB
/
cron.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#https://www.lendingclub.com/developers/listed-loans.action
# System configuration
library(methods)
library(shiny)
library(shinyBS)
library(gtools)
cron <- list()
if (file.exists('config/cron.rda')) {
load('config/cron.rda')
}
launch.browser = function(appUrl, browser.path='/usr/bin/chromium-browser') {
system(sprintf('"%s" --disable-gpu --app="data:text/html,<html>
<head>
<title>Schedule Service</title>
</head>
<body>
<script>window.resizeTo(%s,%s);window.location=\'%s\';</script>
</body></html>" &', browser.path, 900, 560, appUrl))
}
buttonEnableValue <- 0
buttonResetValue <- 0
buttonDisableValue <- 0
hoursChoices <- c(5,9,13,17)
minutesChoices <- c(59)
hoursNoListChoices <- ''
minutesNoListChoices <- ''
shinyApp(
ui = fluidPage(
br(),
h4('Schedule Service'),
fluidRow(
column(6,
wellPanel(
h4('With List Detection'),
HTML('PLS is configured to use the Pacific Time Zone to match the Lending Club platform. Lending Club lists new loans
at 06:00, 10:00, 14:00, 18:00 PST. The default configuration below starts PLS with list detection one minute prior to allow the system
enough time to properly load. <br><br>'),
selectInput('hours', 'Start Hours',
choices=seq(0,23),
multiple=TRUE,
selected=if(length(cron$hours)>0) cron$hours else hoursChoices,
selectize = TRUE),
bsTooltip(id = "hours", title = "Select hours to start service in PST time",
placement = "top", trigger = "hover")
,
selectInput('minutes', 'Start Minutes',
choices=0:59,
multiple=TRUE,
selected=if(length(cron$minutes)>0) cron$minutes else minutesChoices,
selectize = TRUE),
bsTooltip(id = "minutes", title = "Select minutes to start service in PST time",
placement = "top", trigger = "hover")
)
),
column(6,
wellPanel(
h4('Without List Detection'),
HTML('You can enable PLS to start without list detection if desired (expert use only). Note that only one service can run at one time. Please schedule with different start times
than the "With" panel on the left and with enough time between start times to avoid overlap.<br><br>'),
selectInput('hoursNoList', 'Start Hours',
choices=seq(0,23),
multiple=TRUE,
selected=if(length(cron$hoursNoList)>0) cron$hoursNoList else hoursNoListChoices,
selectize = TRUE),
bsTooltip(id = "hoursNoList", title = "Select hours to start service in PST time",
placement = "top", trigger = "hover")
,
selectInput('minutesNoList', 'Start Minutes',
choices=0:59,
multiple=TRUE,
selected=if(length(cron$minutesNoList)>0) cron$minutesNoList else minutesNoListChoices,
selectize = TRUE),
bsTooltip(id = "minutesNoList", title = "Select minutes to start service in PST time",
placement = "top", trigger = "hover")
)
)
),
fluidRow(
column(12,
actionButton('enable', 'Enable Service'),
actionButton('disable', 'Disable Service'),
actionButton('reset', 'Reset'),
tags$button( "Close", id="close", type="button", class="btn action-button", onclick="self.close()")
)
),
fluidRow(
column(12,
br(),
bsAlert(inputId="alert")
)
)
),
server = function(input, output, session) {
session$onSessionEnded(function() {
stopApp()
})
# Enable button
observe({
if(input$enable > buttonEnableValue) {
buttonEnableValue <<- input$enable
if (invalid(input$hours)) {
createAlert(session, inputId = "alert", alertId="a1",
message="Please select start hours with list detection (left panel)",
type = "danger",
append = FALSE)
return()
}
if (invalid(input$minutes)) {
createAlert(session, inputId = "alert", alertId="a1",
message="Please enter start minutes with list detection (left panel)",
type = "danger",
append = FALSE)
return()
}
if ( invalid(input$hoursNoList) & !invalid(input$minutesNoList) ) {
createAlert(session, inputId = "alert", alertId="a1",
message="Please enter start hours without list detection (right panel)",
type = "danger",
append = FALSE)
return()
}
if ( !invalid(input$hoursNoList) & invalid(input$minutesNoList) ) {
createAlert(session, inputId = "alert", alertId="a1",
message="Please enter start minutes without list detection (right panel)",
type = "danger",
append = FALSE)
return()
}
closeAlert(session, alertId="a1")
cron <<- reactiveValuesToList(input)
save(cron,file='config/cron.rda')
hours=paste(cron$hours, collapse=',')
minutes=paste(cron$minutes, collapse=',')
cronStr=paste(minutes,hours,'* * * /home/user/pls/start/startOrder.sh')
cronStrNoList=''
if ( !invalid(input$hoursNoList) & !invalid(input$minutesNoList) ) {
hoursNoList=paste(cron$hoursNoList, collapse=',')
minutesNoList=paste(cron$minutesNoList, collapse=',')
cronStrNoList=paste(minutesNoList,hoursNoList,'* * * /home/user/pls/start/startManual.sh nolist')
cronStrCmb=paste(cronStr,cronStrNoList,sep="\n")
} else {
cronStrCmb=cronStr
}
print(cronStr)
print(cronStrNoList)
print(cronStrCmb)
cmd=paste('echo "',cronStrCmb,'" | crontab -',sep='')
system(cmd)
stat=system('crontab -l | grep startOrder')
if(stat!=0) {
createAlert(session, inputId = "alert", alertId="a1",
message="Error adding service",
type = "danger",
append = FALSE)
return()
}
createAlert(session, inputId = "alert", alertId="a1",
message="Service enabled",
type = "success",
append = FALSE)
}
})
# Disable button
observe({
if(input$disable > buttonDisableValue) {
buttonDisableValue <<- input$disable
closeAlert(session, alertId="a1")
system('echo | crontab -')
stat=system('crontab -l | grep startOrder')
if(stat==0) {
createAlert(session, inputId = "alert", alertId="a1",
message="Error clearing service",
type = "danger",
append = FALSE)
return()
}
createAlert(session, inputId = "alert", alertId="a1",
message="Service disabled",
type = "success",
append = FALSE)
}
})
# Reset button
observe({
if(input$reset > buttonResetValue) {
buttonResetValue <<- input$reset
updateSelectizeInput(session, 'hours', choices=seq(0,23), selected=hoursChoices)
updateSelectizeInput(session, 'minutes', choices=seq(0,59), selected=minutesChoices)
updateSelectizeInput(session, 'hoursNoList', choices=seq(0,23), selected=hoursNoListChoices)
updateSelectizeInput(session, 'minutesNoList', choices=seq(0,59), selected=minutesNoListChoices)
createAlert(session, inputId = "alert", alertId="a1",
message="Start times reset. Remember to click 'Enable Service' to apply",
type = "success",
append = FALSE)
}
})
},
options = list(launch.browser=launch.browser)
)