saving
This commit is contained in:
parent
556935a59e
commit
765dd3229e
|
|
@ -84,13 +84,16 @@ func (a *app) executeJob(ctx context.Context, job store.Job) {
|
|||
}
|
||||
|
||||
func (a *app) runPreflightJob(ctx context.Context, job store.Job, site store.Site) (string, string) {
|
||||
a.log.Debug("preflight begin", zap.Int64("job_id", job.ID), zap.Int64("site_id", site.ID), zap.Int("targets", len(site.Targets)))
|
||||
failures := 0
|
||||
warnings := 0
|
||||
|
||||
requiredLocal := []string{"ssh", "rsync", "restic", "gzip"}
|
||||
for _, tool := range requiredLocal {
|
||||
a.log.Debug("preflight local tool check", zap.Int64("job_id", job.ID), zap.String("tool", tool))
|
||||
if _, err := exec.LookPath(tool); err != nil {
|
||||
failures++
|
||||
a.log.Debug("preflight local tool missing", zap.Int64("job_id", job.ID), zap.String("tool", tool), zap.Error(err))
|
||||
_ = a.store.AddJobEvent(ctx, store.JobEvent{JobID: job.ID, Level: "error", Message: fmt.Sprintf("local tool missing: %s", tool)})
|
||||
}
|
||||
}
|
||||
|
|
@ -98,13 +101,17 @@ func (a *app) runPreflightJob(ctx context.Context, job store.Job, site store.Sit
|
|||
return "failed", fmt.Sprintf("preflight failed: %d local tool checks failed", failures)
|
||||
}
|
||||
|
||||
a.log.Debug("preflight ssh connectivity check", zap.Int64("job_id", job.ID), zap.Int64("site_id", site.ID))
|
||||
if err := sshCheck(ctx, site, "echo preflight-ok"); err != nil {
|
||||
_ = a.store.AddJobEvent(ctx, store.JobEvent{JobID: job.ID, Level: "error", Message: "ssh connectivity failed: " + err.Error()})
|
||||
a.log.Debug("preflight ssh connectivity failed", zap.Int64("job_id", job.ID), zap.Int64("site_id", site.ID), zap.Error(err))
|
||||
return "failed", "preflight failed: ssh connectivity"
|
||||
}
|
||||
a.log.Debug("preflight ssh connectivity ok", zap.Int64("job_id", job.ID), zap.Int64("site_id", site.ID))
|
||||
_ = a.store.AddJobEvent(ctx, store.JobEvent{JobID: job.ID, Level: "info", Message: "ssh connectivity ok"})
|
||||
|
||||
for _, t := range site.Targets {
|
||||
a.log.Debug("preflight target check begin", zap.Int64("job_id", job.ID), zap.Int64("site_id", site.ID), zap.String("target_path", t.Path), zap.String("target_mode", t.Mode))
|
||||
var err error
|
||||
switch t.Mode {
|
||||
case "directory":
|
||||
|
|
@ -119,8 +126,10 @@ func (a *app) runPreflightJob(ctx context.Context, job store.Job, site store.Sit
|
|||
warnings++
|
||||
msg := fmt.Sprintf("target %s (%s): %s", t.Path, t.Mode, err.Error())
|
||||
_ = a.store.AddJobEvent(ctx, store.JobEvent{JobID: job.ID, Level: "warn", Message: msg})
|
||||
a.log.Debug("preflight target check failed", zap.Int64("job_id", job.ID), zap.Int64("site_id", site.ID), zap.String("target_path", t.Path), zap.String("target_mode", t.Mode), zap.Error(err))
|
||||
} else {
|
||||
_ = a.store.AddJobEvent(ctx, store.JobEvent{JobID: job.ID, Level: "info", Message: fmt.Sprintf("target ok: %s (%s)", t.Path, t.Mode)})
|
||||
a.log.Debug("preflight target check ok", zap.Int64("job_id", job.ID), zap.Int64("site_id", site.ID), zap.String("target_path", t.Path), zap.String("target_mode", t.Mode))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ func main() {
|
|||
logger.Fatal("failed to open store", zap.Error(err), zap.String("db_path", dbPath))
|
||||
}
|
||||
defer st.Close()
|
||||
st.SetLogger(logger)
|
||||
|
||||
a := &app{store: st, log: logger}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
|
|
|||
|
|
@ -95,22 +95,26 @@ func (a *app) scanSiteNow(ctx context.Context, siteID int64) {
|
|||
a.log.Warn("scan site load failed", zap.Int64("site_id", siteID), zap.Error(err))
|
||||
return
|
||||
}
|
||||
a.log.Debug("scan site begin", zap.Int64("site_id", siteID), zap.Int("targets", len(site.Targets)))
|
||||
|
||||
scannedAt := time.Now()
|
||||
success := 0
|
||||
failures := 0
|
||||
updated := make([]store.SiteTarget, 0, len(site.Targets))
|
||||
for _, target := range site.Targets {
|
||||
a.log.Debug("scan target begin", zap.Int64("site_id", site.ID), zap.String("target_path", target.Path), zap.String("target_mode", target.Mode))
|
||||
size, outErr := queryTargetSize(ctx, site, target)
|
||||
target.LastScanAt = sql.NullTime{Time: scannedAt, Valid: true}
|
||||
if outErr != nil {
|
||||
failures++
|
||||
target.LastSizeByte = sql.NullInt64{}
|
||||
target.LastError = sql.NullString{String: outErr.Error(), Valid: true}
|
||||
a.log.Debug("scan target failed", zap.Int64("site_id", site.ID), zap.String("target_path", target.Path), zap.String("target_mode", target.Mode), zap.Error(outErr))
|
||||
} else {
|
||||
success++
|
||||
target.LastSizeByte = sql.NullInt64{Int64: size, Valid: true}
|
||||
target.LastError = sql.NullString{}
|
||||
a.log.Debug("scan target ok", zap.Int64("site_id", site.ID), zap.String("target_path", target.Path), zap.String("target_mode", target.Mode), zap.Int64("size_bytes", size))
|
||||
}
|
||||
updated = append(updated, target)
|
||||
}
|
||||
|
|
@ -125,6 +129,7 @@ func (a *app) scanSiteNow(ctx context.Context, siteID int64) {
|
|||
state = "partial"
|
||||
}
|
||||
notes := fmt.Sprintf("%d/%d targets scanned", success, len(site.Targets))
|
||||
a.log.Debug("scan site complete", zap.Int64("site_id", site.ID), zap.String("state", state), zap.String("notes", notes))
|
||||
if err := a.store.UpdateSiteScanResult(ctx, site.ID, state, notes, scannedAt, updated); err != nil {
|
||||
a.log.Warn("scan site update failed", zap.Int64("site_id", siteID), zap.Error(err))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue