Run manual diagnostic attachments in the background
This commit is contained in:
1 parent
d845a5e4b6
commit
89ac4d5bcd
40 files changed
+1560
-58
No files matched your search
@@ -20,16 +20,43 @@ type DiagnosticHold struct {
|
||||
// the hold, in one transaction. ErrNotFound: no such validator; ErrBusy: the
|
||||
// validator is not idle; ErrConflict: the address or validator is already held.
|
||||
func (d *DB) ClaimDiagnostic(ctx context.Context, validatorID, address, fipID, note string) error {
|
||||
return d.claimDiagnostic(ctx, validatorID, address, fipID, note, nil)
|
||||
}
|
||||
|
||||
func (d *DB) claimDiagnostic(ctx context.Context, validatorID, address, fipID, note string, item *DiagnosticOperationItem) error {
|
||||
tx, err := d.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
now := timeToDB(Now())
|
||||
var active bool
|
||||
err = tx.QueryRowContext(ctx, `
|
||||
SELECT EXISTS(SELECT 1 FROM ip_queue WHERE ip_address=? AND state IN (?, ?, ?, ?))
|
||||
`, address, IPAssigningFIP, IPAwaitingSelfCheck, IPChecking, IPAggregating).Scan(&active)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if active {
|
||||
return fmt.Errorf("address %q is being checked: %w", address, ErrBusy)
|
||||
}
|
||||
var held bool
|
||||
if err := tx.QueryRowContext(ctx, `
|
||||
SELECT EXISTS(SELECT 1 FROM diagnostic_holds WHERE address=? OR validator_id=?)
|
||||
`, address, validatorID).Scan(&held); err != nil {
|
||||
return err
|
||||
}
|
||||
if held {
|
||||
return fmt.Errorf("address %q or validator %q is already held: %w", address, validatorID, ErrConflict)
|
||||
}
|
||||
var expectedPort string
|
||||
if item != nil {
|
||||
expectedPort = item.PortID
|
||||
}
|
||||
res, err := tx.ExecContext(ctx, `
|
||||
UPDATE validators SET state=?, updated_at=?
|
||||
WHERE validator_id=? AND state=? AND current_ip_id IS NULL
|
||||
`, ValidatorDiagnostic, now, validatorID, ValidatorIdle)
|
||||
WHERE validator_id=? AND state=? AND current_ip_id IS NULL AND (?='' OR os_port_id=?)
|
||||
`, ValidatorDiagnostic, now, validatorID, ValidatorIdle, expectedPort, expectedPort)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -42,12 +69,22 @@ func (d *DB) ClaimDiagnostic(ctx context.Context, validatorID, address, fipID, n
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("validator %q is not idle: %w", validatorID, ErrBusy)
|
||||
return fmt.Errorf("validator %q is no longer idle on the selected port: %w", validatorID, ErrBusy)
|
||||
}
|
||||
var pendingID *int64
|
||||
if item != nil {
|
||||
pendingID = &item.ID
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
UPDATE diagnostic_operation_items SET validator_id=?, port_id=?, fip_id=? WHERE id=? AND status='running'
|
||||
`, validatorID, item.PortID, fipID, item.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO diagnostic_holds (address, validator_id, fip_id, note, created_at) VALUES (?, ?, ?, ?, ?)
|
||||
`, address, validatorID, fipID, note, now); err != nil {
|
||||
return fmt.Errorf("address %q or validator %q is already held: %w", address, validatorID, ErrConflict)
|
||||
INSERT INTO diagnostic_holds (address, validator_id, fip_id, note, created_at, pending_item_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
`, address, validatorID, fipID, note, now, pendingID); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
@@ -60,26 +97,37 @@ func (d *DB) ReleaseDiagnostic(ctx context.Context, address string) (*Diagnostic
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
h, err := scanHold(tx.QueryRowContext(ctx, holdSelect+` WHERE address=?`, address))
|
||||
h, err := scanHold(tx.QueryRowContext(ctx, holdSelect+` WHERE address=? AND pending_item_id IS NULL`, address))
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("address %q is not on diagnostic: %w", address, ErrNotFound)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM diagnostic_holds WHERE address=?`, address); err != nil {
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM diagnostic_holds WHERE address=? AND pending_item_id IS NULL`, address); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
UPDATE validators SET state=?, updated_at=? WHERE validator_id=? AND state=?
|
||||
`, ValidatorIdle, timeToDB(Now()), h.ValidatorID, ValidatorDiagnostic); err != nil {
|
||||
if err := resetDiagnosticValidator(ctx, tx, h.ValidatorID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return h, tx.Commit()
|
||||
}
|
||||
|
||||
func resetDiagnosticValidator(ctx context.Context, tx *sql.Tx, validatorID string) error {
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
UPDATE validators SET state=?, updated_at=? WHERE validator_id=? AND state=?
|
||||
`, ValidatorIdle, timeToDB(Now()), validatorID, ValidatorDiagnostic)
|
||||
return err
|
||||
}
|
||||
|
||||
const holdSelect = `SELECT address, validator_id, fip_id, note, created_at FROM diagnostic_holds`
|
||||
|
||||
func (d *DB) HasDiagnosticHold(ctx context.Context, address string) (bool, error) {
|
||||
var exists bool
|
||||
err := d.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM diagnostic_holds WHERE address=?)`, address).Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
func scanHold(row rowScanner) (*DiagnosticHold, error) {
|
||||
var h DiagnosticHold
|
||||
var created string
|
||||
@@ -93,9 +141,9 @@ func scanHold(row rowScanner) (*DiagnosticHold, error) {
|
||||
return &h, nil
|
||||
}
|
||||
|
||||
// ListDiagnosticHolds returns all holds, oldest first.
|
||||
// ListDiagnosticHolds returns confirmed holds, oldest first.
|
||||
func (d *DB) ListDiagnosticHolds(ctx context.Context) ([]DiagnosticHold, error) {
|
||||
rows, err := d.QueryContext(ctx, holdSelect+` ORDER BY id`)
|
||||
rows, err := d.QueryContext(ctx, holdSelect+` WHERE pending_item_id IS NULL ORDER BY id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user