feat: complete SyncServer implementation

Full-stack Go monolith with embedded React frontend for orchestrating
rsync-over-SSH file synchronization with Wake-on-LAN support.

Features:
- JWT auth (HS256) with bcrypt password hashing
- CRUD for machines (with WoL config) and sync_pairs
- Ed25519 SSH key generation and known_hosts management
- WoL magic packet sender + TCP-connect waiter with backoff
- Sync engine: rsync subprocess, per-pair job queue, progress parsing
- Homebrew cron parser for scheduled syncs
- SSE stream for live job status (queued/waking_up/running/success/failed)
- React+TS+Vite+Tailwind SPA embedded via embed.FS
- Debian packaging with systemd unit, postinst/prerm/postrm

Tech stack:
- Go 1.22+ (CGO_ENABLED=0, pure SQLite via modernc.org/sqlite)
- chi router for HTTP API
- TypeScript + React 18 + Tailwind CSS frontend
- Cross-compiled to Linux amd64 for Proxmox LXC deployment

Tests: wol (MAC parsing, magic packet), syncengine/queue, scheduler/cron
This commit is contained in:
2026-07-07 15:03:22 -04:00
parent 1a66ac58cd
commit 8e08c73f60
69 changed files with 7949 additions and 152 deletions
+153
View File
@@ -0,0 +1,153 @@
package scheduler
import (
"fmt"
"strconv"
"strings"
"time"
)
type CronExpr struct {
Minute []int
Hour []int
DayOfMonth []int
Month []int
DayOfWeek []int
}
func ParseCron(expr string) (*CronExpr, error) {
parts := strings.Fields(expr)
if len(parts) != 5 {
return nil, fmt.Errorf("expected 5 fields, got %d", len(parts))
}
minute, err := parseField(parts[0], 0, 59)
if err != nil {
return nil, fmt.Errorf("minute: %w", err)
}
hour, err := parseField(parts[1], 0, 23)
if err != nil {
return nil, fmt.Errorf("hour: %w", err)
}
dom, err := parseField(parts[2], 1, 31)
if err != nil {
return nil, fmt.Errorf("day of month: %w", err)
}
month, err := parseField(parts[3], 1, 12)
if err != nil {
return nil, fmt.Errorf("month: %w", err)
}
dow, err := parseField(parts[4], 0, 6)
if err != nil {
return nil, fmt.Errorf("day of week: %w", err)
}
return &CronExpr{
Minute: minute,
Hour: hour,
DayOfMonth: dom,
Month: month,
DayOfWeek: dow,
}, nil
}
func parseField(field string, min, max int) ([]int, error) {
if field == "*" {
var vals []int
for i := min; i <= max; i++ {
vals = append(vals, i)
}
return vals, nil
}
var result []int
parts := strings.Split(field, ",")
for _, part := range parts {
if strings.Contains(part, "/") {
stepParts := strings.Split(part, "/")
if len(stepParts) != 2 {
return nil, fmt.Errorf("invalid step: %s", part)
}
rangePart := stepParts[0]
step, err := strconv.Atoi(stepParts[1])
if err != nil {
return nil, fmt.Errorf("invalid step value: %s", stepParts[1])
}
var start, end int
if rangePart == "*" {
start, end = min, max
} else if strings.Contains(rangePart, "-") {
rp := strings.Split(rangePart, "-")
if len(rp) != 2 {
return nil, fmt.Errorf("invalid range: %s", rangePart)
}
start, _ = strconv.Atoi(rp[0])
end, _ = strconv.Atoi(rp[1])
} else {
v, _ := strconv.Atoi(rangePart)
start, end = v, v
}
for i := start; i <= end; i += step {
result = append(result, i)
}
} else if strings.Contains(part, "-") {
rp := strings.Split(part, "-")
if len(rp) != 2 {
return nil, fmt.Errorf("invalid range: %s", part)
}
start, _ := strconv.Atoi(rp[0])
end, _ := strconv.Atoi(rp[1])
for i := start; i <= end; i++ {
result = append(result, i)
}
} else {
v, err := strconv.Atoi(part)
if err != nil {
return nil, fmt.Errorf("invalid value: %s", part)
}
if v < min || v > max {
return nil, fmt.Errorf("value %d out of range [%d,%d]", v, min, max)
}
result = append(result, v)
}
}
return result, nil
}
func (c *CronExpr) Matches(t time.Time) bool {
if !contains(c.Minute, t.Minute()) {
return false
}
if !contains(c.Hour, t.Hour()) {
return false
}
if !contains(c.DayOfMonth, t.Day()) {
return false
}
if !contains(c.Month, int(t.Month())) {
return false
}
if !contains(c.DayOfWeek, int(t.Weekday())) {
return false
}
return true
}
func contains(slice []int, val int) bool {
for _, v := range slice {
if v == val {
return true
}
}
return false
}
func NextRun(expr *CronExpr, from time.Time) time.Time {
for i := 1; i <= 525600; i++ {
t := from.Add(time.Duration(i) * time.Minute)
if expr.Matches(t) {
return t
}
}
return from.AddDate(0, 0, 1)
}
+97
View File
@@ -0,0 +1,97 @@
package scheduler
import (
"testing"
"time"
)
func TestParseCron(t *testing.T) {
tests := []struct {
expr string
wantErr bool
}{
{"* * * * *", false},
{"0 * * * *", false},
{"*/5 * * * *", false},
{"0,30 * * * *", false},
{"0-30 * * * *", false},
{"*/15 9-17 * * *", false},
{"0 0 1 * *", false},
{"0 0 * * 0", false},
{"0 0 1,15 * *", false},
{"invalid", true},
{"* * * *", true},
{"60 * * * *", true},
{"* 24 * * *", true},
}
for _, tt := range tests {
_, err := ParseCron(tt.expr)
if (err != nil) != tt.wantErr {
t.Errorf("ParseCron(%q) error = %v, wantErr %v", tt.expr, err, tt.wantErr)
}
}
}
func TestCronMatches(t *testing.T) {
expr, err := ParseCron("*/5 * * * *")
if err != nil {
t.Fatal(err)
}
tests := []struct {
minute int
want bool
}{
{0, true},
{5, true},
{10, true},
{15, true},
{1, false},
{2, false},
{7, false},
}
now := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
for _, tt := range tests {
tm := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), tt.minute, 0, 0, time.UTC)
if got := expr.Matches(tm); got != tt.want {
t.Errorf("Matches(minute=%d) = %v, want %v", tt.minute, got, tt.want)
}
}
}
func TestCronMatchesSpecific(t *testing.T) {
expr, err := ParseCron("30 9 15 * *")
if err != nil {
t.Fatal(err)
}
matches := time.Date(2024, 6, 15, 9, 30, 0, 0, time.UTC)
if !expr.Matches(matches) {
t.Error("should match 9:30 on 15th of month")
}
notMatch := time.Date(2024, 6, 16, 9, 30, 0, 0, time.UTC)
if expr.Matches(notMatch) {
t.Error("should not match 9:30 on 16th of month")
}
}
func TestNextRun(t *testing.T) {
expr, err := ParseCron("*/5 * * * *")
if err != nil {
t.Fatal(err)
}
from := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
next := NextRun(expr, from)
if next.Minute() != 5 || next.Hour() != 12 {
t.Errorf("NextRun = %v, want 12:05", next)
}
if !next.After(from) {
t.Error("NextRun should be after from time")
}
}
+95
View File
@@ -0,0 +1,95 @@
package scheduler
import (
"context"
"database/sql"
"log/slog"
"sync"
"time"
"github.com/syncserver/internal/config"
"github.com/syncserver/internal/models"
"github.com/syncserver/internal/syncengine"
)
type Scheduler struct {
db *sql.DB
engine *syncengine.Engine
cfg *config.Config
stopCh chan struct{}
wg sync.WaitGroup
}
func New(database interface{ SQLDB() *sql.DB }, engine *syncengine.Engine, cfg *config.Config) *Scheduler {
return &Scheduler{
db: database.SQLDB(),
engine: engine,
cfg: cfg,
stopCh: make(chan struct{}),
}
}
func (s *Scheduler) Start() {
s.wg.Add(1)
go s.run()
slog.Info("scheduler started")
}
func (s *Scheduler) Stop() {
close(s.stopCh)
s.wg.Wait()
slog.Info("scheduler stopped")
}
func (s *Scheduler) run() {
defer s.wg.Done()
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for {
select {
case <-s.stopCh:
return
case <-ticker.C:
s.tick()
}
}
}
func (s *Scheduler) tick() {
scheduleRepo := models.NewScheduleRepository(s.db)
now := time.Now().UTC()
schedules, err := scheduleRepo.GetEnabledDue(now)
if err != nil {
slog.Error("scheduler: failed to get due schedules", "error", err)
return
}
for _, sch := range schedules {
pairRepo := models.NewSyncPairRepository(s.db)
pair, err := pairRepo.GetByID(sch.SyncPairID)
if err != nil || !pair.Enabled {
continue
}
jobID, err := s.engine.CreateJob(sch.SyncPairID, "scheduled")
if err != nil {
slog.Error("scheduler: failed to create job", "schedule_id", sch.ID, "error", err)
continue
}
ctx := context.Background()
go func(jobID int64, pairID int64, schID int64) {
if err := s.engine.Run(ctx, jobID, pairID); err != nil {
slog.Warn("scheduler: job failed", "job_id", jobID, "error", err)
}
expr, _ := ParseCron(sch.CronExpr)
if expr != nil {
next := NextRun(expr, time.Now().UTC())
scheduleRepo.UpdateNextRun(schID, next)
}
}(jobID, sch.SyncPairID, sch.ID)
}
}