|
morph 0.1.0
Asynchronous bridge between GUI thread and business models
|
Replays queued actions from an IOfflineQueue on reconnect. More...
#include <sync_worker.hpp>
Public Types | |
| using | ReplayFunction = std::function<bool(const std::string& payload)> |
| Callable that attempts to replay a single queued item, reporting only success or failure. | |
| using | DetailedReplayFunction = std::function<ReplayOutcome(const std::string& payload)> |
| Callable that attempts to replay a single queued item and reports what it achieved. | |
| using | DeadLetterSink = std::function<void(const QueueItem& poisoned)> |
| Callable invoked when an item exhausts its retry budget, just before it is removed from the queue. | |
Public Member Functions | |
| SyncWorker (::morph::exec::IExecutor &owner, IOfflineQueue &queue, ReplayFunction replay, DeadLetterSink deadLetterSink=nullptr) | |
Constructs a worker that drains queue using replay, on owner. | |
| SyncWorker (::morph::exec::IExecutor &owner, IOfflineQueue &queue, DetailedReplayFunction replay, DeadLetterSink deadLetterSink=nullptr) | |
Constructs a worker that drains queue using replay, which distinguishes an undelivered replay from a rejected one. | |
| ::morph::async::Completion< SyncResult > | run (::morph::exec::IExecutor &replyExec) |
| Drains the queue, replaying each item via the replay function, on the owner. | |
| void | stop () |
| Signals an in-progress drain to stop after the current item. | |
Replays queued actions from an IOfflineQueue on reconnect.
SyncWorker drains the queue and calls a caller-supplied replay function for each item. The framework has no knowledge of what "replay" means — that is entirely the caller's domain. Retry semantics are baked in with hard-coded sensible defaults; there are intentionally no setters.
ReplayFunction (two outcomes — unchanged for existing callers):
Either form:
The drain calls the queue's synchronous verbs, which answer only on the queue's own owner (see IOfflineQueue, "One owner"): give the queue the worker's owner, or the executor that strand runs over.
Definition at line 139 of file sync_worker.hpp.
| using morph::offline::SyncWorker::ReplayFunction = std::function<bool(const std::string& payload)> |
Callable that attempts to replay a single queued item, reporting only success or failure.
Retained unchanged for existing callers, and false keeps meaning exactly what it always did — ReplayOutcome::Rejected, i.e. one attempt spent. A caller that can tell an undelivered replay from a refused one should use DetailedReplayFunction instead; this overload cannot express the difference and so cannot avoid the dead-lettering it causes.
Definition at line 149 of file sync_worker.hpp.
| using morph::offline::SyncWorker::DetailedReplayFunction = std::function<ReplayOutcome(const std::string& payload)> |
Callable that attempts to replay a single queued item and reports what it achieved.
The three-outcome counterpart of ReplayFunction: it is the only form that can report ReplayOutcome::Undelivered and so keep a reconnect flap from spending the item's retry budget.
Definition at line 157 of file sync_worker.hpp.
| using morph::offline::SyncWorker::DeadLetterSink = std::function<void(const QueueItem& poisoned)> |
Callable invoked when an item exhausts its retry budget, just before it is removed from the queue.
Receives the exhausted item (payload, idempotencyKey, and its final cumulative attempt count). If unset, SyncWorker keeps the default behavior: log at morph::log::LogLevel::error and drop. A throwing sink is caught and logged — the item is still removed.
Definition at line 166 of file sync_worker.hpp.
|
inline |
Constructs a worker that drains queue using replay, on owner.
| owner | The executor every drain runs on; must run one task at a time (a strand). Borrowed: it must outlive this worker and keep running tasks until it is destroyed. |
| queue | Queue to drain on each run() call, belonging to owner (or to the executor it runs over). Borrowed, not owned: it must outlive this worker. |
| replay | Function called for each pending item. Stored and invoked for this worker's whole lifetime, so anything the callable refers to must outlive it. |
| deadLetterSink | Optional hook invoked with the exhausted item instead of the default log-and-drop path when an item exhausts its retry budget. Default: unset. Retained on the same terms as replay. |
Definition at line 183 of file sync_worker.hpp.
|
inline |
Constructs a worker that drains queue using replay, which distinguishes an undelivered replay from a rejected one.
Identical to the overload above except for the replay callable's return type. Only ReplayOutcome::Rejected spends an attempt, so a reconnect flap that never reached the peer no longer walks queued work towards the dead-letter sink.
The two overloads are unambiguous: ReplayOutcome is a scoped enum, so neither return type implicitly converts to the other.
| owner | The executor every drain runs on; must run one task at a time (a strand). Borrowed: it must outlive this worker and keep running tasks until it is destroyed. |
| queue | Queue to drain on each run() call, belonging to owner (or to the executor it runs over). Borrowed, not owned: it must outlive this worker. |
| replay | Function called for each pending item. Stored and invoked for this worker's whole lifetime, so anything the callable refers to must outlive it. |
| deadLetterSink | Optional hook invoked with the exhausted item instead of the default log-and-drop path when an item exhausts its retry budget. Default: unset. Retained on the same terms as replay. |
Definition at line 215 of file sync_worker.hpp.
|
inline |
Drains the queue, replaying each item via the replay function, on the owner.
On the owner the drain runs before this returns; from any other thread it is posted there, after any drain posted before it. It processes every pending item, or stops early once stop() is signalled.
If stop() was called before the drain starts, the drain does nothing, resets the stop flag and answers an empty result.
A posted drain refers to this worker, so the worker must outlive every drain it has posted: destroy it once every run() has settled, or after its owner is closed.
| replyExec | Executor the answer is delivered on and the caller attaches its callbacks on: the caller's own. Borrowed: it must outlive the returned Completion. |
replyExec. Definition at line 240 of file sync_worker.hpp.
|
inline |
Signals an in-progress drain to stop after the current item.
Callable from any thread. The drain clears the flag at its start, so stopping is one-shot — but a stop() that lands during a drain leaves the flag set on return, so the next drain takes its early-out and drains nothing; work resumes only on the drain after that.
Definition at line 257 of file sync_worker.hpp.