| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "github.com/carlosframework/platform/internal/hostbox" |
| 6 | "github.com/carlosframework/platform/internal/hostclaim" |
| 7 | execlease "github.com/carlosframework/platform/internal/hostexec" |
| 8 | "github.com/carlosframework/platform/internal/instancectl" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | func (h *ClaimLaunchRoot) execPermit(ctx context.Context, q hostbox.RoleLaunchRequest) (instancectl.ClaimExecPermit, error) { |
| 13 | r := q.Reservation |
| 14 | o, err := h.Guard.Claims.Read(ctx, r.Target.Host) |
| 15 | if err != nil { |
| 16 | return instancectl.ClaimExecPermit{}, err |
| 17 | } |
| 18 | if o.Absent || o.Claim == nil { |
| 19 | return instancectl.ClaimExecPermit{}, hostbox.ErrChanged |
| 20 | } |
| 21 | _, boot, err := h.Clock.Now() |
| 22 | if err != nil { |
| 23 | return instancectl.ClaimExecPermit{}, err |
| 24 | } |
| 25 | // Serving also gets a finite start window. An operation's lease may only |
| 26 | // shorten it; a later renewal cannot extend this reservation's permit. |
| 27 | until := boot + int64(30*time.Second) |
| 28 | state, err := h.readExecution(ctx, o.Claim) |
| 29 | if err != nil { |
| 30 | return instancectl.ClaimExecPermit{}, err |
| 31 | } |
| 32 | if o.Claim.Op.Kind != hostclaim.KindServing { |
| 33 | if state.SafeUntil < until { |
| 34 | until = state.SafeUntil |
| 35 | } |
| 36 | } |
| 37 | if boot >= until { |
| 38 | return instancectl.ClaimExecPermit{}, execlease.ErrLost |
| 39 | } |
| 40 | return instancectl.ClaimExecPermit{Reservation: r.ID, Inputs: r.Inputs, BootID: r.Target.BootID, SafeUntil: until, Containment: 1}, nil |
| 41 | } |
| 42 | |