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