Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r_bg() error in Docker container #293

Closed
dejan94it opened this issue Nov 17, 2024 · 4 comments
Closed

r_bg() error in Docker container #293

dejan94it opened this issue Nov 17, 2024 · 4 comments
Labels
reprex needs a minimal reproducible example

Comments

@dejan94it
Copy link

Hi!
I have a script which works perfectly fine on my pc (windows) but stuck inside a docker container with rocker/r-ver:4.3.3 image.
In particular, following ChatGPT suggestions, for the container I tried with arch, env and cmdargs arguments SEPARATELY but The error is always the same

cat("R binary path:", Sys.which("R"), "\n")
cat("R binary directory:", dirname(Sys.which("R")), "\n")

 callr::r_bg(
  func = start_telegram_bot,
  args = list(path = ".",
              bot_token = Sys.getenv("BOT_TOKEN"),
              admin_chat_id = Sys.getenv("ADMIN_CHAT_ID")),
  # arch = Sys.which("R"), THIS
  # env = c(PATH = dirname(Sys.which("R"))),  OR THIS
  # cmdargs = c(
  # Sys.which("R"),  #  OR THIS
  #  "--slave", "--no-save", "--no-restore"
  # ),
  stdout = "logs/output.log",
  stderr = "logs/error.log",
  supervise = TRUE # per terminare il processo se la sessione R principale viene chiusa
); bot_bg_process$is_alive()
R binary path: /usr/local/bin/R
R binary directory: /usr/local/bin

Error in `process_initialize(self, private, command, args, stdin, stdout, …` at process.R:212:18:
! Native call to `processx_exec` failed
Caused by error in `chain_call(c_processx_exec, command, c(command, args), pty, pty_options, …` at initialize.R:138:3:
! cannot start processx process '/usr/local/bin/R' (system error 2, No such file or directory) @unix/processx.c:611 (processx_exec)

Any other suggestions?
thanks!

@gaborcsardi
Copy link
Member

You'll have to show how exactly you are running Docker, the full code, and also the full output.

@dejan94it
Copy link
Author

FROM rocker/r-ver:4.3.3

RUN apt-get update -qq && \ 
  apt-get install -y --no-install-recommends \
    libcurl4-openssl-dev \
    libfontconfig1-dev \
    libfreetype6-dev \
    libfribidi-dev \
    libharfbuzz-dev \
    libicu-dev \
    libjpeg-dev \
    libpng-dev \
    libsasl2-dev \
    libssl-dev \
    libtiff-dev \
    libxml2-dev \
    libxt6 \
    make \
    pandoc \
    zlib1g-dev && \
  apt-get clean && \ 
  rm -rf /var/lib/apt/lists/*

COPY renv.lock renv.lock

RUN Rscript -e "install.packages('renv')"

RUN Rscript -e "renv::restore()"

COPY . . 

CMD ["Rscript", "bot_main.R"]

docker run --env-file .env my_image

bot_main.R

library(DBI)
library(RSQLite)
library(telegram.bot)
cat("R binary path:", Sys.which("R"), "\n")
cat("R binary directory:", dirname(Sys.which("R")), "\n")

db_con <- dbConnect(SQLite(), dbname = Sys.getenv("DB_PATH"))

start_telegram_bot <- function(path = ".",
                               bot_token,
                               admin_chat_id = NULL){
  library(telegram.bot)
  library(tidyverse)
  library(DBI)
  library(RSQLite)
  library(glue)
  
  db_con <- dbConnect(SQLite(), dbname = Sys.getenv("DB_PATH"))
  bot <- Bot(token = bot_token)
  updater <- Updater(token = bot_token)

  
  source("./messageHandlers.R", local = T)
  source("./commandHandlers.R", local = T)
  source("./callbackQueryHandlers.R", local = T)
  source("./filters.R", local = T)
  

  bot$send_message(chat_id = Sys.getenv("ADMIN_CHAT_ID"), text = "Bot Avviato")
  updater$start_polling(clean = T)
}


bot_bg_process <- callr::r_bg(
  func = start_telegram_bot,
  args = list(path = ".",
              bot_token = Sys.getenv("BOT_TOKEN"),
              admin_chat_id = Sys.getenv("ADMIN_CHAT_ID")),
  # arch = Sys.which("R"),
  # env = c(PATH = dirname(Sys.which("R"))), # Set the directory of the R binary
  # cmdargs = c(
  #  Sys.which("R"),  
  # "--slave", "--no-save", "--no-restore"
  # ),
  stdout = "logs/output.log",
  stderr = "logs/error.log",
  supervise = TRUE 
); bot_bg_process$is_alive()

I cannot provide the entire code and folders, but you should be able to reproduce the error removing functions which require env variables.

@gaborcsardi gaborcsardi added the reprex needs a minimal reproducible example label Nov 17, 2024
@dejan94it
Copy link
Author

dejan94it commented Nov 17, 2024

update:
this is the full error backtrace:

Error in `process_initialize(self, private, command, args, stdin, stdout, …` at process.R:212:18:
! Native call to `processx_exec` failed
Caused by error in `chain_call(c_processx_exec, command, c(command, args), pty, pty_options, …` at initialize.R:138:3:
! cannot start processx process '/usr/local/bin/R' (system error 2, No such file or directory) @unix/processx.c:611 (processx_exec)
---
Backtrace:
 1. callr::r_bg(func = start_telegram_bot, args = list(path = ".", …
 2. r_process$new(options = options) at eval-bg.R:42:3
 3. local initialize(...) at new.R:156:5
 4. callr:::rp_init(self, private, super, options) at r-process.R:29:18
 5. callr:::with_envvar(options$env, do.call(super$initialize, c(list(options$bin, … at r-process.R:69:3
 6. base::force(code) at utils.R:66:3
 7. base::do.call(super$initialize, c(list(options$bin, options$real_cmdargs, … at r-process.R:69:3
 8. (function (command = NULL, args = character(), stdin = NULL, …
 9. processx:::process_initialize(self, private, command, args, stdin, stdout, … at process.R:212:18
10. processx:::chain_call(c_processx_exec, command, c(command, args), pty, pty_options, … at initialize.R:138:3
11. | base::withCallingHandlers(do.call(".Call", list(.NAME, ...)), error = function(e) { … at standalone-errors.R:379:5
12. | base::do.call(".Call", list(.NAME, ...)) at standalone-errors.R:379:5
13. | base::.handleSimpleError(function (e) …
14. | local h(simpleError(msg, call))
15. | processx:::throw_error(err, parent = e) at standalone-errors.R:390:9

@dejan94it
Copy link
Author

after a thousand tries, even directly with processx, the problem were

  stdout = "logs/output.log",
  stderr = "logs/error.log",

even if thy works fine outside the container.
Anyway I commented those lines and I don't give a hell anymore about logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
reprex needs a minimal reproducible example
Projects
None yet
Development

No branches or pull requests

2 participants