简体   繁体   中英

What is the best way to keep a permanent log from R on stateless (Google Cloud Engine) docker?

I am using a stateless docker image to run various data processing tasks on a Google Cloud VM. When the process interrupts - due to the machine being preempted or R crashing - I need to know where to continue. So how can I keep a permanent log that I can read and write from R?

Currently, I am updating a text file on Google Drive, but that does not feel like the fastest or most stable solution - any better ideas?

It looks like the logs you are referring to are not just logging, but represent a state. I would personally write these logs/state to a Google Cloud Storage bucket. This is the easiest way to do looking at your setup.

There already is a GCS library for R, you can find it here . Remember to give the compute service account the correct permissions on the bucket, otherwise, you would run into a permission denied error.

2 options for you:

  • use a log server and report log messages to it so they are maintained even when the server goes down
  • log messages to a volume that persists beyond the life of the container (this is possible with Kubernetes deployments of containers).. not entirely sure how youd do this with a purely GCP deployment. I'll leave you to investigate that

Thanks to @Nebulastic I have now gotten it to work with GoogleCloudStorage - seems to be a lot faster and more reliable than Google Drive. In case they are helpful for anyone, here the two wrapper functions I am now using (after creating a service account with appropriate permissions and creating the bucket):

log_to_gcs <- function(message, log_name = "mylog", bucket = "logs-and-states") {
  gcs_auth("~/gcs_key.json")
  objects <- gcs_list_objects(bucket = bucket)
if (log_name %in% objects$name) {
  log <- gcs_get_object(log_name, bucket = bucket)
} else {
  log <- character()  
}

log <- c(log, message)
tmp <- tempfile()
writeLines(log, tmp)
gcs_upload(tmp, name = "mylog", bucket = bucket)
file.remove(tmp)
}

read_log_gcs <- function(log_name = "mylog", bucket = "logs-and-states") {
  gcs_get_object(log_name, bucket = bucket)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM