Skip to content

Commit

Permalink
support sighup
Browse files Browse the repository at this point in the history
  • Loading branch information
linyows committed Feb 17, 2025
1 parent 7d29c7f commit 74af4e7
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions dewy.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,32 @@ func (d *Dewy) Start(i int) {
log.Printf("[ERROR] Scheduler failure: %#v", err)
}

d.notify.Send(ctx, fmt.Sprintf("Stop receiving \"%s\" signal", d.waitSigs()))
d.waitSigs(ctx)
}

func (d *Dewy) waitSigs() os.Signal {
func (d *Dewy) waitSigs(ctx context.Context) {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
sigReceived := <-sigCh
log.Printf("[DEBUG] PID %d received signal as %s", os.Getpid(), sigReceived)
d.job.Quit <- true
return sigReceived
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

for sig := range sigCh {
log.Printf("[DEBUG] PID %d received signal as %s", os.Getpid(), sig)
switch sig {
case syscall.SIGHUP:
d.notify.Send(ctx, fmt.Sprintf("Restart receiving by \"%s\" signal", sig))
continue

case syscall.SIGUSR1:
if err := d.restartServer(); err != nil {
log.Printf("[ERROR] Restart failure: %#v", err)
}
continue

case syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
d.job.Quit <- true
d.notify.Send(ctx, fmt.Sprintf("Stop receiving by \"%s\" signal", sig))
return
}
}
}

// cachekeyName is "tag--artifact"
Expand Down Expand Up @@ -292,12 +308,13 @@ func (d *Dewy) restartServer() error {
d.Lock()
defer d.Unlock()

p, _ := os.FindProcess(os.Getpid())
pid := os.Getpid()
p, _ := os.FindProcess(pid)
err := p.Signal(syscall.SIGHUP)
if err != nil {
return err
}
log.Print("[INFO] Send SIGHUP for server restart")
log.Printf("[INFO] Send SIGHUP to PID:%d for server restart", pid)

return nil
}
Expand Down

0 comments on commit 74af4e7

Please sign in to comment.