Repository navigation
Expand file tree
/
Copy path01_outbox_basic.cpp
More file actions
88 lines (72 loc) · 2.03 KB
/
Copy path01_outbox_basic.cpp
File metadata and controls
88 lines (72 loc) · 2.03 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
/**
*
* @file 01_outbox_basic.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira.
* All rights reserved.
* https://github.com/vixcpp/vix
*
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
// Run:
// vix run examples/sync/01_outbox_basic.cpp
#include <chrono>
#include <filesystem>
#include <iostream>
#include <memory>
#include <vix/sync/Operation.hpp>
#include <vix/sync/outbox/Outbox.hpp>
#include <vix/sync/outbox/FileOutboxStore.hpp>
static std::int64_t now_ms()
{
using namespace std::chrono;
return duration_cast<milliseconds>(
steady_clock::now().time_since_epoch())
.count();
}
static void reset_dir(const std::filesystem::path &dir)
{
std::error_code ec;
std::filesystem::remove_all(dir, ec);
std::filesystem::create_directories(dir, ec);
}
int main()
{
using namespace vix::sync;
using namespace vix::sync::outbox;
const std::filesystem::path dir = "./build/examples/sync/basic";
reset_dir(dir);
auto store = std::make_shared<FileOutboxStore>(FileOutboxStore::Config{
.file_path = dir / "outbox.json",
.pretty_json = true});
auto outbox = std::make_shared<Outbox>(
Outbox::Config{
.owner = "example-basic"},
store);
Operation op;
op.kind = "http.post";
op.target = "/api/messages";
op.payload = R"({"text":"hello"})";
const auto t0 = now_ms();
const std::string id = outbox->enqueue(op, t0);
std::cout << "enqueued: " << id << "\n";
auto ready = outbox->peek_ready(t0, 10);
std::cout << "ready count: " << ready.size() << "\n";
const bool claimed = outbox->claim(id, t0 + 1);
std::cout << "claimed: " << (claimed ? "yes" : "no") << "\n";
const bool done = outbox->complete(id, t0 + 2);
std::cout << "completed: " << (done ? "yes" : "no") << "\n";
auto saved = store->get(id);
if (saved)
{
std::cout << "final status: "
<< (saved->status == OperationStatus::Done ? "Done" : "Other")
<< "\n";
}
return 0;
}