Author: Kevin Schoon [kevinschoon@gmail.com]
Committer: GitHub [noreply@github.com] Sun, 06 Sep 2020 23:01:15 +0000
Hash: f2d98027e518b6d6c8fa7f2e3a8c71ace93e8bf3
Timestamp: Sun, 06 Sep 2020 23:01:15 +0000 (4 years ago)

+14 -0 +/-1 browse
Merge pull request #29 from strogiyotec/sock_file_fix
Merge pull request #29 from strogiyotec/sock_file_fix

delete socket file after accidental crash
1diff --git a/server.go b/server.go
2index c57ed82..87c5c7e 100644
3--- a/server.go
4+++ b/server.go
5 @@ -2,7 +2,10 @@ package main
6
7 import (
8 "encoding/json"
9+ "errors"
10+ "fmt"
11 "net"
12+ "os"
13 )
14
15 // Server listens on a Unix domain socket
16 @@ -39,6 +42,17 @@ func (s *Server) Stop() {
17 }
18
19 func NewServer(runner *TaskRunner, config *Config) (*Server, error) {
20+ //check if socket file exists
21+ if _, err := os.Stat(config.SocketPath); err == nil {
22+ _, err := net.Dial("unix", config.SocketPath)
23+ //if error then sock file was saved after crash
24+ if err != nil {
25+ os.Remove(config.SocketPath)
26+ } else {
27+ // another instance of pomo is running
28+ return nil, errors.New(fmt.Sprintf("Socket %s is already in use", config.SocketPath))
29+ }
30+ }
31 listener, err := net.Listen("unix", config.SocketPath)
32 if err != nil {
33 return nil, err