Search code examples
bashshellenvironment-variablesrstudiorstudio-server

Update a global variable in active session from a background process Rstudio Server


This may be theoretical more than anything else; a solution would be remarkable, but any feedback/ideas are just as welcomed.

In simple terms, I'm wondering if it's possible to to update an environment variable in an active session on rstudio-server, from a background process outside of the session altogether.

For example, let's say:

  1. We have a script that is run every minute as a cron job which polls a series of websites and parses certain elements into a data.frame
  2. Our function first reads in the data.frame that was initially created, and if it detects changes in the websites we're polling, it appends a new row with a time-stamp, and then saves the data.frame back to the rds file; if it does not find any differences it simply exits.
  3. In our .First function, every session begins with assign('.url_df', readRDS('/home/user/url_checkr.rds'), envir = .GlobalEnv) so that we always have the latest update assigned into the active working session, and it always has the same variable name globally.

The goal is now to be able to update that environment variable from our external script, running on cron or alternative, with any updated rows if necessary.

I know how it's possible to send messages between terminals using echo msg > /proc/$pid/fd/0 and know we can use system('ps', intern=TRUE) in our INITIAL function to check for any active sessions and get their pid. The question is how would we/is it possible to use this approach to actually update that .Global variable in the background of the active session with any updated fields? From there it wouldn't be hard to formulate ways to send messages to the console etc.

Purely curious / unsure if im wrapping my head around the whole idea properly?

Thanks!


Solution

  • I think the most straightforward way to do this would be to use R's addTaskCallback function.

    This function effectively allows you to run code every time a top-level R task completes (i.e. after every R expression that's executed in the console). You could establish a task callback which effectively does what you're doing in .First (i.e. updates the global variable if necessary). With the callback in place the global variable would update automatically in the background as users continue to execute R code.

    Depending on the size of your data, you might want to add some safeguards so that the entire RDS file isn't read from disk every time; for instance, you could write a checksum along with the file and just check to see if the checksum is different.