Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coordinator support for systemd notifications #649

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions coordinator/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"
"sync"
"time"

"github.com/pg-sharding/spqr/pkg/spqrlog"
"github.com/pg-sharding/spqr/router/port"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/pg-sharding/spqr/coordinator/provider"
"github.com/pg-sharding/spqr/pkg/config"
protos "github.com/pg-sharding/spqr/pkg/protos"
sdnotifier "github.com/pg-sharding/spqr/router/sdnotifier"

"golang.org/x/sync/semaphore"
)
Expand All @@ -43,6 +45,20 @@ func (app *App) Run(withPsql bool) error {

app.coordinator.RunCoordinator(context.TODO(), !withPsql)

var notifier *sdnotifier.Notifier
if config.CoordinatorConfig().UseSystemdNotifier {
// systemd notifier
var err error
notifier, err = sdnotifier.NewNotifier(os.Getenv("NOTIFY_SOCKET"), config.CoordinatorConfig().SystemdNotifierDebug)
if err != nil {
return err
}

if err := notifier.Ready(); err != nil {
return fmt.Errorf("could not send ready msg: %s", err)
}
}

wg := &sync.WaitGroup{}

wg.Add(1)
Expand All @@ -66,6 +82,17 @@ func (app *App) Run(withPsql bool) error {
}
}(wg)

if notifier != nil {
go func() {
for {
if err := notifier.Notify(); err != nil {
spqrlog.Zero.Error().Err(err).Msg("error sending systemd notification")
}
time.Sleep(sdnotifier.Timeout)
}
}()
}

wg.Wait()

spqrlog.Zero.Debug().Msg("exit coordinator app")
Expand Down
18 changes: 10 additions & 8 deletions pkg/config/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ import (
var cfgCoordinator Coordinator

type Coordinator struct {
LogLevel string `json:"log_level" toml:"log_level" yaml:"log_level"`
QdbAddr string `json:"qdb_addr" toml:"qdb_addr" yaml:"qdb_addr"`
CoordinatorPort string `json:"coordinator_port" toml:"coordinator_port" yaml:"coordinator_port"`
GrpcApiPort string `json:"grpc_api_port" toml:"grpc_api_port" yaml:"grpc_api_port"`
Host string `json:"host" toml:"host" yaml:"host"`
Auth *AuthCfg `json:"auth" toml:"auth" yaml:"auth"`
FrontendTLS *TLSConfig `json:"frontend_tls" yaml:"frontend_tls" toml:"frontend_tls"`
ShardDataCfg string `json:"shard_data" toml:"shard_data" yaml:"shard_data"`
LogLevel string `json:"log_level" toml:"log_level" yaml:"log_level"`
QdbAddr string `json:"qdb_addr" toml:"qdb_addr" yaml:"qdb_addr"`
CoordinatorPort string `json:"coordinator_port" toml:"coordinator_port" yaml:"coordinator_port"`
GrpcApiPort string `json:"grpc_api_port" toml:"grpc_api_port" yaml:"grpc_api_port"`
Host string `json:"host" toml:"host" yaml:"host"`
Auth *AuthCfg `json:"auth" toml:"auth" yaml:"auth"`
FrontendTLS *TLSConfig `json:"frontend_tls" yaml:"frontend_tls" toml:"frontend_tls"`
ShardDataCfg string `json:"shard_data" toml:"shard_data" yaml:"shard_data"`
UseSystemdNotifier bool `json:"use_systemd_notifier" toml:"use_systemd_notifier" yaml:"use_systemd_notifier"`
SystemdNotifierDebug bool `json:"systemd_notifier_debug" toml:"systemd_notifier_debug" yaml:"systemd_notifier_debug"`
}

func LoadCoordinatorCfg(cfgPath string) error {
Expand Down
Loading