11
22# ' Queues and Services HTTP Requests
33# '
4- # ' @name WebQueue
5- # '
64# ' @description
75# '
86# ' Connects the 'httpuv' and 'jobqueue' R packages.
8987# ' static paths. If not set or NULL, then it will use the result from
9088# ' calling `httpuv::staticPathOptions()` with no arguments.
9189# '
90+ # '
91+ # ' @return
92+ # ' A `webqueue` object with the following methods:
93+ # ' * `$url`
94+ # ' - Returns the URL where the server is available.
95+ # ' * `$stop(reason = 'server stopped')`
96+ # ' - Shuts down the webqueue and all associated subprocesses. Stopped Jobs
97+ # ' will have their `$output` set to a object of class `<interrupt/condition>`.
98+ # ' - `reason` - A brief message for the condition object.
99+ # ' - Returns this webqueue, invisibly.
92100# '
93101# ' @export
94- # ' @examples
102+ # ' @examplesIf ! jobqueue:::is_cran_check()
95103# '
96104# ' library(webqueue)
97105# '
98- # ' wq <- WebQueue$new (function (req) 'Hello World!\n')
106+ # ' wq <- webqueue (function (req) 'Hello World!\n')
99107# ' readLines(wq$url)
100108# ' wq$stop()
101109# '
102110
103- WebQueue <- R6Class(
104- classname = " WebQueue" ,
111+ webqueue <- function (
112+ handler ,
113+ host = ' 0.0.0.0' ,
114+ port = 8080L ,
115+ parse = NULL ,
116+ globals = list (),
117+ packages = NULL ,
118+ namespace = NULL ,
119+ init = NULL ,
120+ max_cpus = availableCores(),
121+ workers = ceiling(max_cpus * 1.2 ),
122+ timeout = NULL ,
123+ hooks = NULL ,
124+ reformat = NULL ,
125+ stop_id = NULL ,
126+ copy_id = NULL ,
127+ bg = TRUE ,
128+ quiet = FALSE ,
129+ onHeaders = NULL ,
130+ staticPaths = NULL ,
131+ staticPathOptions = NULL ) {
132+
133+ # Capture curly-brace expression
134+ init_subst <- substitute(init )
135+ if (isa(init_subst , ' {' )) init <- init_subst
136+
137+ # Forward arguments onto class constructor
138+ webqueue_class $ new(
139+ handler = handler ,
140+ host = host ,
141+ port = port ,
142+ parse = parse ,
143+ globals = globals ,
144+ packages = packages ,
145+ namespace = namespace ,
146+ init = init ,
147+ max_cpus = max_cpus ,
148+ workers = workers ,
149+ timeout = timeout ,
150+ hooks = hooks ,
151+ reformat = reformat ,
152+ stop_id = stop_id ,
153+ copy_id = copy_id ,
154+ bg = bg ,
155+ quiet = quiet ,
156+ onHeaders = onHeaders ,
157+ staticPaths = staticPaths ,
158+ staticPathOptions = staticPathOptions )
159+
160+ }
161+
162+
163+
164+ # ' @noRd
165+ # ' @keywords internal
166+
167+ webqueue_class <- R6Class(
168+ classname = " webqueue" ,
105169 cloneable = FALSE ,
106170
107171 public = list (
108172
109173 # ' @description
110- # ' Creates an `httpuv::WebServer` with requests handled by a `jobqueue::Queue `.
174+ # ' Creates an `httpuv::WebServer` with requests handled by a `jobqueue::jobqueue `.
111175 # '
112176 # ' @return A `WebQueue` object.
113177 initialize = function (
114178 handler ,
115- host = ' 0.0.0.0' ,
116- port = 8080L ,
117- parse = NULL ,
118- globals = list (),
119- packages = NULL ,
120- namespace = NULL ,
121- init = NULL ,
122- max_cpus = availableCores(),
123- workers = ceiling(max_cpus * 1.2 ),
124- timeout = NULL ,
125- hooks = NULL ,
126- reformat = NULL ,
127- stop_id = NULL ,
128- copy_id = NULL ,
129- bg = TRUE ,
179+ host = ' 0.0.0.0' ,
180+ port = 8080L ,
181+ parse = NULL ,
182+ globals = list (),
183+ packages = NULL ,
184+ namespace = NULL ,
185+ init = NULL ,
186+ max_cpus = availableCores(),
187+ workers = ceiling(max_cpus * 1.2 ),
188+ timeout = NULL ,
189+ hooks = NULL ,
190+ reformat = NULL ,
191+ stop_id = NULL ,
192+ copy_id = NULL ,
193+ bg = TRUE ,
130194 quiet = FALSE ,
131195 onHeaders = NULL ,
132196 staticPaths = NULL ,
@@ -163,38 +227,38 @@ WebQueue <- R6Class(
163227 # Launch WebQueue on a different R process
164228 if (isTRUE(bg )) {
165229
166- worker <- jobqueue :: Worker $ new()
167- sem <- create_semaphore ()
230+ worker <- jobqueue :: worker_class $ new()
231+ sem <- interprocess :: semaphore ()
168232 start_t <- Sys.time()
169233
170- job <- jobqueue :: Job $ new(
234+ job <- jobqueue :: job_class $ new(
171235 vars = environment(),
172236 expr = { # nocov start
173237
174238 # signals an error if unable to start
175- webqueue :: WebQueue $ new (
176- handler = handler ,
177- host = host ,
178- port = port ,
179- parse = parse ,
180- globals = globals ,
181- packages = packages ,
182- namespace = namespace ,
183- init = init ,
184- max_cpus = max_cpus ,
185- workers = workers ,
186- timeout = timeout ,
187- hooks = hooks ,
188- reformat = reformat ,
189- stop_id = stop_id ,
190- copy_id = copy_id ,
191- bg = FALSE ,
239+ webqueue :: webqueue (
240+ handler = handler ,
241+ host = host ,
242+ port = port ,
243+ parse = parse ,
244+ globals = globals ,
245+ packages = packages ,
246+ namespace = namespace ,
247+ init = init ,
248+ max_cpus = max_cpus ,
249+ workers = workers ,
250+ timeout = timeout ,
251+ hooks = hooks ,
252+ reformat = reformat ,
253+ stop_id = stop_id ,
254+ copy_id = copy_id ,
255+ bg = FALSE ,
192256 quiet = quiet ,
193257 onHeaders = onHeaders ,
194258 staticPaths = staticPaths ,
195259 staticPathOptions = staticPathOptions )
196260
197- semaphore :: increment_semaphore( sem )
261+ sem $ post( )
198262
199263 httpuv :: service(timeoutMs = Inf )
200264
@@ -206,7 +270,7 @@ WebQueue <- R6Class(
206270
207271 cnd <- catch_cnd({
208272
209- while (! decrement_semaphore( sem , wait = FALSE )) {
273+ while (! sem $ wait( timeout_ms = 0 )) {
210274
211275 if (job $ is_done ) {
212276 output <- job $ output
@@ -220,7 +284,7 @@ WebQueue <- R6Class(
220284 })
221285
222286 if (! is.null(cnd )) worker $ stop()
223- remove_semaphore( sem )
287+ sem $ remove( )
224288 if (! is.null(cnd )) cnd_signal(cnd )
225289
226290 }
@@ -233,8 +297,8 @@ WebQueue <- R6Class(
233297
234298 cnd <- catch_cnd({
235299
236- # Start a Queue .
237- private $ .jobqueue <- jobqueue :: Queue $ new (
300+ # Start a `jobqueue` .
301+ private $ .jobqueue <- jobqueue :: jobqueue (
238302 globals = globals ,
239303 packages = packages ,
240304 namespace = namespace ,
@@ -250,7 +314,7 @@ WebQueue <- R6Class(
250314
251315 later :: run_now()
252316 if (! identical(private $ .jobqueue $ state , ' idle' ))
253- stop(' Unable to start jobqueue::Queue ' ) # nocov
317+ stop(' Unable to start a ` jobqueue` ' ) # nocov
254318
255319
256320 # Start a Server.
0 commit comments