Learn R Programming

microservices (version 0.2.0)

use_microservice: Use a plumber Microservice in an R Project

Description

Lay the infrastructure for a microservice. That includes unit test, dependency packages, configuration file, entrypoints and utility endpoint.

Usage

use_microservice(path = ".", overwrite = FALSE)

Value

No return value, called for side effects.

Arguments

path

(character) Where is the project root folder?

overwrite

(logical) Should existing destination files be overwritten?

Details

How It Works

Given a path to a folder

When use_microservice(path = ".") is called

Then the function creates the following files:

tests/testthat/test-endpoint-plumber-utility.R
inst/configurations/plumber.yml
inst/endpoints/plumber-utility.R
inst/entrypoints/plumber-background.R
inst/entrypoints/plumber-foreground.R

And updates the following files:

tests/testthat/helpers-xyz.R

And adds the following packages to the DESCRIPTION file:

typepackageversion
Suggestsconfig*
Suggestshttptest*
Suggestshttr*
Importsjsonlite*
Suggestspkgload*
Suggestsplumber>= 1.0.0
Importspurrr*
Suggeststestthat*
Suggestsusethis*
Suggestspromises*
Suggestsfuture*

When to Use plumber

plumber Advantages

  • Comes with familiar way to document the microservice endpoint.

  • Maturing package that comes with documentation, examples and support.

plumber Disadvantages

  • Runs on a single thread. That means that parallel algorithms such as random forest, can only be run on one core.

  • Serves only one caller at a time.

  • Can't make inward calls for other services, That means plumber can't be re-entrant. For example, if a microservice has three endpoints,read_table, write_table, and orchestrator, where the orchestrator reads a data table, transforms it, and writes it back, then the orchestrator can't make inwards calls via HTTP to read_table and write_table.

<div class="alert alert-warning"> **Note:** While `plumber` is single-threaded by nature, it is possible to perform parallel execution using the `promises` package. See links under References. </div>

Workflow

  1. Deploy the Microservice infrastructure

microservices::use_microservice(path = ".")
remotes::install_deps(dependencies = TRUE)
devtools::document()

  1. Spin-up the microservice by running source("./inst/entrypoints/plumber-background.R")

  2. Run the microservice unit-test by pressing Ctrl+Shift+T on Windows

Congratulations! You have added a microservice to your application and tested that it works.

References

See Also

Other plumber microservice: add_service()

Examples

Run this code
path <- tempfile()
use_microservice(path)

list.files(path, recursive = TRUE)

cat(read.dcf(file.path(path, "DESCRIPTION"), "Imports"))
cat(read.dcf(file.path(path, "DESCRIPTION"), "Suggests"))

Run the code above in your browser using DataLab