Download this file
| 1 | package hostops |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/hostclaim" |
| 7 | "github.com/carlosframework/platform/internal/park" |
| 8 | ) |
| 9 | |
| 10 | type ParkScopeProvider func(context.Context, hostclaim.Observed, hostclaim.Obligation) (hostclaim.ParkScope, error) |
| 11 | |
| 12 | // RunOwnedRemote is called only by the appointed regional worker. It never |
| 13 | // takes another region's chore or acknowledges local work on another box. |
| 14 | func RunOwnedRemote(ctx context.Context, e *park.Engine, w park.Work, scope ParkScopeProvider) error { |
| 15 | if e == nil || e.Claims == nil || e.Region == "" || scope == nil || e.Authorize == nil || e.View == nil || e.Leases == nil { |
| 16 | return ErrContract |
| 17 | } |
| 18 | obs, err := hostclaim.Read(ctx, e.Claims, w.Host) |
| 19 | if err != nil { |
| 20 | return err |
| 21 | } |
| 22 | if obs.Absent { |
| 23 | return ErrPending |
| 24 | } |
| 25 | for _, ob := range obs.Claim.Obligations { |
| 26 | if ob.ID != w.ID || ob.Op != w.Op { |
| 27 | continue |
| 28 | } |
| 29 | if !e.OwnsRegion(ob.Region) || ob.Owner != "region:"+ob.Region || (ob.Kind != "park-replica" && ob.Kind != "park-generation") { |
| 30 | return ErrIdentity |
| 31 | } |
| 32 | if ob.Park == nil { |
| 33 | p, err := scope(ctx, obs, ob) |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | if err := e.Bind(ctx, w, p); err != nil { |
| 38 | return err |
| 39 | } |
| 40 | } |
| 41 | return e.Run(ctx, w) |
| 42 | } |
| 43 | return ErrPending |
| 44 | } |
| 45 | |