2020-04-20 02:31:12 +08:00
|
|
|
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package asynq_test
|
|
|
|
|
|
|
|
import (
|
2020-03-26 22:31:08 +08:00
|
|
|
"fmt"
|
2020-04-20 02:31:12 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-10-12 21:47:43 +08:00
|
|
|
"time"
|
2020-04-20 02:31:12 +08:00
|
|
|
|
|
|
|
"github.com/hibiken/asynq"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleServer_Run() {
|
|
|
|
srv := asynq.NewServer(
|
|
|
|
asynq.RedisClientOpt{Addr: ":6379"},
|
|
|
|
asynq.Config{Concurrency: 20},
|
|
|
|
)
|
|
|
|
|
|
|
|
h := asynq.NewServeMux()
|
|
|
|
// ... Register handlers
|
|
|
|
|
|
|
|
// Run blocks and waits for os signal to terminate the program.
|
|
|
|
if err := srv.Run(h); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 21:20:54 +08:00
|
|
|
func ExampleServer_Shutdown() {
|
2020-04-20 02:31:12 +08:00
|
|
|
srv := asynq.NewServer(
|
|
|
|
asynq.RedisClientOpt{Addr: ":6379"},
|
|
|
|
asynq.Config{Concurrency: 20},
|
|
|
|
)
|
|
|
|
|
|
|
|
h := asynq.NewServeMux()
|
|
|
|
// ... Register handlers
|
|
|
|
|
|
|
|
if err := srv.Start(h); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, unix.SIGTERM, unix.SIGINT)
|
|
|
|
<-sigs // wait for termination signal
|
|
|
|
|
2021-03-23 21:20:54 +08:00
|
|
|
srv.Shutdown()
|
2020-04-20 02:31:12 +08:00
|
|
|
}
|
|
|
|
|
2021-03-23 21:20:54 +08:00
|
|
|
func ExampleServer_Stop() {
|
2020-04-20 02:31:12 +08:00
|
|
|
srv := asynq.NewServer(
|
|
|
|
asynq.RedisClientOpt{Addr: ":6379"},
|
|
|
|
asynq.Config{Concurrency: 20},
|
|
|
|
)
|
|
|
|
|
|
|
|
h := asynq.NewServeMux()
|
|
|
|
// ... Register handlers
|
|
|
|
|
|
|
|
if err := srv.Start(h); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, unix.SIGTERM, unix.SIGINT, unix.SIGTSTP)
|
|
|
|
// Handle SIGTERM, SIGINT to exit the program.
|
|
|
|
// Handle SIGTSTP to stop processing new tasks.
|
|
|
|
for {
|
|
|
|
s := <-sigs
|
|
|
|
if s == unix.SIGTSTP {
|
2021-03-23 21:20:54 +08:00
|
|
|
srv.Stop() // stop processing new tasks
|
2020-04-20 02:31:12 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-03-23 21:20:54 +08:00
|
|
|
break // received SIGTERM or SIGINT signal
|
2020-04-20 02:31:12 +08:00
|
|
|
}
|
|
|
|
|
2021-03-23 21:20:54 +08:00
|
|
|
srv.Shutdown()
|
2020-04-20 02:31:12 +08:00
|
|
|
}
|
2020-03-26 22:31:08 +08:00
|
|
|
|
2020-10-12 21:47:43 +08:00
|
|
|
func ExampleScheduler() {
|
|
|
|
scheduler := asynq.NewScheduler(
|
|
|
|
asynq.RedisClientOpt{Addr: ":6379"},
|
|
|
|
&asynq.SchedulerOpts{Location: time.Local},
|
|
|
|
)
|
|
|
|
|
|
|
|
if _, err := scheduler.Register("* * * * *", asynq.NewTask("task1", nil)); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := scheduler.Register("@every 30s", asynq.NewTask("task2", nil)); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run blocks and waits for os signal to terminate the program.
|
|
|
|
if err := scheduler.Run(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:31:08 +08:00
|
|
|
func ExampleParseRedisURI() {
|
|
|
|
rconn, err := asynq.ParseRedisURI("redis://localhost:6379/10")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
r, ok := rconn.(asynq.RedisClientOpt)
|
|
|
|
if !ok {
|
|
|
|
log.Fatal("unexpected type")
|
|
|
|
}
|
|
|
|
fmt.Println(r.Addr)
|
|
|
|
fmt.Println(r.DB)
|
|
|
|
// Output:
|
|
|
|
// localhost:6379
|
|
|
|
// 10
|
|
|
|
}
|