Archived
1
0
Fork 0
This repository has been archived on 2024-12-18. You can view files and clone it, but cannot push or open issues or pull requests.
restockbot/pid.go
Julien Riou 3a4aba93e5
Release 0.2.0
- new language: go
- new shops: cybertek.fr, mediamarkt.ch
- deprecated shops: alternate.be, minershop.eu
- improved database transaction management
- better web parsing library (ferret, requires headless chrome browser)
- include or exclude products by applying regex on their names
- check for PID file to avoid running the bot twice
- hastags are now configurable

Signed-off-by: Julien Riou <julien@riou.xyz>
2021-02-27 08:10:43 +01:00

74 lines
1.6 KiB
Go

package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
log "github.com/sirupsen/logrus"
)
// writePid writes current process ID into a file
func writePid(file string) error {
if file == "" {
return nil
}
log.Debugf("writing PID to %s", file)
pid := strconv.Itoa(os.Getpid())
f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(pid)
return err
}
// removePid removes file containing the process ID
func removePid(file string) error {
if file == "" {
return nil
}
if _, err := os.Stat(file); err == nil {
log.Debugf("removing PID file %s", file)
return os.Remove(file)
}
return nil
}
// waitPid check if another instance is running and eventually wait for it to terminate (with a timeout)
func waitPid(pidFile string, waitTimeout int) error {
if pidFile == "" {
return nil
}
waitTime := 100 * time.Millisecond // initial backoff time for waiting
maxBackoffTime := 1000 // maximum time in millisecond to add between each waiting runs
timeout := false
maxTime := time.Now().Add(time.Second * time.Duration(waitTimeout))
for {
if _, err := os.Stat(pidFile); err == nil {
// PID file exists
log.Debugf("waiting %s before checking PID again", waitTime)
time.Sleep(waitTime)
waitTime = waitTime + time.Duration(rand.Intn(maxBackoffTime))*time.Millisecond
} else {
// file does not exist
break
}
if time.Now().After(maxTime) {
timeout = true
break
}
}
if timeout {
return fmt.Errorf("timeout waiting for PID file")
}
return nil
}