mirror of
https://github.com/hibiken/asynq.git
synced 2024-11-13 04:46:39 +08:00
fix: run on Windows
This commit is contained in:
parent
2b632b93d5
commit
7e62f8a506
@ -2,6 +2,8 @@
|
||||
// Use of this source code is governed by a MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
//go:build linux || dragonfly || freebsd || netbsd || openbsd || darwin
|
||||
|
||||
package asynq_test
|
||||
|
||||
import (
|
||||
|
@ -7,6 +7,8 @@ package asynq
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
@ -58,27 +60,49 @@ func TestServerRun(t *testing.T) {
|
||||
ignoreOpt := goleak.IgnoreTopFunction("github.com/redis/go-redis/v9/internal/pool.(*ConnPool).reaper")
|
||||
defer goleak.VerifyNone(t, ignoreOpt)
|
||||
|
||||
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel})
|
||||
|
||||
signalWindows := make(chan struct{})
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
|
||||
mux := NewServeMux()
|
||||
srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{LogLevel: testLogLevel})
|
||||
if err := srv.Start(mux); err != nil {
|
||||
t.Error("start server:", err)
|
||||
}
|
||||
if runtime.GOOS == "windows" {
|
||||
<-signalWindows
|
||||
} else {
|
||||
srv.waitForSignals()
|
||||
}
|
||||
srv.Shutdown()
|
||||
}()
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
// Make sure server exits when receiving TERM signal.
|
||||
go func() {
|
||||
time.Sleep(2 * time.Second)
|
||||
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
|
||||
done <- struct{}{}
|
||||
}()
|
||||
if runtime.GOOS == "windows" {
|
||||
// It is not implemented to signal Interrupt on Windows
|
||||
signalWindows <- struct{}{}
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
select {
|
||||
case <-time.After(10 * time.Second):
|
||||
panic("server did not stop after receiving TERM signal")
|
||||
case <-done:
|
||||
p, err := os.FindProcess(os.Getpid())
|
||||
if err != nil {
|
||||
t.Error("find process:", err)
|
||||
return
|
||||
}
|
||||
err = p.Signal(syscall.SIGTERM)
|
||||
if err != nil {
|
||||
t.Error("signal:", err)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
mux := NewServeMux()
|
||||
if err := srv.Run(mux); err != nil {
|
||||
t.Fatal(err)
|
||||
select {
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Error("stop server: server did not stop after receiving TERM signal")
|
||||
case <-done:
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,8 +5,7 @@ package asynq
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// waitForSignals waits for signals and handles them.
|
||||
@ -14,16 +13,17 @@ import (
|
||||
// SIGTERM and SIGINT will signal the process to exit.
|
||||
//
|
||||
// Note: Currently SIGTSTP is not supported for windows build.
|
||||
// Note: Currently Ctrl-C on Windows signals syscall.SIGINT, not windows.SIGINT
|
||||
func (srv *Server) waitForSignals() {
|
||||
srv.logger.Info("Send signal TERM or INT to terminate the process")
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, windows.SIGTERM, windows.SIGINT)
|
||||
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
|
||||
<-sigs
|
||||
}
|
||||
|
||||
func (s *Scheduler) waitForSignals() {
|
||||
s.logger.Info("Send signal TERM or INT to stop the scheduler")
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, windows.SIGTERM, windows.SIGINT)
|
||||
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
|
||||
<-sigs
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user