This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
/
persister.go
54 lines (46 loc) · 1.6 KB
/
persister.go
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
package saga
import (
"context"
"github.com/jmalloc/ax"
"github.com/jmalloc/ax/persistence"
)
// Persister is an interface for loading saga instances, and persisting the
// changes that occur to them.
type Persister interface {
// BeginUnitOfWork starts a new unit-of-work that modifies a saga instance.
//
// If the saga instance does not exist, it returns a UnitOfWork with an
// instance at revision zero.
BeginUnitOfWork(
ctx context.Context,
sg Saga,
tx persistence.Tx,
s ax.Sender,
id InstanceID,
) (UnitOfWork, error)
}
// UnitOfWork encapsulates the logic for persisting changes to an instance.
type UnitOfWork interface {
// Sender returns the ax.Sender that the saga must use to send messages.
// This allows the persister to capture new messages if necessary.
Sender() ax.Sender
// Instance returns the saga instance that the unit-of-work applies to.
Instance() Instance
// Save persists changes to the instance.
//
// It returns true if any changes have occurred.
// On success, the Instance().Revision is updated to match the new revision.
Save(ctx context.Context) (bool, error)
// SaveAndComplete persists changes to a completed instance.
//
// The precise behavior is implementation defined. Typically meta-data about
// the instance is discarded. The implementation may completely remove any
// record of the instance.
//
// On success, the Instance().Revision is updated to match the revision
// produced by the save.
SaveAndComplete(ctx context.Context) error
// Close is called when the unit-of-work has ended, regardless of whether
// Save() has been called.
Close()
}