oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package runner
2
3import (
4 "context"
5 "errors"
6
7 "amadan.net/oficina/equipo/internal/runner/provider"
8 "amadan.net/oficina/equipo/internal/runnerproto"
9)
10
11func nonStorageError(err error) bool {
12 return errors.Is(err, ErrJournalBudget) || errors.Is(err, provider.ErrIdentity) || errors.Is(err, errDirectUncertain) || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) || errors.Is(err, runnerproto.ErrInvalid)
13}
14func (j *Journal) storageFailure(ctx context.Context, err error) error {
15 if ctx.Err() != nil {
16 return ctx.Err()
17 }
18 if nonStorageError(err) {
19 return err
20 }
21 return j.fail()
22}
23
24// The operator reset holds admission closed until recovered work is contained
25// and every durable event has been accepted by the instance.
26func (j *Journal) resetAfterReconcile(ctx context.Context, reconcile func(context.Context) error) error {
27 if reconcile == nil {
28 return runnerproto.ErrInvalid
29 }
30 _ = j.fail()
31 if err := reconcile(ctx); err != nil {
32 return err
33 }
34 var pending int
35 if err := j.db.QueryRowContext(ctx, `SELECT
36 (SELECT count(*) FROM journal_events WHERE acked=0)+
37 (SELECT count(*) FROM journal_tools WHERE acked=0)+
38 (SELECT count(*) FROM journal_supervisor WHERE kind='attempt' AND json_extract(body,'$.Phase')!='contained')+
39 (SELECT count(*) FROM direct_attempts WHERE contained=0)+
40 (SELECT count(*) FROM direct_events WHERE projected=0)`).Scan(&pending); err != nil {
41 return j.storageFailure(ctx, err)
42 }
43 if pending != 0 {
44 return ErrJournal
45 }
46 if _, err := j.db.ExecContext(ctx, "UPDATE journal_state SET blocked=0 WHERE id=1"); err != nil {
47 return j.storageFailure(ctx, err)
48 }
49 j.mu.Lock()
50 j.blocked = false
51 j.mu.Unlock()
52 return nil
53}
54
55func Reset(ctx context.Context, c Config) error {
56 r, err := openRuntime(ctx, c)
57 if err != nil {
58 return err
59 }
60 defer r.Close()
61 if err = r.wire(ctx); err != nil {
62 return err
63 }
64 return r.journal.resetAfterReconcile(ctx, func(ctx context.Context) error {
65 if err := r.service.recoverClaims(ctx); err != nil {
66 return err
67 }
68 return r.service.Supervisor.Recover(ctx)
69 })
70}
71