|
| 1 | +package statemachine |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/qmuntal/stateless" |
| 9 | + |
| 10 | + "github.com/openmeterio/openmeter/openmeter/billing/charges/meta" |
| 11 | + "github.com/openmeterio/openmeter/pkg/models" |
| 12 | +) |
| 13 | + |
| 14 | +type Status interface { |
| 15 | + ~string |
| 16 | + Validate() error |
| 17 | +} |
| 18 | + |
| 19 | +type ChargeLike[CHARGE any, BASE any, STATUS Status] interface { |
| 20 | + GetChargeID() meta.ChargeID |
| 21 | + GetStatus() STATUS |
| 22 | + WithStatus(STATUS) CHARGE |
| 23 | + GetBase() BASE |
| 24 | + WithBase(BASE) CHARGE |
| 25 | +} |
| 26 | + |
| 27 | +type Persistence[CHARGE any, BASE any] struct { |
| 28 | + UpdateBase func(ctx context.Context, base BASE) (BASE, error) |
| 29 | + Refetch func(ctx context.Context, chargeID meta.ChargeID) (CHARGE, error) |
| 30 | +} |
| 31 | + |
| 32 | +type Config[CHARGE ChargeLike[CHARGE, BASE, STATUS], BASE any, STATUS Status] struct { |
| 33 | + Charge CHARGE |
| 34 | + Persistence Persistence[CHARGE, BASE] |
| 35 | +} |
| 36 | + |
| 37 | +type StateMachine[CHARGE any] interface { |
| 38 | + AdvanceUntilStateStable(ctx context.Context) (*CHARGE, error) |
| 39 | + CanFire(ctx context.Context, trigger meta.Trigger) (bool, error) |
| 40 | + FireAndActivate(ctx context.Context, trigger meta.Trigger, args ...any) error |
| 41 | + GetCharge() CHARGE |
| 42 | +} |
| 43 | + |
| 44 | +func (c Config[CHARGE, BASE, STATUS]) Validate() error { |
| 45 | + var errs []error |
| 46 | + |
| 47 | + if c.Persistence.UpdateBase == nil { |
| 48 | + errs = append(errs, errors.New("persistence.update base is required")) |
| 49 | + } |
| 50 | + |
| 51 | + if c.Persistence.Refetch == nil { |
| 52 | + errs = append(errs, errors.New("persistence.refetch is required")) |
| 53 | + } |
| 54 | + |
| 55 | + return errors.Join(errs...) |
| 56 | +} |
| 57 | + |
| 58 | +type Machine[CHARGE ChargeLike[CHARGE, BASE, STATUS], BASE any, STATUS Status] struct { |
| 59 | + Charge CHARGE |
| 60 | + stateMachine *stateless.StateMachine |
| 61 | + config Config[CHARGE, BASE, STATUS] |
| 62 | +} |
| 63 | + |
| 64 | +func New[CHARGE ChargeLike[CHARGE, BASE, STATUS], BASE any, STATUS Status](config Config[CHARGE, BASE, STATUS]) (*Machine[CHARGE, BASE, STATUS], error) { |
| 65 | + if err := config.Validate(); err != nil { |
| 66 | + return nil, fmt.Errorf("config: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + out := &Machine[CHARGE, BASE, STATUS]{ |
| 70 | + Charge: config.Charge, |
| 71 | + config: config, |
| 72 | + } |
| 73 | + |
| 74 | + out.stateMachine = stateless.NewStateMachineWithExternalStorage( |
| 75 | + func(ctx context.Context) (stateless.State, error) { |
| 76 | + return out.Charge.GetStatus(), nil |
| 77 | + }, |
| 78 | + func(ctx context.Context, state stateless.State) error { |
| 79 | + newStatus := state.(STATUS) |
| 80 | + if err := newStatus.Validate(); err != nil { |
| 81 | + return fmt.Errorf("invalid status: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + out.Charge = out.Charge.WithStatus(newStatus) |
| 85 | + |
| 86 | + return nil |
| 87 | + }, |
| 88 | + stateless.FiringImmediate, |
| 89 | + ) |
| 90 | + |
| 91 | + return out, nil |
| 92 | +} |
| 93 | + |
| 94 | +func (m *Machine[CHARGE, BASE, STATUS]) Configure(state STATUS) *stateless.StateConfiguration { |
| 95 | + return m.stateMachine.Configure(state) |
| 96 | +} |
| 97 | + |
| 98 | +func (m *Machine[CHARGE, BASE, STATUS]) CanFire(ctx context.Context, trigger meta.Trigger) (bool, error) { |
| 99 | + return m.stateMachine.CanFireCtx(ctx, trigger) |
| 100 | +} |
| 101 | + |
| 102 | +func (m *Machine[CHARGE, BASE, STATUS]) GetCharge() CHARGE { |
| 103 | + return m.Charge |
| 104 | +} |
| 105 | + |
| 106 | +var ErrUnsupportedOperation = models.NewGenericPreConditionFailedError(fmt.Errorf("unsupported operation")) |
| 107 | + |
| 108 | +func (m *Machine[CHARGE, BASE, STATUS]) FireAndActivate(ctx context.Context, trigger meta.Trigger, args ...any) error { |
| 109 | + canFire, err := m.CanFire(ctx, trigger) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + if !canFire { |
| 115 | + return fmt.Errorf( |
| 116 | + "%w: %s [status=%s,id=%s]", |
| 117 | + ErrUnsupportedOperation, |
| 118 | + trigger, |
| 119 | + m.Charge.GetStatus(), |
| 120 | + m.Charge.GetChargeID().ID, |
| 121 | + ) |
| 122 | + } |
| 123 | + |
| 124 | + if err := m.stateMachine.FireCtx(ctx, trigger, args...); err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + return m.stateMachine.ActivateCtx(ctx) |
| 129 | +} |
| 130 | + |
| 131 | +func (m *Machine[CHARGE, BASE, STATUS]) AdvanceUntilStateStable(ctx context.Context) (*CHARGE, error) { |
| 132 | + var advanced bool |
| 133 | + |
| 134 | + for { |
| 135 | + canFire, err := m.CanFire(ctx, meta.TriggerNext) |
| 136 | + if err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + |
| 140 | + if !canFire { |
| 141 | + if !advanced { |
| 142 | + return nil, nil |
| 143 | + } |
| 144 | + |
| 145 | + charge := m.Charge |
| 146 | + return &charge, nil |
| 147 | + } |
| 148 | + |
| 149 | + currentStatus := m.Charge.GetStatus() |
| 150 | + |
| 151 | + if err := m.FireAndActivate(ctx, meta.TriggerNext); err != nil { |
| 152 | + return nil, fmt.Errorf("cannot transition to the next status [current_status=%s]: %w", currentStatus, err) |
| 153 | + } |
| 154 | + |
| 155 | + updatedBase, err := m.config.Persistence.UpdateBase(ctx, m.Charge.GetBase()) |
| 156 | + if err != nil { |
| 157 | + return nil, fmt.Errorf("persist charge: %w", err) |
| 158 | + } |
| 159 | + |
| 160 | + m.Charge = m.Charge.WithBase(updatedBase) |
| 161 | + |
| 162 | + advanced = true |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func (m *Machine[CHARGE, BASE, STATUS]) RefetchCharge(ctx context.Context) error { |
| 167 | + chargeID := m.Charge.GetChargeID() |
| 168 | + |
| 169 | + charge, err := m.config.Persistence.Refetch(ctx, chargeID) |
| 170 | + if err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + |
| 174 | + m.Charge = charge |
| 175 | + return nil |
| 176 | +} |
0 commit comments