Skip to content

Instantly share code, notes, and snippets.

@aLucaz
Created May 7, 2023 15:11
Show Gist options
  • Save aLucaz/90b1873fa98aa4cddffb0fff46274cb1 to your computer and use it in GitHub Desktop.
Save aLucaz/90b1873fa98aa4cddffb0fff46274cb1 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"time"
)
const folderPath = "/path/to/folder"
const logPath = "/path/to/log/file.log"
func main() {
// create log file if not exists
_, err := os.Stat(logPath)
if os.IsNotExist(err) {
_, err := os.Create(logPath)
if err != nil {
log.Fatalf("Failed to create log file: %v", err)
}
}
// start file system monitor
go monitorFileSystem()
// start cron job
go processFiles()
// keep main thread running
select {}
}
func monitorFileSystem() {
for {
err := filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
// log path of added file
log.Printf("Added file: %s\n", path)
// append path to log file
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("Failed to open log file: %v", err)
}
defer f.Close()
_, err = fmt.Fprintln(f, path)
if err != nil {
log.Fatalf("Failed to write to log file: %v", err)
}
}
return nil
})
if err != nil {
log.Fatalf("Failed to monitor file system: %v", err)
}
time.Sleep(time.Second)
}
}
func processFiles() {
// wait until midnight
now := time.Now()
nextMidnight := now.Truncate(24 * time.Hour).AddDate(0, 0, 1)
time.Sleep(nextMidnight.Sub(now))
// read log file and process each file
f, err := os.Open(logPath)
if err != nil {
log.Fatalf("Failed to open log file: %v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
path := scanner.Text()
// process file in a goroutine
go func(path string) {
// TODO: process file
}(path)
}
if err := scanner.Err(); err != nil {
log.Fatalf("Failed to read log file: %v", err)
}
// wait for next midnight and repeat
for {
now := time.Now()
nextMidnight := now.Truncate(24 * time.Hour).AddDate(0, 0, 1)
time.Sleep(nextMidnight.Sub(now))
// read log file and process each file
f, err := os.Open(logPath)
if err != nil {
log.Fatalf("Failed to open log file: %v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
path := scanner.Text()
// process file in a goroutine
go func(path string) {
// TODO: process file
}(path)
}
if err := scanner.Err(); err != nil {
log.Fatalf("Failed to read log file: %v", err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment