Skip to content

Commit

Permalink
systemd: change process manager
Browse files Browse the repository at this point in the history
  • Loading branch information
et-nik committed Mar 1, 2024
1 parent 15850b1 commit b056ce3
Showing 1 changed file with 37 additions and 28 deletions.
65 changes: 37 additions & 28 deletions internal/processmanager/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,36 @@ func (pm *SystemD) Install(
}

func (pm *SystemD) Uninstall(
_ context.Context, _ *domain.Server, _ io.Writer,
ctx context.Context, server *domain.Server, _ io.Writer,
) (domain.Result, error) {
// Nothing to do here
err := os.Remove(pm.socketFile(server))
if err != nil {
logger.WithError(ctx, err).Warn("failed to remove socket file")
}

err = os.Remove(pm.serviceFile(server))
if err != nil {
logger.WithError(ctx, err).Warn("failed to remove service file")
}

err = pm.daemonReload(ctx)
if err != nil {
logger.Logger(ctx).WithError(err).Warn("Failed to daemon-reload")
}

return domain.SuccessResult, nil
}

func (pm *SystemD) Start(ctx context.Context, server *domain.Server, out io.Writer) (domain.Result, error) {
f, err := os.Create(pm.logFile(server))
logFile := pm.logFile(server)
if _, err := os.Stat(logFile); errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(filepath.Dir(logFile), 0755)
if err != nil {
return domain.ErrorResult, errors.WithMessage(err, "failed to create directory")
}
}

f, err := os.Create(logFile)
if err != nil {
return domain.ErrorResult, errors.WithMessage(err, "failed to create file")
}
Expand Down Expand Up @@ -104,21 +126,6 @@ func (pm *SystemD) Stop(ctx context.Context, server *domain.Server, out io.Write
return domain.ErrorResult, errors.WithMessage(err, "failed to exec command")
}

err = os.Remove(pm.socketFile(server))
if err != nil {
logger.WithError(ctx, err).Warn("failed to remove socket file")
}

err = os.Remove(pm.serviceFile(server))
if err != nil {
logger.WithError(ctx, err).Warn("failed to remove service file")
}

err = pm.daemonReload(ctx)
if err != nil {
logger.Logger(ctx).WithError(err).Warn("Failed to daemon-reload")
}

return domain.Result(result), nil
}

Expand Down Expand Up @@ -146,16 +153,18 @@ func (pm *SystemD) command(
return domain.ErrorResult, errors.WithMessage(err, "failed to daemon-reload")
}

_, err = pm.executor.ExecWithWriter(
ctx,
fmt.Sprintf("systemctl %s %s", command, pm.socketName(server)),
out,
contracts.ExecutorOptions{
WorkDir: pm.cfg.WorkDir(),
},
)
if err != nil {
return domain.ErrorResult, errors.WithMessage(err, "failed to exec command")
if command != "restart" {
_, err = pm.executor.ExecWithWriter(
ctx,
fmt.Sprintf("systemctl %s %s", command, pm.socketName(server)),
out,
contracts.ExecutorOptions{
WorkDir: pm.cfg.WorkDir(),
},
)
if err != nil {
return domain.ErrorResult, errors.WithMessage(err, "failed to exec command")
}
}

result, err := pm.executor.ExecWithWriter(
Expand Down

0 comments on commit b056ce3

Please sign in to comment.