2021-09-18 17:55:59 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-09-30 23:56:01 +05:30
|
|
|
"github.com/go-redis/redis/v8"
|
2021-09-18 19:53:42 +05:30
|
|
|
"github.com/rs/cors"
|
|
|
|
|
2021-09-18 17:55:59 +05:30
|
|
|
"github.com/hibiken/asynq"
|
2021-09-18 19:40:42 +05:30
|
|
|
"github.com/hibiken/asynqmon"
|
2021-09-18 17:55:59 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Command-line flags
|
|
|
|
var (
|
|
|
|
flagPort int
|
|
|
|
flagRedisAddr string
|
|
|
|
flagRedisDB int
|
|
|
|
flagRedisPassword string
|
|
|
|
flagRedisTLS string
|
|
|
|
flagRedisURL string
|
|
|
|
flagRedisInsecureTLS bool
|
|
|
|
flagRedisClusterNodes string
|
2021-10-21 07:11:46 -07:00
|
|
|
flagMaxPayloadLength int
|
2021-11-06 15:23:10 -07:00
|
|
|
flagMaxResultLength int
|
2021-09-18 17:55:59 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.IntVar(&flagPort, "port", 8080, "port number to use for web ui server")
|
|
|
|
flag.StringVar(&flagRedisAddr, "redis-addr", "127.0.0.1:6379", "address of redis server to connect to")
|
|
|
|
flag.IntVar(&flagRedisDB, "redis-db", 0, "redis database number")
|
|
|
|
flag.StringVar(&flagRedisPassword, "redis-password", "", "password to use when connecting to redis server")
|
|
|
|
flag.StringVar(&flagRedisTLS, "redis-tls", "", "server name for TLS validation used when connecting to redis server")
|
|
|
|
flag.StringVar(&flagRedisURL, "redis-url", "", "URL to redis server")
|
|
|
|
flag.BoolVar(&flagRedisInsecureTLS, "redis-insecure-tls", false, "disable TLS certificate host checks")
|
|
|
|
flag.StringVar(&flagRedisClusterNodes, "redis-cluster-nodes", "", "comma separated list of host:port addresses of cluster nodes")
|
2021-10-21 07:11:46 -07:00
|
|
|
flag.IntVar(&flagMaxPayloadLength, "max-payload-length", 200, "maximum number of utf8 characters printed in the payload cell in the Web UI")
|
2021-11-06 15:23:10 -07:00
|
|
|
flag.IntVar(&flagMaxResultLength, "max-result-length", 200, "maximum number of utf8 characters printed in the result cell in the Web UI")
|
2021-09-18 17:55:59 +05:30
|
|
|
}
|
|
|
|
|
2021-10-10 06:33:38 -07:00
|
|
|
// TODO: Write test and refactor this code.
|
|
|
|
// IDEA: https://eli.thegreenplace.net/2020/testing-flag-parsing-in-go-programs/
|
|
|
|
func getRedisOptionsFromFlags() (asynq.RedisConnOpt, error) {
|
2021-09-18 17:55:59 +05:30
|
|
|
var opts redis.UniversalOptions
|
|
|
|
|
|
|
|
if flagRedisClusterNodes != "" {
|
|
|
|
opts.Addrs = strings.Split(flagRedisClusterNodes, ",")
|
|
|
|
opts.Password = flagRedisPassword
|
|
|
|
} else {
|
|
|
|
if flagRedisURL != "" {
|
|
|
|
res, err := redis.ParseURL(flagRedisURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opts.Addrs = append(opts.Addrs, res.Addr)
|
|
|
|
opts.DB = res.DB
|
|
|
|
opts.Password = res.Password
|
|
|
|
|
|
|
|
} else {
|
|
|
|
opts.Addrs = []string{flagRedisAddr}
|
|
|
|
opts.DB = flagRedisDB
|
|
|
|
opts.Password = flagRedisPassword
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if flagRedisTLS != "" {
|
|
|
|
opts.TLSConfig = &tls.Config{ServerName: flagRedisTLS}
|
|
|
|
}
|
|
|
|
if flagRedisInsecureTLS {
|
|
|
|
if opts.TLSConfig == nil {
|
|
|
|
opts.TLSConfig = &tls.Config{}
|
|
|
|
}
|
|
|
|
opts.TLSConfig.InsecureSkipVerify = true
|
|
|
|
}
|
|
|
|
|
2021-10-10 06:33:38 -07:00
|
|
|
if flagRedisClusterNodes != "" {
|
|
|
|
return asynq.RedisClusterClientOpt{
|
|
|
|
Addrs: opts.Addrs,
|
|
|
|
Password: opts.Password,
|
|
|
|
TLSConfig: opts.TLSConfig,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return asynq.RedisClientOpt{
|
|
|
|
Addr: opts.Addrs[0],
|
|
|
|
DB: opts.DB,
|
|
|
|
Password: opts.Password,
|
|
|
|
TLSConfig: opts.TLSConfig,
|
|
|
|
}, nil
|
2021-09-18 17:55:59 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2021-10-10 06:33:38 -07:00
|
|
|
redisConnOpt, err := getRedisOptionsFromFlags()
|
2021-09-18 17:55:59 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-10-04 20:48:00 +05:30
|
|
|
h := asynqmon.New(asynqmon.Options{
|
2021-10-21 07:11:46 -07:00
|
|
|
RedisConnOpt: redisConnOpt,
|
|
|
|
PayloadFormatter: asynqmon.PayloadFormatterFunc(formatPayload),
|
2021-11-06 15:23:10 -07:00
|
|
|
ResultFormatter: asynqmon.ResultFormatterFunc(formatResult),
|
2021-09-18 17:55:59 +05:30
|
|
|
})
|
2021-10-04 20:48:00 +05:30
|
|
|
defer h.Close()
|
2021-09-18 17:55:59 +05:30
|
|
|
|
|
|
|
c := cors.New(cors.Options{
|
|
|
|
AllowedMethods: []string{"GET", "POST", "DELETE"},
|
|
|
|
})
|
|
|
|
|
|
|
|
srv := &http.Server{
|
2021-10-10 06:33:38 -07:00
|
|
|
Handler: c.Handler(h),
|
2021-09-18 17:55:59 +05:30
|
|
|
Addr: fmt.Sprintf(":%d", flagPort),
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Asynq Monitoring WebUI server is listening on port %d\n", flagPort)
|
|
|
|
log.Fatal(srv.ListenAndServe())
|
|
|
|
}
|
2021-10-21 07:11:46 -07:00
|
|
|
|
|
|
|
func formatPayload(taskType string, payload []byte) string {
|
|
|
|
payloadStr := asynqmon.DefaultPayloadFormatter.FormatPayload(taskType, payload)
|
|
|
|
return truncate(payloadStr, flagMaxPayloadLength)
|
|
|
|
}
|
|
|
|
|
2021-11-06 15:23:10 -07:00
|
|
|
func formatResult(taskType string, result []byte) string {
|
|
|
|
resultStr := asynqmon.DefaultResultFormatter.FormatResult(taskType, result)
|
|
|
|
return truncate(resultStr, flagMaxResultLength)
|
|
|
|
}
|
|
|
|
|
2021-10-21 07:11:46 -07:00
|
|
|
// truncates string s to limit length (in utf8).
|
|
|
|
func truncate(s string, limit int) string {
|
|
|
|
i := 0
|
|
|
|
for pos := range s {
|
|
|
|
if i == limit {
|
|
|
|
return s[:pos] + "…"
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|