This repository was archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.go
More file actions
executable file
·67 lines (57 loc) · 1.63 KB
/
sync.go
File metadata and controls
executable file
·67 lines (57 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"github.com/jasonlvhit/gocron"
"log"
"time"
)
var callSyncFlow = callSyncFlowRepeat
// callSyncFlowRepeat calls the Rsync workflow. Executes a dry run first for
// identifying changes. Then runs the actual file sync operations. Finally
// updates the db with changes. Gocron schedules repeating runs.
func callSyncFlowRepeat(ctx *context, repeat bool) error {
log.Print("Start of sync flow...")
var err error
// Check db
if err = ctx.db.Ping(); err != nil {
return handle("Failed to ping database. Aborting run.", err)
}
// Offset scheduling of next run so it'll only schedule after this one
// finishes.
gocron.Clear()
defer func() {
if !repeat {
return
}
gocron.Every(12).Hours().Do(callSyncFlowRepeat, ctx, true)
log.Print("Next run has been scheduled...")
<-gocron.Start()
}()
// Dry run analysis stage for identifying file changes.
toSync, err := dryRunStage(ctx)
if err != nil {
// If listing from NCBI fails, wait some time and try again. Gocron
// scheduling will still be in effect.
defer func() {
time.Sleep(5 * time.Minute)
callSyncFlowRepeat(ctx, false)
}()
return handle("Error in dry run stage.", err)
}
// File operation stage. Moving actual files around.
fileOperationStage(ctx, toSync)
log.Print("Finished processing changes.")
log.Print("End of sync flow...")
return err
}
// An fInfo represents file path name, modified time, and size in bytes.
type fInfo struct {
name string
modTime string
size int
}
// A syncResult represents lists of new, modified, and deleted files.
type syncResult struct {
newF []string
modified []string
deleted []string
}