liteq
所属分类:中间件编程
开发工具:R
文件大小:0KB
下载次数:0
上传日期:2022-03-05 15:41:34
上 传 者:
sh-1993
说明: 使用SQLite的无服务器R消息队列
(Serverless R message queue using SQLite)
文件列表:
.Rbuildignore (130, 2023-11-06)
DESCRIPTION (913, 2023-11-06)
LICENSE (43, 2023-11-06)
LICENSE.md (1072, 2023-11-06)
Makefile (127, 2023-11-06)
NAMESPACE (814, 2023-11-06)
NEWS.md (1048, 2023-11-06)
R/ (0, 2023-11-06)
R/assertions.R (1820, 2023-11-06)
R/db.R (12574, 2023-11-06)
R/liteq-package.R (124, 2023-11-06)
R/messages.R (5830, 2023-11-06)
R/package.R (1395, 2023-11-06)
R/queue.R (2496, 2023-11-06)
R/utils.R (404, 2023-11-06)
codecov.yml (232, 2023-11-06)
inst/ (0, 2023-11-06)
inst/examples/ (0, 2023-11-06)
inst/examples/queue-scraper.R (3759, 2023-11-06)
man/ (0, 2023-11-06)
man/ack.Rd (711, 2023-11-06)
man/consume.Rd (859, 2023-11-06)
man/create_queue.Rd (993, 2023-11-06)
man/db_ack.Rd (547, 2023-11-06)
man/db_consume.Rd (521, 2023-11-06)
man/db_create_queue.Rd (1220, 2023-11-06)
man/db_try_consume.Rd (1069, 2023-11-06)
man/default_db.Rd (479, 2023-11-06)
man/delete_queue.Rd (519, 2023-11-06)
man/ensure_db.Rd (333, 2023-11-06)
... ...
# liteq
> Lightweight Portable Message Queue Using SQLite
[![R-CMD-check](https://github.com/r-lib/liteq/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/liteq/actions/workflows/R-CMD-check.yaml)
[![](https://www.r-pkg.org/badges/version/liteq)](https://www.r-pkg.org/pkg/liteq)
[![CRAN RStudio mirror downloads](https://cranlogs.r-pkg.org/badges/liteq)](https://www.r-pkg.org/pkg/liteq)
[![Codecov test coverage](https://codecov.io/gh/r-lib/liteq/branch/main/graph/badge.svg)](https://app.codecov.io/gh/r-lib/liteq?branch=main)
Temporary and permanent message queues for R. Built on top of SQLite
databases. 'SQLite' provides locking, and makes it possible to detect
crashed consumers. Crashed jobs can be automatically marked as "failed",
or put back in the queue again, potentially a limited number of times.
## Installation
Stable version:
```r
install.packages("liteq")
```
Development versiot:
```r
pak::pak("r-lib/liteq")
```
## Introduction
`liteq` implements a serverless message queue system in R.
It can handle multiple databases, and each database can contain
multiple queues.
`liteq` uses SQLite to store a database of queues, and uses other,
temporary SQLites databases for locking, and finding crashed workers
(see below).
## Usage
### Basic usage
```r
library(liteq)
```
In the following we create a queue in a temporary queue database.
The database will be removed if the R session quits.
```r
db <- tempfile()
q <- ensure_queue("jobs", db = db)
q
```
```
#> liteq queue 'jobs'
```
```r
list_queues(db)
```
```
#> [[1]]
#> liteq queue 'jobs'
```
Note that `ensure_queue()` is idempotent, if you call it again on the same
database, it will return the queue that was created previously. So it is
safe to call it multiple times, even from multiple processes. In case of
multiple processes, the locking mechanism eliminates race conditions.
To publish a message in the queue, call `publish()` on the queue object:
```r
publish(q, title = "First message", message = "Hello world!")
publish(q, title = "Second message", message = "Hello again!")
list_messages(q)
```
```
#> id title status
#> 1 1 First message READY
#> 2 2 Second message READY
```
A `liteq` message has a title, which is a string scalar, and the message
body itself is a string scalar as well. To use more complex data types in
messages, you need to serialize them using the `serialize()` function (set
`ascii` to `TRUE`!), or convert them to JSON with the `jsonlite` package.
Two functions are available to consume a message from a queue.
`try_consume()` returns immediately, either with a message (`liteq_message`
object), or `NULL` if the queue is empty. The `consume()` function blocks
if the queue is empty, and waits until a message appears in it.
```r
msg <- try_consume(q)
msg
```
```
#> liteq message from queue 'jobs':
#> First message (12 B)
```
The title and the message body are available as fields of the message
object:
```r
msg$title
```
```
#> [1] "First message"
```
```r
msg$message
```
```
#> [1] "Hello world!"
```
When a consumer is done processing a message it must call `ack()` on the
message object, to notify the queue that it is safe to remove the message.
If the consumer fails to process a message, it can call `nack()` (negative
ackowledgement) on the message object. Then the status of the message will
be set to `"FAILED"`. Failed messages can be removed from the queue, or
put back in the queue again, depending on the application.
```r
ack(msg)
list_messages(q)
```
```
#> id title status
#> 1 2 Second message READY
```
```r
msg2 <- try_consume(q)
nack(msg2)
list_messages(q)
```
```
#> id title status
#> 1 2 Second message FAILED
```
The queue is empty now, so `try_consume()` returns `NULL`:
```r
try_consume(q)
```
```
#> NULL
```
### Crashed workers
If a worker crashes without calling either `ack()` or `nack()` on a message,
then this messages will be put back in the queue the next time a message is
requested from the queue.
To make this possible, each delivered message keeps an open connection to
a lock file, and crashed workers are found by the absense of this open
connection. In R basically means that the worker is considered as crashed
if the R process has no reference to the message object.
Note, that this also means that having many workers at the same time means
that it is possible to reach the maximum number of open connections by
R or the operating system.
## License
MIT Gábor Csárdi
近期下载者:
相关文件:
收藏者: