-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathlib.rs
More file actions
89 lines (79 loc) · 2.95 KB
/
Copy pathlib.rs
File metadata and controls
89 lines (79 loc) · 2.95 KB
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
82
83
84
85
86
87
88
89
use spacetimedb::{
procedure, reducer, table, DbContext, ProcedureContext, ReducerContext, ScheduleAt, Table, TxContext,
};
use std::time::Duration;
#[table(public, accessor = procedure_concurrency_row)]
struct ProcedureConcurrencyRow {
#[auto_inc]
insertion_order: u32,
insertion_context: String,
}
fn insert_procedure_concurrency_row(ctx: &TxContext, insertion_context: &str) {
ctx.db.procedure_concurrency_row().insert(ProcedureConcurrencyRow {
insertion_order: 0,
insertion_context: insertion_context.into(),
});
}
#[reducer]
fn insert_reducer_row(ctx: &ReducerContext) {
ctx.db().procedure_concurrency_row().insert(ProcedureConcurrencyRow {
insertion_order: 0,
insertion_context: "reducer".into(),
});
}
#[procedure]
fn procedure_sleep_between_inserts(ctx: &mut ProcedureContext) {
ctx.with_tx(|ctx| insert_procedure_concurrency_row(ctx, "procedure_before"));
ctx.sleep_until(ctx.timestamp + Duration::from_secs(10));
ctx.with_tx(|ctx| insert_procedure_concurrency_row(ctx, "procedure_after"));
}
#[table(accessor = scheduled_reducer_row, scheduled(insert_scheduled_reducer))]
struct ScheduledReducerRow {
#[primary_key]
#[auto_inc]
scheduled_id: u64,
scheduled_at: ScheduleAt,
}
#[reducer]
fn insert_scheduled_reducer(ctx: &ReducerContext, _schedule: ScheduledReducerRow) {
ctx.db().procedure_concurrency_row().insert(ProcedureConcurrencyRow {
insertion_order: 0,
insertion_context: "scheduled_reducer".into(),
});
}
#[procedure]
fn procedure_schedule_reducer_between_inserts(ctx: &mut ProcedureContext) {
ctx.with_tx(|ctx| {
insert_procedure_concurrency_row(ctx, "procedure_before");
ctx.db.scheduled_reducer_row().insert(ScheduledReducerRow {
scheduled_id: 0,
scheduled_at: ctx.timestamp.into(),
});
});
ctx.sleep_until(ctx.timestamp + Duration::from_secs(10));
ctx.with_tx(|ctx| insert_procedure_concurrency_row(ctx, "procedure_after"));
}
#[table(accessor = scheduled_procedure_row, scheduled(scheduled_procedure_sleep_between_inserts))]
struct ScheduledProcedureRow {
#[primary_key]
#[auto_inc]
scheduled_id: u64,
scheduled_at: ScheduleAt,
}
#[procedure]
fn scheduled_procedure_sleep_between_inserts(ctx: &mut ProcedureContext, _schedule: ScheduledProcedureRow) {
ctx.with_tx(|ctx| insert_procedure_concurrency_row(ctx, "scheduled_procedure_before"));
ctx.sleep_until(ctx.timestamp + Duration::from_secs(10));
ctx.with_tx(|ctx| insert_procedure_concurrency_row(ctx, "scheduled_procedure_after"));
}
#[reducer]
fn schedule_procedure_then_reducer(ctx: &ReducerContext) {
ctx.db().scheduled_procedure_row().insert(ScheduledProcedureRow {
scheduled_id: 0,
scheduled_at: ctx.timestamp.into(),
});
ctx.db().scheduled_reducer_row().insert(ScheduledReducerRow {
scheduled_id: 0,
scheduled_at: (ctx.timestamp + Duration::from_secs(2)).into(),
});
}