Merge pull request #51 from amiel/publish-and-listen

Use a separate socket path to publish so pomo can publish and listen on different sockets
This commit is contained in:
Kevin Schoon 2021-11-02 07:46:52 -05:00 committed by GitHub
commit 7d4c4889c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 17 deletions

View File

@ -2,6 +2,7 @@ package pomo
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
@ -27,6 +28,8 @@ type Config struct {
// PublishJson pushes socket updates as a JSON // PublishJson pushes socket updates as a JSON
// encoded status message instead of string formatted // encoded status message instead of string formatted
PublishJson bool `json:"publishJson"` PublishJson bool `json:"publishJson"`
// If Publish is true, provide a socket path to publish to
PublishSocketPath string `json:"publishSocketPath"`
} }
type ColorMap struct { type ColorMap struct {
@ -116,5 +119,9 @@ func LoadConfig(configPath string, config *Config) error {
if config.IconPath == "" { if config.IconPath == "" {
config.IconPath = path.Join(config.BasePath, "/icon.png") config.IconPath = path.Join(config.BasePath, "/icon.png")
} }
if config.Publish && (config.PublishSocketPath == "" || config.PublishSocketPath == config.SocketPath) {
return fmt.Errorf("'publish' option now requires 'publishSocketPath' which must not be the same as 'socketPath'")
}
return nil return nil
} }

View File

@ -17,7 +17,7 @@ type Server struct {
running bool running bool
publish bool publish bool
publishJson bool publishJson bool
socketPath string publishSocketPath string
} }
func (s *Server) listen() { func (s *Server) listen() {
@ -38,7 +38,7 @@ func (s *Server) listen() {
func (s *Server) push() { func (s *Server) push() {
ticker := time.NewTicker(1 * time.Second) ticker := time.NewTicker(1 * time.Second)
for s.running { for s.running {
conn, err := net.Dial("unix", s.socketPath) conn, err := net.Dial("unix", s.publishSocketPath)
if err != nil { if err != nil {
<-ticker.C <-ticker.C
continue continue
@ -59,9 +59,9 @@ func (s *Server) Start() {
s.running = true s.running = true
if s.publish { if s.publish {
go s.push() go s.push()
} else {
go s.listen()
} }
go s.listen()
} }
func (s *Server) Stop() { func (s *Server) Stop() {
@ -72,13 +72,6 @@ func (s *Server) Stop() {
} }
func NewServer(runner *TaskRunner, config *Config) (*Server, error) { func NewServer(runner *TaskRunner, config *Config) (*Server, error) {
if config.Publish {
return &Server{
runner: runner,
publish: true,
publishJson: config.PublishJson,
socketPath: config.SocketPath}, nil
}
//check if socket file exists //check if socket file exists
if _, err := os.Stat(config.SocketPath); err == nil { if _, err := os.Stat(config.SocketPath); err == nil {
_, err := net.Dial("unix", config.SocketPath) _, err := net.Dial("unix", config.SocketPath)
@ -94,7 +87,16 @@ func NewServer(runner *TaskRunner, config *Config) (*Server, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Server{listener: listener, runner: runner}, nil
server := &Server{
listener: listener,
runner: runner,
publish: config.Publish,
publishJson: config.PublishJson,
publishSocketPath: config.PublishSocketPath,
}
return server, nil
} }
// Client makes requests to a listening // Client makes requests to a listening