Queue
A general purpose queue that allows you to enqueue and dequeue items singularly or in bulk to it.
Imports and Servicers
To use a Queue
, import the library where you would like to use it.
- Python
- TypeScript
from reboot.std.collections.queue.v1.queue import Item, Queue
import { Queue } from "@reboot-dev/reboot-std/collections/queue/v1";
Also make sure to include the Queue
servicers when starting up your Application
.
(Note: this import is different from above.)
- Python
- TypeScript
from reboot.std.collections.queue.v1 import queue
async def main():
application = Application(
servicers=[MyServicer] + queue.servicers(),
)
await application.run()
import queue from "@reboot-dev/reboot-std/collections/queue/v1";
new Application({
servicers: [MyServicer, ...queue.servicers()],
initialize,
}).run();
Queues
Each Queue
you create will need a unique ID. Using a reference to that
Queue
, you will be able to Enqueue
and Dequeue
Item
s to it
one at a time or in bulk.
Referencing Queues
This creates two Queue
s with IDs "my-first-queue"
and "my-second-queue"
.
- Python
- TypeScript
first_queue = Queue.ref("my-first-queue")
second_queue = Queue.ref("my-second-queue")
const firstQueue = Queue.ref("my-first-queue");
const secondQueue = Queue.ref("my-second-queue");
Items
An Item
can be a Value
, string of bytes
, or Any
and they can be
enqueued and dequeued from a specified Queue
. See the examples below for
how to format Item
s to be enqueued and dequeued.
In practice, you will want to use just one of these three options in a given
Queue
.
To provide the proper formatting to Value
or Any
, this library also requires
a dependency on protobuf in Python, or on
@bufbuild/protobuf in TypeScript.
Any
refers to the Any
protobuf, and is most useful if you are using protobufs to structure your data.
Methods
Enqueue
Enqueues a single Item
as a Value
, bytes
, or Any
.
- Python
- TypeScript
from reboot.protobuf import from_dict
await first_queue.Enqueue(
context,
value=from_dict({"details": "details-go-here"}),
)
await second_queue.Enqueue(context, bytes=b"my-bytes")
await third_queue.Enqueue(context, any=<Any>)
import { Value } from "@bufbuild/protobuf";
await firstQueue.enqueue(context, {
value: Value.fromJson({ details: "details-go-here" }),
});
await secondQueue.enqueue(context, {
bytes: new TextEncoder().encode("my-bytes"),
});
await thirdQueue.enqueue(context, {
any: <Any>,
});
Dequeue
Dequeues a single Item
as the same format you enqueued it.
- Python
- TypeScript
from reboot.protobuf import as_dict
item = await first_queue.Dequeue(context)
print(as_dict(item.value)["details"])
item = await second_queue.Dequeue(context)
print(item.bytes)
item = await third_queue.Dequeue(context)
print(item.any)
const { value } = await firstQueue.dequeue(context);
console.log(value?.toJson()["details"]);
const { bytes } = await secondQueue.dequeue(context);
console.log(bytes);
const { any } = await thirdQueue.dequeue(context);
console.log(any);
Bulk Enqueue
Enqueues a list of Item
s.
This examples demonstrates how to store different types of data in Value
, but
in practice, you will want to use a uniform structure for all Value
s
stored in one Queue
.
- Python
- TypeScript
from reboot.protobuf import from_bool, from_dict, from_int, from_list, from_string
await first_queue.Enqueue(
context,
items=[
Item(value=from_int(3)),
Item(value=from_string("apple")),
Item(value=from_bool(True)),
Item(value=from_dict({"details": "details-go-here"})),
Item(value=from_list(["a", "b", "c"])),
],
)
await second_queue.Enqueue(
context,
items=[
Item(bytes=b"some-bytes"),
Item(bytes=b"some-more-bytes"),
],
)
await third_queue.Enqueue(
context,
items=[
Item(any=<Any>),
Item(any=<Any>),
],
)
import { Value } from "@bufbuild/protobuf";
await firstQueue.enqueue(context, {
items: [
{ value: Value.fromJson(null) },
{ value: Value.fromJson(3) },
{ value: Value.fromJson("apple") },
{ value: Value.fromJson(true) },
{ value: Value.fromJson({ details: "details-go-here" }) },
{ value: Value.fromJson(["a", "b", "c"]) },
],
});
await secondQueue.enqueue(context, { items: [
{ bytes: new TextEncoder().encode("some-bytes") },
{ bytes: new TextEncoder().encode("some-more-bytes") },
]});
await thirdQueue.enqueue(context, { items: [
{ any: <Any> },
{ any: <Any> },
]});
Bulk Dequeue
By specifying bulk
and at_most
/atMost
, you can also dequeue Item
s in bulk.
- Python
- TypeScript
items = await first_queue.Dequeue(context, bulk=True, at_most=5)
# items.items is a list of Items
const { items } = await firstQueue.dequeue(context, {
bulk: true,
atMost: 5,
})
// `items` is a list of Items