mirror of
https://github.com/hibiken/asynq.git
synced 2026-02-04 15:50:07 +00:00
ci: format code with golangci-lint
This commit is contained in:
@@ -11,11 +11,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/google/go-cmp/cmp/cmpopts"
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
"github.com/hibiken/asynq/internal/log"
|
"github.com/hibiken/asynq/internal/log"
|
||||||
h "github.com/hibiken/asynq/internal/testutil"
|
h "github.com/hibiken/asynq/internal/testutil"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
//============================================================================
|
//============================================================================
|
||||||
|
|||||||
81
doc.go
81
doc.go
@@ -8,41 +8,41 @@ Package asynq provides a framework for Redis based distrubted task queue.
|
|||||||
Asynq uses Redis as a message broker. To connect to redis,
|
Asynq uses Redis as a message broker. To connect to redis,
|
||||||
specify the connection using one of RedisConnOpt types.
|
specify the connection using one of RedisConnOpt types.
|
||||||
|
|
||||||
redisConnOpt = asynq.RedisClientOpt{
|
redisConnOpt = asynq.RedisClientOpt{
|
||||||
Addr: "127.0.0.1:6379",
|
Addr: "127.0.0.1:6379",
|
||||||
Password: "xxxxx",
|
Password: "xxxxx",
|
||||||
DB: 2,
|
DB: 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
The Client is used to enqueue a task.
|
The Client is used to enqueue a task.
|
||||||
|
|
||||||
|
client := asynq.NewClient(redisConnOpt)
|
||||||
|
|
||||||
client := asynq.NewClient(redisConnOpt)
|
// Task is created with two parameters: its type and payload.
|
||||||
|
// Payload data is simply an array of bytes. It can be encoded in JSON, Protocol Buffer, Gob, etc.
|
||||||
|
b, err := json.Marshal(ExamplePayload{UserID: 42})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
// Task is created with two parameters: its type and payload.
|
task := asynq.NewTask("example", b)
|
||||||
// Payload data is simply an array of bytes. It can be encoded in JSON, Protocol Buffer, Gob, etc.
|
|
||||||
b, err := json.Marshal(ExamplePayload{UserID: 42})
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
task := asynq.NewTask("example", b)
|
// Enqueue the task to be processed immediately.
|
||||||
|
info, err := client.Enqueue(task)
|
||||||
|
|
||||||
// Enqueue the task to be processed immediately.
|
// Schedule the task to be processed after one minute.
|
||||||
info, err := client.Enqueue(task)
|
info, err = client.Enqueue(t, asynq.ProcessIn(1*time.Minute))
|
||||||
|
|
||||||
// Schedule the task to be processed after one minute.
|
|
||||||
info, err = client.Enqueue(t, asynq.ProcessIn(1*time.Minute))
|
|
||||||
|
|
||||||
The Server is used to run the task processing workers with a given
|
The Server is used to run the task processing workers with a given
|
||||||
handler.
|
handler.
|
||||||
srv := asynq.NewServer(redisConnOpt, asynq.Config{
|
|
||||||
Concurrency: 10,
|
|
||||||
})
|
|
||||||
|
|
||||||
if err := srv.Run(handler); err != nil {
|
srv := asynq.NewServer(redisConnOpt, asynq.Config{
|
||||||
log.Fatal(err)
|
Concurrency: 10,
|
||||||
}
|
})
|
||||||
|
|
||||||
|
if err := srv.Run(handler); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
Handler is an interface type with a method which
|
Handler is an interface type with a method which
|
||||||
takes a task and returns an error. Handler should return nil if
|
takes a task and returns an error. Handler should return nil if
|
||||||
@@ -50,23 +50,24 @@ the processing is successful, otherwise return a non-nil error.
|
|||||||
If handler panics or returns a non-nil error, the task will be retried in the future.
|
If handler panics or returns a non-nil error, the task will be retried in the future.
|
||||||
|
|
||||||
Example of a type that implements the Handler interface.
|
Example of a type that implements the Handler interface.
|
||||||
type TaskHandler struct {
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *TaskHandler) ProcessTask(ctx context.Context, task *asynq.Task) error {
|
type TaskHandler struct {
|
||||||
switch task.Type {
|
// ...
|
||||||
case "example":
|
}
|
||||||
var data ExamplePayload
|
|
||||||
if err := json.Unmarshal(task.Payload(), &data); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// perform task with the data
|
|
||||||
|
|
||||||
default:
|
func (h *TaskHandler) ProcessTask(ctx context.Context, task *asynq.Task) error {
|
||||||
return fmt.Errorf("unexpected task type %q", task.Type)
|
switch task.Type {
|
||||||
}
|
case "example":
|
||||||
return nil
|
var data ExamplePayload
|
||||||
}
|
if err := json.Unmarshal(task.Payload(), &data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// perform task with the data
|
||||||
|
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unexpected task type %q", task.Type)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
*/
|
*/
|
||||||
package asynq
|
package asynq
|
||||||
|
|||||||
@@ -107,6 +107,7 @@ type Op string
|
|||||||
// only the last one is recorded.
|
// only the last one is recorded.
|
||||||
//
|
//
|
||||||
// The types are:
|
// The types are:
|
||||||
|
//
|
||||||
// errors.Op
|
// errors.Op
|
||||||
// The operation being performed, usually the method
|
// The operation being performed, usually the method
|
||||||
// being invoked (Get, Put, etc.).
|
// being invoked (Get, Put, etc.).
|
||||||
|
|||||||
@@ -156,5 +156,5 @@ func NotFound(ctx context.Context, task *Task) error {
|
|||||||
return fmt.Errorf("%w %q", ErrHandlerNotFound, task.Type())
|
return fmt.Errorf("%w %q", ErrHandlerNotFound, task.Type())
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotFoundHandler returns a simple task handler that returns a ``not found`` error.
|
// NotFoundHandler returns a simple task handler that returns a “not found“ error.
|
||||||
func NotFoundHandler() Handler { return HandlerFunc(NotFound) }
|
func NotFoundHandler() Handler { return HandlerFunc(NotFound) }
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ func makeFakeHandler(identity string) Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// makeFakeMiddleware returns a middleware function that appends the given identity
|
// makeFakeMiddleware returns a middleware function that appends the given identity
|
||||||
//to the global invoked slice.
|
// to the global invoked slice.
|
||||||
func makeFakeMiddleware(identity string) MiddlewareFunc {
|
func makeFakeMiddleware(identity string) MiddlewareFunc {
|
||||||
return func(next Handler) Handler {
|
return func(next Handler) Handler {
|
||||||
return HandlerFunc(func(ctx context.Context, t *Task) error {
|
return HandlerFunc(func(ctx context.Context, t *Task) error {
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
"github.com/hibiken/asynq/internal/base"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
"github.com/hibiken/asynq/internal/log"
|
"github.com/hibiken/asynq/internal/log"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
type subscriber struct {
|
type subscriber struct {
|
||||||
|
|||||||
@@ -16,11 +16,12 @@ import (
|
|||||||
// ScreenDrawer is used to draw contents on screen.
|
// ScreenDrawer is used to draw contents on screen.
|
||||||
//
|
//
|
||||||
// Usage example:
|
// Usage example:
|
||||||
// d := NewScreenDrawer(s)
|
//
|
||||||
// d.Println("Hello world", mystyle)
|
// d := NewScreenDrawer(s)
|
||||||
// d.NL() // adds newline
|
// d.Println("Hello world", mystyle)
|
||||||
// d.Print("foo", mystyle.Bold(true))
|
// d.NL() // adds newline
|
||||||
// d.Print("bar", mystyle.Italic(true))
|
// d.Print("foo", mystyle.Bold(true))
|
||||||
|
// d.Print("bar", mystyle.Italic(true))
|
||||||
type ScreenDrawer struct {
|
type ScreenDrawer struct {
|
||||||
l *LineDrawer
|
l *LineDrawer
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -414,18 +414,22 @@ func getTLSConfig() *tls.Config {
|
|||||||
// cols is a list of headers and printRow specifies how to print rows.
|
// cols is a list of headers and printRow specifies how to print rows.
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
// type User struct {
|
//
|
||||||
// Name string
|
// type User struct {
|
||||||
// Addr string
|
// Name string
|
||||||
// Age int
|
// Addr string
|
||||||
// }
|
// Age int
|
||||||
|
// }
|
||||||
|
//
|
||||||
// data := []*User{{"user1", "addr1", 24}, {"user2", "addr2", 42}, ...}
|
// data := []*User{{"user1", "addr1", 24}, {"user2", "addr2", 42}, ...}
|
||||||
// cols := []string{"Name", "Addr", "Age"}
|
// cols := []string{"Name", "Addr", "Age"}
|
||||||
// printRows := func(w io.Writer, tmpl string) {
|
//
|
||||||
// for _, u := range data {
|
// printRows := func(w io.Writer, tmpl string) {
|
||||||
// fmt.Fprintf(w, tmpl, u.Name, u.Addr, u.Age)
|
// for _, u := range data {
|
||||||
// }
|
// fmt.Fprintf(w, tmpl, u.Name, u.Addr, u.Age)
|
||||||
// }
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
// printTable(cols, printRows)
|
// printTable(cols, printRows)
|
||||||
func printTable(cols []string, printRows func(w io.Writer, tmpl string)) {
|
func printTable(cols []string, printRows func(w io.Writer, tmpl string)) {
|
||||||
format := strings.Repeat("%v\t", len(cols)) + "\n"
|
format := strings.Repeat("%v\t", len(cols)) + "\n"
|
||||||
|
|||||||
@@ -770,4 +770,3 @@ func taskRunAll(cmd *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
fmt.Printf("%d tasks are now pending\n", n)
|
fmt.Printf("%d tasks are now pending\n", n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user