feat(assessment): record capture hints and keep one window per attempt
This commit is contained in:
1 parent
c975a0d506
commit
bb917bf62e
108 files changed
+6649
-139
No files matched your search
@@ -0,0 +1,95 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ErrInvalidAssessmentWorkLease reports a malformed work session lease.
|
||||
var ErrInvalidAssessmentWorkLease = errors.New("invalid assessment work lease")
|
||||
|
||||
const (
|
||||
// AssessmentWorkLeaseDuration is how long a hold of the work session lasts
|
||||
// after its last renewal. It is long enough for a background tab, whose
|
||||
// timers a browser slows to about one a minute, to renew in time, and short
|
||||
// enough that a crashed page frees the attempt for the candidate quickly.
|
||||
AssessmentWorkLeaseDuration = 2 * time.Minute
|
||||
// AssessmentWorkLeaseRenewEvery is how often a page repeats the hold.
|
||||
AssessmentWorkLeaseRenewEvery = 15 * time.Second
|
||||
)
|
||||
|
||||
// AssessmentWorkLease is the hold of the single active work session of a real
|
||||
// attempt. One open page holds it at a time: it is a rule that keeps one
|
||||
// attempt from being worked on in two windows, not a lock on the candidate. The
|
||||
// holder is only a keyed digest of the identifier the page generated, so the
|
||||
// value never reaches the store.
|
||||
type AssessmentWorkLease struct {
|
||||
AttemptID AssessmentAttemptID
|
||||
Holder AssessmentDigest
|
||||
KeyVersion int16
|
||||
// AcquiredAt is when the current holder took the session; a renewal keeps it.
|
||||
AcquiredAt time.Time
|
||||
RenewedAt time.Time
|
||||
// ReleasedAt is set when the holder closed its page, and zero otherwise.
|
||||
ReleasedAt time.Time
|
||||
}
|
||||
|
||||
// Validate checks that the lease can be stored.
|
||||
func (lease AssessmentWorkLease) Validate() error {
|
||||
const location = "AssessmentWorkLease.Validate"
|
||||
switch {
|
||||
case lease.AttemptID.IsZero(), lease.Holder.IsZero(), lease.KeyVersion < 1:
|
||||
return fmt.Errorf("%s - attempt, holder, and key version are required: %w", location, ErrInvalidAssessmentWorkLease)
|
||||
case lease.AcquiredAt.IsZero(), lease.RenewedAt.Before(lease.AcquiredAt):
|
||||
return fmt.Errorf("%s - acquisition and renewal times are inconsistent: %w", location, ErrInvalidAssessmentWorkLease)
|
||||
case !lease.ReleasedAt.IsZero() && lease.ReleasedAt.Before(lease.AcquiredAt):
|
||||
return fmt.Errorf("%s - release predates acquisition: %w", location, ErrInvalidAssessmentWorkLease)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ActiveAt reports whether the hold is in force at the time: not released and
|
||||
// renewed within AssessmentWorkLeaseDuration.
|
||||
func (lease AssessmentWorkLease) ActiveAt(now time.Time) bool {
|
||||
return lease.ReleasedAt.IsZero() && now.Before(lease.RenewedAt.Add(AssessmentWorkLeaseDuration))
|
||||
}
|
||||
|
||||
// HeldUntil is the end of the hold if it is not renewed.
|
||||
func (lease AssessmentWorkLease) HeldUntil() time.Time {
|
||||
return lease.RenewedAt.Add(AssessmentWorkLeaseDuration)
|
||||
}
|
||||
|
||||
// AdmitsHolder reports whether a page with the given holder digest may work on
|
||||
// the attempt now: nobody holds it, the hold was released or has expired, or
|
||||
// the page is the holder. An absent lease is passed as the zero value.
|
||||
func (lease AssessmentWorkLease) AdmitsHolder(holder AssessmentDigest, now time.Time) bool {
|
||||
return lease.Holder.IsZero() || !lease.ActiveAt(now) || lease.Holder == holder
|
||||
}
|
||||
|
||||
// Held returns the lease after the page took or renewed it at the time. A
|
||||
// change of holder, a released hold, or an expired hold starts a new
|
||||
// acquisition; a renewal by the holder keeps the acquisition time.
|
||||
func (lease AssessmentWorkLease) Held(
|
||||
attemptID AssessmentAttemptID,
|
||||
holder AssessmentDigest,
|
||||
keyVersion int16,
|
||||
now time.Time,
|
||||
) AssessmentWorkLease {
|
||||
renewal := !lease.Holder.IsZero() && lease.Holder == holder && lease.ActiveAt(now)
|
||||
next := AssessmentWorkLease{AttemptID: attemptID, Holder: holder, KeyVersion: keyVersion, AcquiredAt: now, RenewedAt: now}
|
||||
if renewal && lease.KeyVersion == keyVersion {
|
||||
next.AcquiredAt = lease.AcquiredAt
|
||||
}
|
||||
return next
|
||||
}
|
||||
|
||||
// ReleasedBy returns the lease released by the holder at the time. Any other
|
||||
// page, and a lease that is no longer held, leave it as it is.
|
||||
func (lease AssessmentWorkLease) ReleasedBy(holder AssessmentDigest, now time.Time) AssessmentWorkLease {
|
||||
if lease.Holder.IsZero() || lease.Holder != holder || !lease.ReleasedAt.IsZero() {
|
||||
return lease
|
||||
}
|
||||
lease.ReleasedAt = now
|
||||
return lease
|
||||
}
|
||||
Reference in new issue
Block a user