Runtime Caches

These recipes let an administrator enumerate and manage the runtime caches for Python and R. Posit Connect populates these caches with the package dependencies for your content and makes those packages available when running your content.

Enumerate runtime caches

This example uses the GET /v1/system/caches/runtime endpoint to enumerate the runtime caches created by Connect.

import os
import requests
import pprint

# The connect_server URL must have a trailing slash.
connect_server = os.getenv("CONNECT_SERVER")
connect_api_key = os.getenv("CONNECT_API_KEY")

resp = requests.get(
    f"{connect_server}__api__/v1/system/caches/runtime",
    headers = {
        "Authorization": f"Key {connect_api_key}",
    },
)
payload = resp.json()
pprint.pprint(payload["caches"])
library(httr)

# The connectServer URL must have a trailing slash.
connectServer <- Sys.getenv("CONNECT_SERVER")
connectAPIKey <- Sys.getenv("CONNECT_API_KEY")

resp <- GET(
  paste0(connectServer, "__api__/v1/system/caches/runtime"),
  add_headers(Authorization = paste("Key", connectAPIKey))
)
details <- httr::content(resp, as = "parsed", simplifyDataFrame = TRUE)
print(details$caches)

Delete runtime caches

This example uses the DELETE /v1/system/caches/runtime to request the removal of a named runtime cache.

The GET /v1/tasks/{id} endpoint is used to poll for completion of that requested operation.

import os
import pprint
import requests

# The connect_server URL must have a trailing slash.
connect_server = os.getenv("CONNECT_SERVER")
connect_api_key = os.getenv("CONNECT_API_KEY")

instructions = {
    "language": "Python",
    "version": "3.8.2",
    "image_name": "Local"
}

session = requests.Session()

headers = { "Authorization": f"Key {connect_api_key}" }

resp = session.delete(
    f"{connect_server}__api__/v1/system/caches/runtime",
    headers = headers,
    json = instructions,
)
payload = resp.json()
pprint.pprint(payload)

if "task_id" in payload:
    task_id = payload["task_id"]
    while True:
        print("waiting for task to finish...")
        resp = session.get(
            f"{connect_server}__api__/v1/tasks/{task_id}?wait=1",
            headers = headers,
        )
        payload = resp.json()
        if payload["finished"]:
            if payload["error"]:
                print("operation erred: " + payload["error"])
            else:
                print("operation complete.")
            break
else: 
    print("Error: no task id returned by delete request")
library(httr)

# The connectServer URL must have a trailing slash.
connectServer <- Sys.getenv("CONNECT_SERVER")
connectAPIKey <- Sys.getenv("CONNECT_API_KEY")

instructions = list(
  language = "R",
  version = "3.6.3",
  image_name = "Local"
)

resp <- DELETE(
  paste0(connectServer, "__api__/v1/system/caches/runtime"),
  add_headers(Authorization = paste("Key", connectAPIKey)),
  body = instructions,
  encode = "json"
)
details <- as.data.frame(httr::content(resp, as = "parsed"))
print(details)

taskId <- details$task_id
while(TRUE) {
  resp <- GET(
    paste0(connectServer, "__api__/v1/tasks/", taskId, "?wait=1"),
    add_headers(Authorization = paste("Key", connectAPIKey))
  )
  details <- httr::content(resp, as = "parsed", simplifyDataFrame = TRUE)
  if (details$finished) {
    if (nchar(details$error) > 0) {
      cat(paste0("operation erred: ", details$error, "\n"))
    } else {
      cat("operation complete.\n")
    }
    break
  }
}