Created missing files: - logger.go: Logger type, setupLogger method, and env helpers - signals_unix.go: Unix signal definitions (SIGTERM, SIGINT, SIGHUP) - signals_windows.go: Windows signal definitions - sync.go: Sync loop implementation with export/import/validation helpers Fixed errors: - Added missing 'version' parameter to acquireDaemonLock call - Removed duplicate setupLock method from process.go (kept in daemon.go) - Removed duplicate startRPCServer from daemon.go (kept in rpc.go) - Fixed LogPath -> LogFile config field reference - Removed unused 'io' import from process.go Implementation notes: - exportToJSONL: Full implementation with dependencies, labels, comments - importFromJSONL: Placeholder (TODO: extract from cmd/bd/import.go) - countDBIssues: Uses SQL COUNT(*) optimization with fallback - validatePostImport: Checks for data loss - runSyncLoop/runEventLoop: Main daemon event loops with signal handling All packages now compile successfully with 'go build ./...' Amp-Thread-ID: https://ampcode.com/threads/T-36a7f730-3420-426f-9e23-f13d5fa089c4 Co-authored-by: Amp <amp@ampcode.com>
64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package daemonrunner
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
)
|
|
|
|
// logger provides simple logging with timestamp
|
|
type logger struct {
|
|
logFunc func(string, ...interface{})
|
|
}
|
|
|
|
func (l *logger) log(format string, args ...interface{}) {
|
|
l.logFunc(format, args...)
|
|
}
|
|
|
|
// setupLogger configures the rotating log file
|
|
func (d *Daemon) setupLogger() (*lumberjack.Logger, *logger) {
|
|
maxSizeMB := getEnvInt("BEADS_DAEMON_LOG_MAX_SIZE", 10)
|
|
maxBackups := getEnvInt("BEADS_DAEMON_LOG_MAX_BACKUPS", 3)
|
|
maxAgeDays := getEnvInt("BEADS_DAEMON_LOG_MAX_AGE", 7)
|
|
compress := getEnvBool("BEADS_DAEMON_LOG_COMPRESS", true)
|
|
|
|
logF := &lumberjack.Logger{
|
|
Filename: d.cfg.LogFile,
|
|
MaxSize: maxSizeMB,
|
|
MaxBackups: maxBackups,
|
|
MaxAge: maxAgeDays,
|
|
Compress: compress,
|
|
}
|
|
|
|
log := &logger{
|
|
logFunc: func(format string, args ...interface{}) {
|
|
msg := fmt.Sprintf(format, args...)
|
|
timestamp := time.Now().Format("2006-01-02 15:04:05")
|
|
_, _ = fmt.Fprintf(logF, "[%s] %s\n", timestamp, msg)
|
|
},
|
|
}
|
|
|
|
return logF, log
|
|
}
|
|
|
|
// getEnvInt reads an integer from environment variable with a default value
|
|
func getEnvInt(key string, defaultValue int) int {
|
|
if val := os.Getenv(key); val != "" {
|
|
var parsed int
|
|
if _, err := fmt.Sscanf(val, "%d", &parsed); err == nil {
|
|
return parsed
|
|
}
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
// getEnvBool reads a boolean from environment variable with a default value
|
|
func getEnvBool(key string, defaultValue bool) bool {
|
|
if val := os.Getenv(key); val != "" {
|
|
return val == "true" || val == "1"
|
|
}
|
|
return defaultValue
|
|
}
|