-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
task queue: support multiple client subscriber
- Loading branch information
1 parent
2e05fef
commit 56db39b
Showing
7 changed files
with
174 additions
and
99 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package taskqueueworker | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
const maxClientSubscribers = 2 | ||
|
||
var errClientLimitExceeded = errors.New("client limit exceeded, please try again later") | ||
|
||
func registerNewTaskListSubscriber(clientID string, clientChannel chan []TaskResolver) error { | ||
if len(clientTaskSubscribers) >= maxClientSubscribers { | ||
return errClientLimitExceeded | ||
} | ||
|
||
mutex.Lock() | ||
defer mutex.Unlock() | ||
|
||
clientTaskSubscribers[clientID] = clientChannel | ||
return nil | ||
} | ||
|
||
func removeTaskListSubscriber(clientID string) { | ||
mutex.Lock() | ||
defer mutex.Unlock() | ||
|
||
delete(clientTaskSubscribers, clientID) | ||
} | ||
|
||
func registerNewJobListSubscriber(taskName, clientID string, filter Filter, clientChannel chan JobListResolver) error { | ||
if len(clientJobTaskSubscribers) >= maxClientSubscribers { | ||
return errClientLimitExceeded | ||
} | ||
|
||
mutex.Lock() | ||
defer mutex.Unlock() | ||
|
||
clientJobTaskSubscribers[clientID] = clientJobTaskSubscriber{ | ||
c: clientChannel, filter: filter, | ||
} | ||
return nil | ||
} | ||
|
||
func removeJobListSubscriber(taskName, clientID string) { | ||
mutex.Lock() | ||
defer mutex.Unlock() | ||
|
||
delete(clientJobTaskSubscribers, clientID) | ||
} | ||
|
||
func broadcastAllToSubscribers() { | ||
go broadcastTaskList() | ||
go broadcastJobList() | ||
} | ||
|
||
func broadcastTaskList() { | ||
var taskRes []TaskResolver | ||
for _, task := range tasks { | ||
var tsk = TaskResolver{ | ||
Name: task, | ||
} | ||
tsk.Detail.GiveUp = repo.countTaskJobDetail(task, statusFailure) | ||
tsk.Detail.Retrying = repo.countTaskJobDetail(task, statusRetrying) | ||
tsk.Detail.Success = repo.countTaskJobDetail(task, statusSuccess) | ||
tsk.Detail.Queueing = repo.countTaskJobDetail(task, statusQueueing) | ||
tsk.Detail.Stopped = repo.countTaskJobDetail(task, statusStopped) | ||
tsk.TotalJobs = tsk.Detail.GiveUp + tsk.Detail.Retrying + tsk.Detail.Success + tsk.Detail.Queueing + tsk.Detail.Stopped | ||
taskRes = append(taskRes, tsk) | ||
} | ||
|
||
for _, subscriber := range clientTaskSubscribers { | ||
subscriber <- taskRes | ||
} | ||
} | ||
|
||
func broadcastJobList() { | ||
for _, subscriber := range clientJobTaskSubscribers { | ||
meta, jobs := repo.findAllJob(subscriber.filter) | ||
subscriber.c <- JobListResolver{ | ||
Meta: meta, | ||
Data: jobs, | ||
} | ||
} | ||
} |
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
Oops, something went wrong.