1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! Commands for Centrifugo's async consumers (spec 9).
use Serialize;
use RawValue;
use crate::;
/// An API call left for Centrifugo's [async
/// consumers](https://centrifugal.dev/docs/server/consumers) to make, instead
/// of calling Centrifugo: written to a PostgreSQL outbox table, a Kafka topic,
/// a Redis stream. A function per API method makes one, such as
/// [`Command::publish`]: it checks the request as the client does before
/// sending it - an invalid request fails here, rather than in a consumer,
/// which drops a command it cannot run - and encodes it.
///
/// Consumers take a command in one of two forms:
///
/// - its [`method`](Self::method) and [`payload`](Self::payload) apart: the
/// outbox table's `method` and `payload` columns, or a payload with the
/// method in a message attribute (Google Cloud Pub/Sub, AWS SQS, Azure
/// Service Bus, a Redis stream);
/// - the command encoded as JSON, `{"method": ..., "payload": {...}}`, as the
/// message itself (Kafka, NATS JetStream): [`to_json`](Self::to_json), or
/// the command serialized with serde, which gives the payload as the JSON
/// object, never as a string of it.
///
/// ```
/// let data = cent::json(&serde_json::json!({"text": "hi"}))?;
/// let cmd = cent::Command::publish(cent::PublishRequest::new("news").data(data))?;
/// // INSERT INTO centrifugo_outbox (method, payload, partition) VALUES ($1, $2::jsonb, $3)
/// assert_eq!(cmd.method(), "publish");
/// assert_eq!(cmd.payload().get(), r#"{"channel":"news","data":{"text":"hi"}}"#);
/// # Ok::<(), cent::Error>(())
/// ```
///
/// Within a partition - an outbox table's partition, a Kafka partition - a
/// consumer runs commands in order; across partitions it does not. To keep a
/// channel's commands in order, write them to one partition, derived from the
/// channel the same way by every writer: the channel as a Kafka message key,
/// say, or a hash of it modulo the outbox table's number of partitions.