Download this file
| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "github.com/carlosframework/platform/internal/hostbox" |
| 7 | "github.com/carlosframework/platform/internal/hostguard" |
| 8 | "os" |
| 9 | ) |
| 10 | |
| 11 | // A row mutation can commit before the inventory journal. Settle that exact |
| 12 | // root intention locally even if the claim has since advanced. This grants no |
| 13 | // new timer write: SQL is checked only against the effect root already intended. |
| 14 | func (h ClaimRootHTTP) settleTimerInventory(ctx context.Context, q hostbox.JournalRequest, raw []byte) (bool, error) { |
| 15 | if q.Timer == nil { |
| 16 | return false, nil |
| 17 | } |
| 18 | if h.Reconcile.Registry == nil || h.Journals == nil { |
| 19 | return false, hostbox.ErrUnsupported |
| 20 | } |
| 21 | m := q.Timer |
| 22 | applied := func() (bool, error) { |
| 23 | row, err := h.Reconcile.Registry.GetTimer(m.Timer.Host, m.Timer.Name) |
| 24 | if errors.Is(err, os.ErrNotExist) { |
| 25 | return m.Remove, nil |
| 26 | } |
| 27 | if err != nil { |
| 28 | return false, err |
| 29 | } |
| 30 | return !m.Remove && row != nil && *row == m.Timer, nil |
| 31 | } |
| 32 | done, err := applied() |
| 33 | if err != nil || !done { |
| 34 | return false, err |
| 35 | } |
| 36 | err = h.Stop.Coordinator.Guard.SettleRootInventory(ctx, q.Target.Host, raw, func(in hostguard.RootInstallation) error { |
| 37 | if in.Host != q.Inventory.Host || in.Incarnation != q.Inventory.Incarnation || in.Stamp != q.Inventory.InstallationStamp || in.InventoryRevision != q.Revision+1 { |
| 38 | return hostbox.ErrChanged |
| 39 | } |
| 40 | exact, err := applied() |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | if !exact { |
| 45 | return hostbox.ErrChanged |
| 46 | } |
| 47 | _, err = h.Journals.CompletePendingInventory(ctx, in.Box+"/"+in.Enrollment, q) |
| 48 | return err |
| 49 | }) |
| 50 | return err == nil, err |
| 51 | } |
| 52 | |