Skip to content
Snippets Groups Projects
Commit e962cfbc authored by Ophestra's avatar Ophestra
Browse files

lock store after opening, implement windows-specific restart function

parent e774140e
No related branches found
No related tags found
No related merge requests found
package main
import (
"context"
log "github.com/sirupsen/logrus"
"time"
)
func cleanup(restart bool) {
var err error
// Set restart
d = true
r = restart
// Close store
instance.Close()
// Shutdown web server
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err = server.Shutdown(ctx)
if err != nil {
log.Errorf("Error while shutting down web server, %s", err)
}
}
// +build !windows
package main
import (
"context"
log "github.com/sirupsen/logrus"
"os"
"syscall"
"time"
)
func cleanup(restart bool) {
var err error
// Set restart
d = true
r = restart
// Shutdown web server
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err = server.Shutdown(ctx)
if err != nil {
log.Errorf("Error while shutting down web server, %s", err)
}
}
func restart() {
var err error
......
package main
import (
log "github.com/sirupsen/logrus"
"os"
)
func restart() {
if _, err := os.Stat(executable); err != nil {
log.Fatalf("Unable to get executable path, %s", err)
os.Exit(1)
}
log.Infof("Program found at %s.", executable)
wd, err := os.Getwd()
if err != nil {
log.Fatalf("Unable to get working directory, %s", err)
os.Exit(1)
}
log.Infof("Current working directory is %s.", wd)
_, err = os.StartProcess(executable, []string{}, &os.ProcAttr{
Dir: wd,
Env: nil,
Files: []*os.File{os.Stderr, os.Stdin, os.Stdout},
Sys: nil,
})
if err != nil {
log.Fatalf("Unable to create new process, %s", err)
os.Exit(1)
}
}
......@@ -4,6 +4,11 @@ import (
"strconv"
)
// LockPath returns path to lock file.
func (s *Store) LockPath() string {
return s.Path + "/lock"
}
// TagsDir returns path to tags.
func (s *Store) TagsDir() string {
return s.Path + "/tags"
......
......@@ -146,9 +146,31 @@ func NewStore(path string, single bool) *Store {
mutex: make(map[string]*sync.RWMutex),
}
}
if store.file(store.LockPath()) {
if pid, err := os.ReadFile(store.LockPath()); err != nil {
log.Fatalf("Store locked, lock file unreadable, %s", err)
log.Exit(1)
} else {
log.Fatalf("Store locked by process %s.", string(pid))
log.Exit(1)
}
}
if err := os.WriteFile(store.LockPath(), []byte(strconv.Itoa(os.Getpid())), store.PermissionFile); err != nil {
log.Fatalf("Error locking store, %s", err)
log.Exit(1)
}
return store
}
// Close closes the store.
func (s *Store) Close() {
if err := os.Remove(s.LockPath()); err != nil {
log.Fatalf("Error unlocking store, %s", err)
log.Exit(1)
}
log.Info("Store closed.")
}
// create sets up the store directory if it does not exist.
func (s *Store) create() error {
// Check if exists
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment