Skip to content

Commit

Permalink
Add flag to modify the timestamp path.
Browse files Browse the repository at this point in the history
This is necessary when running the binary outside of the working
directory, e.g., via systemctl.
  • Loading branch information
LTLA committed Oct 22, 2024
1 parent 416d34c commit 5939d95
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ These intervals can be modified with the `-log` and `-full` flags, respectively.
After every log scan, **sayoko** produces a `.sayoko_last_scan` file containing the RFC3339-formatted time of the most recent log.
This avoids redundant re-processing of the same log files when **sayoko** itself is restarted.
Advanced users can exploit this by modifying the timestamp in this file to force **sayoko** to process logs after a desired timepoint.
The path of this timestamp file can also be modified via the `-timestamp` flag.

## Developer notes

Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
"errors"
)

const last_scan_path = ".sayoko_last_scan"

func retrieveLastScanTime() time.Time {
func retrieveLastScanTime(last_scan_path string) time.Time {
last_scan_raw, err := os.ReadFile(last_scan_path)
if err == nil {
candidate, err := time.Parse(time.RFC3339, string(last_scan_raw))
Expand All @@ -29,7 +27,7 @@ func retrieveLastScanTime() time.Time {
return time.Now()
}

func depositLastScanTime(last_scan time.Time) {
func depositLastScanTime(last_scan time.Time, last_scan_path string) {
err := os.WriteFile(last_scan_path, []byte(last_scan.Format(time.RFC3339)), 0644)
if err != nil {
log.Printf("failed to write the last scan time; %v", err)
Expand All @@ -46,6 +44,7 @@ func main() {
surl := flag.String("url", "", "URL of the SewerRat instance")
log_time := flag.Int("log", 10, "Interval in which to check for new logs, in minutes")
full_time := flag.Int("full", 24, "Interval in which to do a full check, in hours")
tpath := flag.String("timestamp", ".sayoko_last_scan", "Path to the last scan timestamp")
flag.Parse()

registry := *gpath
Expand All @@ -68,7 +67,8 @@ func main() {

// Timer to inspect logs.
go func() {
last_scan := retrieveLastScanTime()
last_scan_path := *tpath
last_scan := retrieveLastScanTime(last_scan_path)
timer := time.NewTicker(time.Minute * time.Duration(*log_time))
for {
<-timer.C
Expand All @@ -79,7 +79,7 @@ func main() {
log.Printf("detected failures for log check; %v", err)
}
last_scan = new_last_scan // this can be set regardless of 'err'.
depositLastScanTime(last_scan)
depositLastScanTime(last_scan, last_scan_path)
ch_reignore <- true
}
}()
Expand Down
5 changes: 3 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (

func TestLastScanTime(t *testing.T) {
last_scan := time.Now()
depositLastScanTime(last_scan)
retrieved := retrieveLastScanTime()
const last_scan_path = ".sayoko_last_scan"
depositLastScanTime(last_scan, last_scan_path)
retrieved := retrieveLastScanTime(last_scan_path)
if last_scan.Sub(retrieved).Abs() > time.Second { // needs some tolerance due to rounding of the stringified time.
t.Fatalf("incorrect time value after a roundtrip (%v vs %v)", last_scan, retrieved)
}
Expand Down

0 comments on commit 5939d95

Please sign in to comment.