lockfiles

This commit is contained in:
Peter Kleiweg
2026-03-19 14:42:35 +01:00
parent 025c134c07
commit 6d15441a5a
11 changed files with 221 additions and 1 deletions

View File

@@ -12,6 +12,7 @@ import (
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
@@ -71,6 +72,12 @@ func main() {
}
}()
myLock := "/net/corpora/nlnieuws/NU/lock"
mkLock(myLock)
defer func() {
_ = os.Remove(myLock)
}()
resp, err := http.Get("https://www.nu.nl/rss")
p(err)
body, err := io.ReadAll(resp.Body)
@@ -253,3 +260,16 @@ func doArticle(filename string, url string, timestamp time.Time, needUpdate bool
func fixSpace(s string) string {
return strings.Join(strings.Fields(s), " ")
}
func mkLock(filename string) {
pid := os.Getpid()
link := fmt.Sprintf("%s.%d", filepath.Base(filename), pid)
p(os.Symlink(link, filename))
name, err := os.Readlink(filename)
p(err)
if name != link {
p(fmt.Errorf("wrong lock name %q, should be %q", name, link))
}
}