2

Is it possible to manually add a task to the redis queue, so that it can be executed by a redis worker?

As a simple example, I'm launching a worker using:

require('doRedis')
redisWorker('jobs')

In another R session, I'm creating a queue and would like to send a simple expression (for example: print("hello world")) to the queue so that it is executed by the worker.

I know how to do it using foreach:

require('doRedis')
registerDoRedis('jobs')
foreach(j=1,.combine=sum,.multicombine=TRUE) %dopar% {
  print("hello world")
  1
}    

I would like to be able to add a task to the queue without using foreach. The reason is, I do not want to have my R session wait for the output (the script would write its results to the disk).

Here is what I have tried so far, based on code in the .doRedis() function:

data <- list(queue = "jobs")
queue <- data$queue
queueCounter <- sprintf("%s:counter", queue)   # job task ID counter
ID <- redisIncr(queueCounter)
queueEnv <- sprintf("%s:%.0f.env",queue,ID) # R job environment
queueTasks <- sprintf("%s:%.0f",queue,ID) # Job tasks hash
queueResults <- sprintf("%s:%.0f.results",queue,ID) # Output values
queueStart <- sprintf("%s:%.0f.start*",queue,ID)
queueAlive <- sprintf("%s:%.0f.alive*",queue,ID)

# add the environment to the queue
redisSet(key = queueEnv,
         value = list(expr=expression(), 
                      exportenv=baseenv(), 
                      packages=NULL)

# put tasks in queue
taskblock <- list(ex1 <- expression('print("hello world")'))
j <- 1
taskLabel <- I
task_id = as.character(taskLabel(j))
task <- list(task_id=task_id, args=taskblock)
redisHSet(key = queueTasks, 
          field = task_id, 
          value = task)
redisRPush(key = queue, value = ID)

It doesn't work, and I think (at least) the definition of the environment is wrong...

Any help is very welcome !

Tomas
  • 57,621
  • 49
  • 238
  • 373
nassimhddd
  • 8,340
  • 1
  • 29
  • 44

0 Answers0