oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1package store
2
3import (
4 "context"
5 "database/sql"
6 "errors"
7 "time"
8)
9
10// Install is one app's install on this Base. State is "installing",
11// "installed" or "failed".
12type Install struct {
13 App, Host, State string
14 Error string
15 UpdatedAt time.Time
16}
17
18func Installs(ctx context.Context, db *sql.DB) ([]Install, error) {
19 rows, err := db.QueryContext(ctx, `SELECT app, host, state, error, updated_at FROM installs ORDER BY app`)
20 if err != nil {
21 return nil, err
22 }
23 defer rows.Close()
24 var out []Install
25 for rows.Next() {
26 var i Install
27 if err := rows.Scan(&i.App, &i.Host, &i.State, &i.Error, &i.UpdatedAt); err != nil {
28 return nil, err
29 }
30 out = append(out, i)
31 }
32 return out, rows.Err()
33}
34
35// ClaimInstall moves app into "installing" and reports true, but only when
36// it has no row or its row is "failed". Callers that get false must not
37// install.
38func ClaimInstall(ctx context.Context, db *sql.DB, app, host string) (bool, error) {
39 res, err := db.ExecContext(ctx,
40 `INSERT INTO installs (app, host, state, error, updated_at) VALUES (?, ?, 'installing', '', ?)
41 ON CONFLICT (app) DO UPDATE SET host = excluded.host, state = 'installing', error = '', updated_at = excluded.updated_at
42 WHERE installs.state = 'failed'`,
43 app, host, time.Now().UTC())
44 if err != nil {
45 return false, err
46 }
47 n, err := res.RowsAffected()
48 return n == 1, err
49}
50
51// ErrNoInstall means an install row that was expected is gone.
52var ErrNoInstall = errors.New("no such install")
53
54// SetInstallState sets app's state. It returns ErrNoInstall if app has no row.
55func SetInstallState(ctx context.Context, db *sql.DB, app, state, errMsg string) error {
56 res, err := db.ExecContext(ctx, `UPDATE installs SET state = ?, error = ?, updated_at = ? WHERE app = ?`,
57 state, errMsg, time.Now().UTC(), app)
58 if err != nil {
59 return err
60 }
61 if n, err := res.RowsAffected(); err != nil {
62 return err
63 } else if n == 0 {
64 return ErrNoInstall
65 }
66 return nil
67}
68
69// DeleteInstall removes app's row unless it is "installing", in which case
70// it leaves the row alone and reports busy. A missing row is not busy. When
71// removedDefault is set, the same transaction records that the owner removed
72// this default app, so DefaultRemoved reports it from then on.
73func DeleteInstall(ctx context.Context, db *sql.DB, app string, removedDefault bool) (busy bool, err error) {
74 tx, err := db.BeginTx(ctx, nil)
75 if err != nil {
76 return false, err
77 }
78 defer tx.Rollback()
79 res, err := tx.ExecContext(ctx, `DELETE FROM installs WHERE app = ? AND state <> 'installing'`, app)
80 if err != nil {
81 return false, err
82 }
83 n, err := res.RowsAffected()
84 if err != nil {
85 return false, err
86 }
87 if n == 0 {
88 var one int
89 err = tx.QueryRowContext(ctx, `SELECT 1 FROM installs WHERE app = ?`, app).Scan(&one)
90 if err == nil {
91 return true, nil
92 }
93 if !errors.Is(err, sql.ErrNoRows) {
94 return false, err
95 }
96 }
97 if removedDefault {
98 if _, err := tx.ExecContext(ctx, `INSERT INTO removed_defaults (app, removed_at) VALUES (?, ?)
99 ON CONFLICT (app) DO UPDATE SET removed_at = excluded.removed_at`, app, time.Now().UTC()); err != nil {
100 return false, err
101 }
102 }
103 return false, tx.Commit()
104}
105
106// DefaultRemoved reports whether the owner uninstalled app, a default app,
107// and has not installed it again since.
108func DefaultRemoved(ctx context.Context, db *sql.DB, app string) (bool, error) {
109 var one int
110 err := db.QueryRowContext(ctx, `SELECT 1 FROM removed_defaults WHERE app = ?`, app).Scan(&one)
111 if errors.Is(err, sql.ErrNoRows) {
112 return false, nil
113 }
114 return err == nil, err
115}
116
117// ClearDefaultRemoved forgets that app was removed: someone chose to install
118// it again.
119func ClearDefaultRemoved(ctx context.Context, db *sql.DB, app string) error {
120 _, err := db.ExecContext(ctx, `DELETE FROM removed_defaults WHERE app = ?`, app)
121 return err
122}
123
124// ResetInterrupted marks every "installing" row "failed". Call it once at
125// boot: Base is one process, so nothing is genuinely installing then.
126func ResetInterrupted(ctx context.Context, db *sql.DB) error {
127 _, err := db.ExecContext(ctx, `UPDATE installs SET state = 'failed', error = ?, updated_at = ? WHERE state = 'installing'`,
128 InterruptedMessage, time.Now().UTC())
129 return err
130}
131
132// InterruptedMessage is stored on installs cut short by a restart.
133const InterruptedMessage = "Interrupted. Try again."
134