|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + |
| 11 | + mprbackend "github.com/mendixlabs/mxcli/mdl/backend/mpr" |
| 12 | +) |
| 13 | + |
| 14 | +// persistentDaemonBackend holds the pre-connected backend for the daemon process. |
| 15 | +// Non-nil only in --serve + --mpr-path mode (per-MPR daemon). |
| 16 | +// Kept as concrete type so duck-type checks (microflowsRepoProvider etc.) work. |
| 17 | +// Never modified after main() sets it. |
| 18 | +var persistentDaemonBackend *mprbackend.MprBackend |
| 19 | + |
| 20 | +// daemonRequestMu serializes command execution on the persistent backend. |
| 21 | +// SQLite is single-writer; os.Chdir is process-global — both require serialization. |
| 22 | +var daemonRequestMu sync.Mutex |
| 23 | + |
| 24 | +// noOpConnectBackend wraps *mprbackend.MprBackend (not the interface) so that |
| 25 | +// duck-type checks like b.(microflowsRepoProvider) still succeed — the concrete |
| 26 | +// type's Microflows() / Nanoflows() / etc. methods are promoted and visible. |
| 27 | +// Only Connect / Disconnect / IsConnected are overridden to be no-ops so the |
| 28 | +// persistent SQLite connection is never closed between daemon requests. |
| 29 | +type noOpConnectBackend struct{ *mprbackend.MprBackend } |
| 30 | + |
| 31 | +func (n *noOpConnectBackend) Connect(string) error { return nil } |
| 32 | +func (n *noOpConnectBackend) Disconnect() error { return nil } |
| 33 | +func (n *noOpConnectBackend) IsConnected() bool { return true } |
| 34 | + |
| 35 | +// openPersistentBackend opens an MprBackend and returns it as FullBackend. |
| 36 | +// Called once at daemon startup; the connection lives for the daemon's lifetime. |
| 37 | +// EnableContentCache is called so mxunit file reads are cached in memory: |
| 38 | +// the first request populates the cache; subsequent requests skip all file I/O. |
| 39 | +func openPersistentBackend(mprPath string) (*mprbackend.MprBackend, error) { |
| 40 | + b := mprbackend.New() |
| 41 | + if err := b.Connect(mprPath); err != nil { |
| 42 | + return nil, fmt.Errorf("pre-connect %s: %w", mprPath, err) |
| 43 | + } |
| 44 | + b.EnableContentCache() |
| 45 | + return b, nil |
| 46 | +} |
| 47 | + |
| 48 | +// extractMPRPath scans args for "--mpr-path <path>" or "--mpr-path=<path>". |
| 49 | +func extractMPRPath(args []string) string { |
| 50 | + const flag = "--mpr-path" |
| 51 | + for i, a := range args { |
| 52 | + if a == flag && i+1 < len(args) { |
| 53 | + return args[i+1] |
| 54 | + } |
| 55 | + if strings.HasPrefix(a, flag+"=") { |
| 56 | + return a[len(flag)+1:] |
| 57 | + } |
| 58 | + } |
| 59 | + return "" |
| 60 | +} |
| 61 | + |
| 62 | +// closePersistentBackend is called (deferred) when the daemon exits. |
| 63 | +func closePersistentBackend() { |
| 64 | + if persistentDaemonBackend != nil { |
| 65 | + _ = persistentDaemonBackend.Disconnect() |
| 66 | + // Also log to stderr so it's visible in diagnostic output. |
| 67 | + fmt.Fprintln(os.Stderr, "mxcli-daemon: persistent backend closed") |
| 68 | + } |
| 69 | +} |
0 commit comments