Created
April 2, 2021 03:06
-
-
Save StdioA/5b35f6a4d0ee8538e66b728f6bf2e2a6 to your computer and use it in GitHub Desktop.
gocraft/work #181 POC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"errors" | |
"fmt" | |
"reflect" | |
"time" | |
"github.com/gocraft/work" | |
"github.com/gomodule/redigo/redis" | |
) | |
type Context struct { | |
Ctx context.Context | |
} | |
func main() { | |
const ( | |
namespace = "test" | |
jobName = "testjob" | |
) | |
pool := &redis.Pool{ | |
MaxActive: 5, | |
MaxIdle: 5, | |
Wait: true, | |
Dial: func() (redis.Conn, error) { | |
return redis.DialURL("redis://localhost:6379") | |
}, | |
} | |
eq := work.NewEnqueuer(namespace, pool) | |
_, _ = eq.EnqueueUnique(jobName, nil) | |
wp := work.NewWorkerPool(Context{}, 10, namespace, pool) | |
wp.JobWithOptions(jobName, work.JobOptions{}, func(j *work.Job) error { | |
return errors.New("error") | |
}) | |
wp.Start() | |
defer wp.Stop() | |
<-time.After(time.Second) | |
// Fetch length of inprogress queue | |
poolID := getPoolID(wp) | |
inProgressKey := fmt.Sprintf("%s:jobs:%s:%s:inprogress", namespace, jobName, poolID) | |
length, _ := redis.Int(pool.Get().Do("LLEN", inProgressKey)) | |
fmt.Println("poolID:", poolID) | |
fmt.Println("Queue length:", length) | |
} | |
func getPoolID(wp *work.WorkerPool) string { | |
value := reflect.ValueOf(wp).Elem() | |
return value.FieldByName("workerPoolID").String() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment