I have a background task (cleanUp
) that deletes outdated files.
func main() {
// 4 procs/childs max
runtime.GOMAXPROCS(4)
// start a cleanup cron-job
go cleanUp()
app := fiber.New(fiber.Config{
Prefork: true,
})
app.Post("/", handleFileupload)
log.Fatal(app.Listen(":4000"))
}
func cleanUp() {
fmt.Println("Cleaning Up..")
for {
// deletes old files here
time.Sleep(60 * time.Second)
}
}
At the moment cleanUp
runs on all 4 processes and prints Cleaning Up..
4 times.
But I want it to run only on a single process and print a single Cleaning Up..
. I have tried waitGroups and channels but failed to achieve what I wanted.
How can I run it only on a single process to avoid race conditions?
Here is the output:
Cleaning Up..
┌───────────────────────────────────────────────────┐ ┌───────────────────────────────────────────────────┐
│ Fiber v2.38.1 │ │ Child PIDs ... 79378, 79379, 79380, 79381 │
│ http://127.0.0.1:4000 │ └───────────────────────────────────────────────────┘
│ (bound on host 0.0.0.0 and port 4000) │
│ │
│ Handlers ............. 5 Processes ........... 4 │
│ Prefork ........ Enabled PID ............. 79377 │
└───────────────────────────────────────────────────┘
Cleaning Up..
Cleaning Up..
Cleaning Up..
Cleaning Up..