mirror of
https://github.com/hibiken/asynq.git
synced 2025-10-03 05:12:01 +08:00
(cli): Define dataFetcher type
This commit is contained in:
@@ -99,19 +99,24 @@ func Run(opts Options) {
|
|||||||
ticker := time.NewTicker(opts.PollInterval)
|
ticker := time.NewTicker(opts.PollInterval)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
f := dataFetcher{
|
||||||
|
ticker,
|
||||||
|
inspector,
|
||||||
|
opts,
|
||||||
|
errorCh,
|
||||||
|
queueCh,
|
||||||
|
queuesCh,
|
||||||
|
groupsCh,
|
||||||
|
tasksCh,
|
||||||
|
redisInfoCh,
|
||||||
|
}
|
||||||
|
|
||||||
h := keyEventHandler{
|
h := keyEventHandler{
|
||||||
s: s,
|
s: s,
|
||||||
|
fetcher: &f,
|
||||||
state: &state,
|
state: &state,
|
||||||
opts: opts,
|
opts: opts,
|
||||||
done: done,
|
done: done,
|
||||||
ticker: ticker,
|
|
||||||
inspector: inspector,
|
|
||||||
errorCh: errorCh,
|
|
||||||
queueCh: queueCh,
|
|
||||||
queuesCh: queuesCh,
|
|
||||||
groupsCh: groupsCh,
|
|
||||||
tasksCh: tasksCh,
|
|
||||||
redisInfoCh: redisInfoCh,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Double check that we are not leaking goroutine with this one.
|
// TODO: Double check that we are not leaking goroutine with this one.
|
||||||
|
@@ -6,10 +6,35 @@ package dash
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hibiken/asynq"
|
"github.com/hibiken/asynq"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type dataFetcher struct {
|
||||||
|
ticker *time.Ticker
|
||||||
|
inspector *asynq.Inspector
|
||||||
|
opts Options
|
||||||
|
|
||||||
|
errorCh chan<- error
|
||||||
|
queueCh chan<- *asynq.QueueInfo
|
||||||
|
queuesCh chan<- []*asynq.QueueInfo
|
||||||
|
groupsCh chan<- []*asynq.GroupInfo
|
||||||
|
tasksCh chan<- []*asynq.TaskInfo
|
||||||
|
redisInfoCh chan<- *redisInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *dataFetcher) fetchQueues() {
|
||||||
|
var (
|
||||||
|
inspector = f.inspector
|
||||||
|
queuesCh = f.queuesCh
|
||||||
|
errorCh = f.errorCh
|
||||||
|
opts = f.opts
|
||||||
|
)
|
||||||
|
go fetchQueues(inspector, queuesCh, errorCh, opts)
|
||||||
|
f.ticker.Reset(opts.PollInterval)
|
||||||
|
}
|
||||||
|
|
||||||
func fetchQueues(i *asynq.Inspector, queuesCh chan<- []*asynq.QueueInfo, errorCh chan<- error, opts Options) {
|
func fetchQueues(i *asynq.Inspector, queuesCh chan<- []*asynq.QueueInfo, errorCh chan<- error, opts Options) {
|
||||||
if !opts.UseRealData {
|
if !opts.UseRealData {
|
||||||
n := rand.Intn(100)
|
n := rand.Intn(100)
|
||||||
@@ -37,6 +62,18 @@ func fetchQueues(i *asynq.Inspector, queuesCh chan<- []*asynq.QueueInfo, errorCh
|
|||||||
queuesCh <- res
|
queuesCh <- res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *dataFetcher) fetchQueueInfo(qname string) {
|
||||||
|
var (
|
||||||
|
inspector = f.inspector
|
||||||
|
queueCh = f.queueCh
|
||||||
|
errorCh = f.errorCh
|
||||||
|
opts = f.opts
|
||||||
|
ticker = f.ticker
|
||||||
|
)
|
||||||
|
go fetchQueueInfo(inspector, qname, queueCh, errorCh)
|
||||||
|
ticker.Reset(opts.PollInterval)
|
||||||
|
}
|
||||||
|
|
||||||
func fetchQueueInfo(i *asynq.Inspector, qname string, queueCh chan<- *asynq.QueueInfo, errorCh chan<- error) {
|
func fetchQueueInfo(i *asynq.Inspector, qname string, queueCh chan<- *asynq.QueueInfo, errorCh chan<- error) {
|
||||||
q, err := i.GetQueueInfo(qname)
|
q, err := i.GetQueueInfo(qname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -46,6 +83,11 @@ func fetchQueueInfo(i *asynq.Inspector, qname string, queueCh chan<- *asynq.Queu
|
|||||||
queueCh <- q
|
queueCh <- q
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *dataFetcher) fetchRedisInfo() {
|
||||||
|
go fetchRedisInfo(f.redisInfoCh, f.errorCh)
|
||||||
|
f.ticker.Reset(f.opts.PollInterval)
|
||||||
|
}
|
||||||
|
|
||||||
func fetchRedisInfo(redisInfoCh chan<- *redisInfo, errorCh chan<- error) {
|
func fetchRedisInfo(redisInfoCh chan<- *redisInfo, errorCh chan<- error) {
|
||||||
n := rand.Intn(1000)
|
n := rand.Intn(1000)
|
||||||
redisInfoCh <- &redisInfo{
|
redisInfoCh <- &redisInfo{
|
||||||
@@ -56,6 +98,13 @@ func fetchRedisInfo(redisInfoCh chan<- *redisInfo, errorCh chan<- error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *dataFetcher) fetchGroups(qname string) {
|
||||||
|
i, groupsCh, errorCh := f.inspector, f.groupsCh, f.errorCh
|
||||||
|
ticker, opts := f.ticker, f.opts
|
||||||
|
go fetchGroups(i, qname, groupsCh, errorCh)
|
||||||
|
ticker.Reset(opts.PollInterval)
|
||||||
|
}
|
||||||
|
|
||||||
func fetchGroups(i *asynq.Inspector, qname string, groupsCh chan<- []*asynq.GroupInfo, errorCh chan<- error) {
|
func fetchGroups(i *asynq.Inspector, qname string, groupsCh chan<- []*asynq.GroupInfo, errorCh chan<- error) {
|
||||||
groups, err := i.Groups(qname)
|
groups, err := i.Groups(qname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -65,6 +114,18 @@ func fetchGroups(i *asynq.Inspector, qname string, groupsCh chan<- []*asynq.Grou
|
|||||||
groupsCh <- groups
|
groupsCh <- groups
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *dataFetcher) fetchAggregatingTasks(qname, group string, pageSize, pageNum int) {
|
||||||
|
var (
|
||||||
|
i = f.inspector
|
||||||
|
tasksCh = f.tasksCh
|
||||||
|
errorCh = f.errorCh
|
||||||
|
ticker = f.ticker
|
||||||
|
opts = f.opts
|
||||||
|
)
|
||||||
|
go fetchAggregatingTasks(i, qname, group, pageSize, pageNum, tasksCh, errorCh)
|
||||||
|
ticker.Reset(opts.PollInterval)
|
||||||
|
}
|
||||||
|
|
||||||
func fetchAggregatingTasks(i *asynq.Inspector, qname, group string, pageSize, pageNum int,
|
func fetchAggregatingTasks(i *asynq.Inspector, qname, group string, pageSize, pageNum int,
|
||||||
tasksCh chan<- []*asynq.TaskInfo, errorCh chan<- error) {
|
tasksCh chan<- []*asynq.TaskInfo, errorCh chan<- error) {
|
||||||
tasks, err := i.ListAggregatingTasks(qname, group, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
tasks, err := i.ListAggregatingTasks(qname, group, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
||||||
@@ -75,6 +136,18 @@ func fetchAggregatingTasks(i *asynq.Inspector, qname, group string, pageSize, pa
|
|||||||
tasksCh <- tasks
|
tasksCh <- tasks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *dataFetcher) fetchTasks(qname string, taskState asynq.TaskState, pageSize, pageNum int) {
|
||||||
|
var (
|
||||||
|
i = f.inspector
|
||||||
|
tasksCh = f.tasksCh
|
||||||
|
errorCh = f.errorCh
|
||||||
|
ticker = f.ticker
|
||||||
|
opts = f.opts
|
||||||
|
)
|
||||||
|
go fetchTasks(i, qname, taskState, pageSize, pageNum, tasksCh, errorCh)
|
||||||
|
ticker.Reset(opts.PollInterval)
|
||||||
|
}
|
||||||
|
|
||||||
func fetchTasks(i *asynq.Inspector, qname string, taskState asynq.TaskState, pageSize, pageNum int,
|
func fetchTasks(i *asynq.Inspector, qname string, taskState asynq.TaskState, pageSize, pageNum int,
|
||||||
tasksCh chan<- []*asynq.TaskInfo, errorCh chan<- error) {
|
tasksCh chan<- []*asynq.TaskInfo, errorCh chan<- error) {
|
||||||
var (
|
var (
|
||||||
|
@@ -6,7 +6,6 @@ package dash
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gdamore/tcell/v2"
|
"github.com/gdamore/tcell/v2"
|
||||||
"github.com/hibiken/asynq"
|
"github.com/hibiken/asynq"
|
||||||
@@ -18,15 +17,7 @@ type keyEventHandler struct {
|
|||||||
opts Options
|
opts Options
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
|
|
||||||
ticker *time.Ticker
|
fetcher *dataFetcher
|
||||||
inspector *asynq.Inspector
|
|
||||||
|
|
||||||
errorCh chan error
|
|
||||||
queueCh chan *asynq.QueueInfo
|
|
||||||
queuesCh chan []*asynq.QueueInfo
|
|
||||||
groupsCh chan []*asynq.GroupInfo
|
|
||||||
tasksCh chan []*asynq.TaskInfo
|
|
||||||
redisInfoCh chan *redisInfo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *keyEventHandler) quit() {
|
func (h *keyEventHandler) quit() {
|
||||||
@@ -173,10 +164,7 @@ func (h *keyEventHandler) enterKeyQueues() {
|
|||||||
s = h.s
|
s = h.s
|
||||||
state = h.state
|
state = h.state
|
||||||
opts = h.opts
|
opts = h.opts
|
||||||
inspector = h.inspector
|
f = h.fetcher
|
||||||
ticker = h.ticker
|
|
||||||
errorCh = h.errorCh
|
|
||||||
tasksCh = h.tasksCh
|
|
||||||
)
|
)
|
||||||
if state.queueTableRowIdx != 0 {
|
if state.queueTableRowIdx != 0 {
|
||||||
state.selectedQueue = state.queues[state.queueTableRowIdx-1]
|
state.selectedQueue = state.queues[state.queueTableRowIdx-1]
|
||||||
@@ -184,9 +172,7 @@ func (h *keyEventHandler) enterKeyQueues() {
|
|||||||
state.taskState = asynq.TaskStateActive
|
state.taskState = asynq.TaskStateActive
|
||||||
state.tasks = nil
|
state.tasks = nil
|
||||||
state.pageNum = 1
|
state.pageNum = 1
|
||||||
go fetchTasks(inspector, state.selectedQueue.Queue, state.taskState,
|
f.fetchTasks(state.selectedQueue.Queue, state.taskState, taskPageSize(s), state.pageNum)
|
||||||
taskPageSize(s), state.pageNum, tasksCh, errorCh)
|
|
||||||
ticker.Reset(opts.PollInterval)
|
|
||||||
drawDash(s, state, opts)
|
drawDash(s, state, opts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -196,18 +182,13 @@ func (h *keyEventHandler) enterKeyQueueDetails() {
|
|||||||
s = h.s
|
s = h.s
|
||||||
state = h.state
|
state = h.state
|
||||||
opts = h.opts
|
opts = h.opts
|
||||||
inspector = h.inspector
|
f = h.fetcher
|
||||||
ticker = h.ticker
|
|
||||||
errorCh = h.errorCh
|
|
||||||
tasksCh = h.tasksCh
|
|
||||||
)
|
)
|
||||||
if shouldShowGroupTable(state) && state.groupTableRowIdx != 0 {
|
if shouldShowGroupTable(state) && state.groupTableRowIdx != 0 {
|
||||||
state.selectedGroup = state.groups[state.groupTableRowIdx-1]
|
state.selectedGroup = state.groups[state.groupTableRowIdx-1]
|
||||||
state.tasks = nil
|
state.tasks = nil
|
||||||
state.pageNum = 1
|
state.pageNum = 1
|
||||||
go fetchAggregatingTasks(inspector, state.selectedQueue.Queue, state.selectedGroup.Group,
|
f.fetchAggregatingTasks(state.selectedQueue.Queue, state.selectedGroup.Group, taskPageSize(s), state.pageNum)
|
||||||
taskPageSize(s), state.pageNum, tasksCh, errorCh)
|
|
||||||
ticker.Reset(opts.PollInterval)
|
|
||||||
drawDash(s, state, opts)
|
drawDash(s, state, opts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -217,11 +198,7 @@ func (h *keyEventHandler) handleLeftKey() {
|
|||||||
s = h.s
|
s = h.s
|
||||||
state = h.state
|
state = h.state
|
||||||
opts = h.opts
|
opts = h.opts
|
||||||
inspector = h.inspector
|
f = h.fetcher
|
||||||
ticker = h.ticker
|
|
||||||
errorCh = h.errorCh
|
|
||||||
tasksCh = h.tasksCh
|
|
||||||
groupsCh = h.groupsCh
|
|
||||||
)
|
)
|
||||||
if state.view == viewTypeQueueDetails {
|
if state.view == viewTypeQueueDetails {
|
||||||
state.taskState = prevTaskState(state.taskState)
|
state.taskState = prevTaskState(state.taskState)
|
||||||
@@ -230,12 +207,10 @@ func (h *keyEventHandler) handleLeftKey() {
|
|||||||
state.tasks = nil
|
state.tasks = nil
|
||||||
state.selectedGroup = nil
|
state.selectedGroup = nil
|
||||||
if shouldShowGroupTable(state) {
|
if shouldShowGroupTable(state) {
|
||||||
go fetchGroups(inspector, state.selectedQueue.Queue, groupsCh, errorCh)
|
f.fetchGroups(state.selectedQueue.Queue)
|
||||||
} else {
|
} else {
|
||||||
go fetchTasks(inspector, state.selectedQueue.Queue, state.taskState,
|
f.fetchTasks(state.selectedQueue.Queue, state.taskState, taskPageSize(s), state.pageNum)
|
||||||
taskPageSize(s), state.pageNum, tasksCh, errorCh)
|
|
||||||
}
|
}
|
||||||
ticker.Reset(opts.PollInterval)
|
|
||||||
drawDash(s, state, opts)
|
drawDash(s, state, opts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -245,11 +220,7 @@ func (h *keyEventHandler) handleRightKey() {
|
|||||||
s = h.s
|
s = h.s
|
||||||
state = h.state
|
state = h.state
|
||||||
opts = h.opts
|
opts = h.opts
|
||||||
inspector = h.inspector
|
f = h.fetcher
|
||||||
ticker = h.ticker
|
|
||||||
errorCh = h.errorCh
|
|
||||||
tasksCh = h.tasksCh
|
|
||||||
groupsCh = h.groupsCh
|
|
||||||
)
|
)
|
||||||
if state.view == viewTypeQueueDetails {
|
if state.view == viewTypeQueueDetails {
|
||||||
state.taskState = nextTaskState(state.taskState)
|
state.taskState = nextTaskState(state.taskState)
|
||||||
@@ -258,12 +229,10 @@ func (h *keyEventHandler) handleRightKey() {
|
|||||||
state.tasks = nil
|
state.tasks = nil
|
||||||
state.selectedGroup = nil
|
state.selectedGroup = nil
|
||||||
if shouldShowGroupTable(state) {
|
if shouldShowGroupTable(state) {
|
||||||
go fetchGroups(inspector, state.selectedQueue.Queue, groupsCh, errorCh)
|
f.fetchGroups(state.selectedQueue.Queue)
|
||||||
} else {
|
} else {
|
||||||
go fetchTasks(inspector, state.selectedQueue.Queue, state.taskState,
|
f.fetchTasks(state.selectedQueue.Queue, state.taskState, taskPageSize(s), state.pageNum)
|
||||||
taskPageSize(s), state.pageNum, tasksCh, errorCh)
|
|
||||||
}
|
}
|
||||||
ticker.Reset(opts.PollInterval)
|
|
||||||
drawDash(s, state, opts)
|
drawDash(s, state, opts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -273,10 +242,7 @@ func (h *keyEventHandler) nextPage() {
|
|||||||
s = h.s
|
s = h.s
|
||||||
state = h.state
|
state = h.state
|
||||||
opts = h.opts
|
opts = h.opts
|
||||||
inspector = h.inspector
|
f = h.fetcher
|
||||||
ticker = h.ticker
|
|
||||||
errorCh = h.errorCh
|
|
||||||
tasksCh = h.tasksCh
|
|
||||||
)
|
)
|
||||||
if state.view == viewTypeQueueDetails {
|
if state.view == viewTypeQueueDetails {
|
||||||
if shouldShowGroupTable(state) {
|
if shouldShowGroupTable(state) {
|
||||||
@@ -293,9 +259,7 @@ func (h *keyEventHandler) nextPage() {
|
|||||||
totalCount := getTaskCount(state.selectedQueue, state.taskState)
|
totalCount := getTaskCount(state.selectedQueue, state.taskState)
|
||||||
if (state.pageNum-1)*pageSize+len(state.tasks) < totalCount {
|
if (state.pageNum-1)*pageSize+len(state.tasks) < totalCount {
|
||||||
state.pageNum++
|
state.pageNum++
|
||||||
go fetchTasks(inspector, state.selectedQueue.Queue, state.taskState,
|
f.fetchTasks(state.selectedQueue.Queue, state.taskState, pageSize, state.pageNum)
|
||||||
pageSize, state.pageNum, tasksCh, errorCh)
|
|
||||||
ticker.Reset(opts.PollInterval)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -306,10 +270,7 @@ func (h *keyEventHandler) prevPage() {
|
|||||||
s = h.s
|
s = h.s
|
||||||
state = h.state
|
state = h.state
|
||||||
opts = h.opts
|
opts = h.opts
|
||||||
inspector = h.inspector
|
f = h.fetcher
|
||||||
ticker = h.ticker
|
|
||||||
errorCh = h.errorCh
|
|
||||||
tasksCh = h.tasksCh
|
|
||||||
)
|
)
|
||||||
if state.view == viewTypeQueueDetails {
|
if state.view == viewTypeQueueDetails {
|
||||||
if shouldShowGroupTable(state) {
|
if shouldShowGroupTable(state) {
|
||||||
@@ -322,9 +283,7 @@ func (h *keyEventHandler) prevPage() {
|
|||||||
} else {
|
} else {
|
||||||
if state.pageNum > 1 {
|
if state.pageNum > 1 {
|
||||||
state.pageNum--
|
state.pageNum--
|
||||||
go fetchTasks(inspector, state.selectedQueue.Queue, state.taskState,
|
f.fetchTasks(state.selectedQueue.Queue, state.taskState, taskPageSize(s), state.pageNum)
|
||||||
taskPageSize(s), state.pageNum, tasksCh, errorCh)
|
|
||||||
ticker.Reset(opts.PollInterval)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -334,15 +293,11 @@ func (h *keyEventHandler) showQueues() {
|
|||||||
var (
|
var (
|
||||||
s = h.s
|
s = h.s
|
||||||
state = h.state
|
state = h.state
|
||||||
inspector = h.inspector
|
|
||||||
queuesCh = h.queuesCh
|
|
||||||
errorCh = h.errorCh
|
|
||||||
opts = h.opts
|
opts = h.opts
|
||||||
ticker = h.ticker
|
f = h.fetcher
|
||||||
)
|
)
|
||||||
if state.view != viewTypeQueues {
|
if state.view != viewTypeQueues {
|
||||||
go fetchQueues(inspector, queuesCh, errorCh, opts)
|
f.fetchQueues()
|
||||||
ticker.Reset(opts.PollInterval)
|
|
||||||
state.view = viewTypeQueues
|
state.view = viewTypeQueues
|
||||||
drawDash(s, state, opts)
|
drawDash(s, state, opts)
|
||||||
}
|
}
|
||||||
@@ -379,13 +334,10 @@ func (h *keyEventHandler) showRedisInfo() {
|
|||||||
s = h.s
|
s = h.s
|
||||||
state = h.state
|
state = h.state
|
||||||
opts = h.opts
|
opts = h.opts
|
||||||
redisInfoCh = h.redisInfoCh
|
f = h.fetcher
|
||||||
errorCh = h.errorCh
|
|
||||||
ticker = h.ticker
|
|
||||||
)
|
)
|
||||||
if state.view != viewTypeRedis {
|
if state.view != viewTypeRedis {
|
||||||
go fetchRedisInfo(redisInfoCh, errorCh)
|
f.fetchRedisInfo()
|
||||||
ticker.Reset(opts.PollInterval)
|
|
||||||
state.view = viewTypeRedis
|
state.view = viewTypeRedis
|
||||||
drawDash(s, state, opts)
|
drawDash(s, state, opts)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user