Files
archie/modules/connectors/module.go
T

165 lines
4.5 KiB
Go

package connectors
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"regexp"
"sort"
"strings"
"sync"
"github.com/jmoiron/sqlx"
"github.com/samcharles93/archie-core/internal/platform/httpjson"
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
)
// validName is the shape of a connector name: it appears in logs and URLs.
var validName = regexp.MustCompile(`^[a-z][a-z0-9-]{0,62}$`)
// Module runs an organisation's accepted connectors and serves their
// administration.
type Module struct {
orgID string
host *Host
store *store.Store
log *slog.Logger
mu sync.RWMutex
running map[string]*Connector
failed map[string]string
}
// NewModule returns the connectors module for orgID.
func NewModule(db *sqlx.DB, log *slog.Logger, orgID string) *Module {
return &Module{
orgID: orgID, host: NewHost(log), store: store.New(db), log: log,
running: map[string]*Connector{}, failed: map[string]string{},
}
}
// Start launches every accepted connector. A connector that fails to start is
// reported by Ready and logged; the others still run.
func (m *Module) Start(ctx context.Context) error {
accepted, err := m.store.List(ctx, m.orgID)
if err != nil {
return err
}
for _, a := range accepted {
// A failure is recorded for Ready and logged; the rest still start.
_ = m.launch(ctx, a)
}
return nil
}
// Get returns the running connector named name.
func (m *Module) Get(name string) (*Connector, bool) {
m.mu.RLock()
defer m.mu.RUnlock()
c, ok := m.running[name]
return c, ok
}
// Ready reports connectors that failed to start or whose process has exited.
func (m *Module) Ready(context.Context) error {
m.mu.RLock()
defer m.mu.RUnlock()
var problems []string
for name, reason := range m.failed {
problems = append(problems, name+": "+reason)
}
for name := range m.running {
if !m.host.Alive(name) {
problems = append(problems, name+": process exited")
}
}
if len(problems) == 0 {
return nil
}
sort.Strings(problems)
return errors.New(strings.Join(problems, "; "))
}
// Close stops every connector.
func (m *Module) Close() { m.host.Close() }
// Routes adds the admin routes, each wrapped in guard.
func (m *Module) Routes(mux *http.ServeMux, guard func(http.Handler) http.Handler) {
mux.Handle("PUT /api/connectors/{name}", guard(http.HandlerFunc(m.accept)))
}
type acceptRequest struct {
Path string `json:"path"`
SHA256 string `json:"sha256"`
Egress []string `json:"egress"`
Env []string `json:"env"`
Settings map[string]string `json:"settings"`
}
func (m *Module) accept(w http.ResponseWriter, r *http.Request) {
name := r.PathValue("name")
var req acceptRequest
if err := httpjson.Read(r, &req); err != nil {
httpjson.Error(w, http.StatusBadRequest, err.Error())
return
}
if !validName.MatchString(name) || req.Path == "" || req.SHA256 == "" {
httpjson.Error(w, http.StatusBadRequest, "a connector needs a lowercase name, a path and a sha256")
return
}
settings, err := json.Marshal(req.Settings)
if err != nil {
httpjson.Error(w, http.StatusBadRequest, err.Error())
return
}
a := store.Accepted{
OrgID: m.orgID, Name: name, Path: req.Path, SHA256: req.SHA256,
Egress: req.Egress, Env: req.Env, Settings: settings,
}
if err := m.store.Upsert(r.Context(), a); err != nil {
httpjson.Error(w, http.StatusInternalServerError, err.Error())
return
}
if err := m.launch(context.WithoutCancel(r.Context()), a); err != nil {
httpjson.Error(w, http.StatusUnprocessableEntity, err.Error())
return
}
httpjson.Write(w, http.StatusOK, map[string]string{"name": name, "state": "running"})
}
// launch starts or restarts one accepted connector and records the outcome.
func (m *Module) launch(ctx context.Context, a store.Accepted) error {
var settings map[string]string
if len(a.Settings) > 0 {
if err := json.Unmarshal(a.Settings, &settings); err != nil {
return m.fail(ctx, a.Name, fmt.Errorf("settings: %w", err))
}
}
c, err := m.host.Start(ctx, Spec{
Name: a.Name, Path: a.Path, SHA256: a.SHA256,
Env: a.Env, Egress: a.Egress, Settings: settings,
})
if err != nil {
return m.fail(ctx, a.Name, err)
}
m.mu.Lock()
m.running[a.Name] = c
delete(m.failed, a.Name)
m.mu.Unlock()
m.log.InfoContext(ctx, "connector started", "connector", a.Name)
return nil
}
func (m *Module) fail(ctx context.Context, name string, err error) error {
m.mu.Lock()
delete(m.running, name)
m.failed[name] = err.Error()
m.mu.Unlock()
m.log.ErrorContext(ctx, "connector failed to start", "connector", name, "error", err)
return err
}