mirror of
https://github.com/hibiken/asynq.git
synced 2026-04-26 07:25:56 +03:00
[GH-ISSUE #132] [QUESTION] i think set stop in run function is not a good idea #2065
Labels
No labels
CLI
bug
designing
documentation
duplicate
enhancement
good first issue
good first issue
help wanted
idea
invalid
investigate
needs-more-info
performance
pr-welcome
pull-request
question
wontfix
work in progress
work in progress
work-around-available
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/asynq#2065
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @yxlimo on GitHub (Apr 9, 2020).
Original GitHub issue: https://github.com/hibiken/asynq/issues/132
Originally assigned to: @hibiken on GitHub.
https://github.com/hibiken/asynq/blob/master/background.go#L253
normally, packages has two methods,
RunandStop, so i can cleanup other resources before or after Stop(),another scene is that user will run multipe backgroud server in
main(), like kafka consumer@hibiken commented on GitHub (Apr 9, 2020):
@yxlimo Thank you for opening this issue!
Would exporting
Background.StartandBackground.Stopwould resolve this?Proposal
Keep
Background.Runas is, since it also does signal handling.Expose
Background.StartandBackground.Stop, if a user of the package wants to have more control over when to start and stop the background workers .Please let me know your thoughts and feedback!
@yxlimo commented on GitHub (Apr 9, 2020):
Looks good.
also export
Background.WaitSignal? more control and more convenient@hibiken commented on GitHub (Apr 9, 2020):
Yep, I agree.
So if you opt out of using
Runthen code would look something like this to achieve the same effect:would that work for your use case?
@yxlimo commented on GitHub (Apr 9, 2020):
its work, thanks :)
@hibiken commented on GitHub (Apr 9, 2020):
Actually I've been thinking about doing some major API changes and I think this is a great opportunity to clean up some things.
Let me run this by you and hopefully get your feedback.
Or optionally you can do
ServeandShutdownto start/stop background workers manually.I've take an API ideas from
net/http'sServertype.Since this is a disruptive but important change, any feedback is welcome 👍
@yxlimo commented on GitHub (Apr 9, 2020):
the idea from
net/httpseems good, but the functions nameNewServeris confused.If i did not read the doc, i would think it's a server to receive request and produce a job. other names likeServe,NewServeMuxalso confusedHandler is part of Config: i prefer the api likegin@hibiken commented on GitHub (Apr 9, 2020):
Great feedback, thank you! Let me think about this a bit more.
@hibiken commented on GitHub (Apr 9, 2020):
Ok here's my next proposal, feedback appreciated!
Add
func (bg *Background) Use( ...Middleware)func (bg *Background) Handle(string, Handler)func (bg *Background) HandleFunc(string, HandlerFunc)func (bg *Background) Start() errorfunc (bg *Background) Stop()func (bg *Background) Shutdown()ErrBackgroundClosed: onceStoporShutdownis called on aBackground, this error will be returned fromRunorStartto prevent reuse.Remove
ServeMuxtype (it's internalized inBackground)Change
func (bg *Background) Run(Handler)tofunc (bg *Background) Run() errorExample
After the change,
mainmay look something like this.Or
bg.Run()could be replaced with@yxlimo commented on GitHub (Apr 10, 2020):
awesome! just a little confused.
Stopmethod, what doesShutdowndo ? this method make me think the process will exist.and here is more thinking(based on the experience of using
gocraft/workandsidekiq):bg.Handlercan receive options to custom each task handler, i think its important. i can't find this feature in current design, the option likeQueuesandConcurrencyseems should be task level instead of global level@hibiken commented on GitHub (Apr 10, 2020):
I see. I can see why this could be confusing. Let me try to explain.
First Question
Asynq took a lot of ideas from Sidekiq and they use OS signals to handle graceful shutdown (video).
Currently, when you start background workers with
bg.Runit waits for signals.Asynq handles TERM, INT, and TSTP signals.
If you send TSTP signal to the process, asynq will stop processing new tasks but doesn't exit. This is documented in Sidekiq here.
If you send TERM or INT signal to the process, asynq will wait for few seconds to let active workers finish its tasks but if they don't finish within that timeout, asynq will push the tasks back to Redis and exit the process. This is documented in Sidekiq here.
My intention was to name the first operation
Stopand the second oneShutdown. But I can see whyStopis a bit confusing. Looking at sidekiq doc, maybe we can name itbg.Quiet()?Does that makes sense?
Second Question
I agree with the
Concurrencyper Handler (i.e.Concurrencyper task type). For example, if you have a task to use third-party API and if they have a limit on how many concurrent API calls they can handle, this is definitely useful. Am I understanding you correctly here?I'm not sure if we can add an option to specify Queues per Handler though.
gocraft/workhas a different design in that they have one-to-one mapping between job-type and queue. Asynq's design is similar to sidekiq. It allows for multipe queues with different priority but any task can be put on any queue. So I'm not sure if we can create Queue option for a Handler. (please let me know if I'm not understanding your point correctly).@yxlimo commented on GitHub (Apr 10, 2020):
totally right. thanks for your explanation.
now i understand the design for
QueuesFor the first question
after reading the wiki, i found that TSTP just only stop processing new task, and TERM is more useful.
in
golanguage, i found most packages have only oneStopmethod(orClose).i think it's enough. sostopmethod do not need to be exported.leave two methods maybe will let user like me confused too, we can't assume that everyone will read the doc. What do you think?
@hibiken commented on GitHub (Apr 10, 2020):
Yep, just exporting just
StartandStopmakes sense to me (choseStopbecause of the symmetry withStart).So effectively, if user uses
StartandStopinstead ofRunthey don't get some nice signal handling but I guess that's okay. If we get feature request to exportQuiet(i.e. stop processing new tasks), maybe we'll add that later.I'll get started on making these changes and ping you in the PR 👍
@hibiken commented on GitHub (Apr 11, 2020):
@yxlimo Now that I've started working on these API changes. I have a few thoughts and want to think about them more (sorry, for going back and forth so many times 🙏).
If this change is blocking you, please feel free to fork and use that in the meantime!
@yxlimo commented on GitHub (Apr 12, 2020):
It's fine. breaking change needs to thinking more and more.
Looking forward to your final design.
@zsiec commented on GitHub (Apr 15, 2020):
+1 to exporting these. Some weeks ago I forked and exposed start/stop myself but have now stumbled on this thread and would be happy to remove my fork and use this once it’s ready!
My current implementation (importing my fork) handles the syscall termination signals for the workers as I was already handling them for other things before using asynq and it’s helpful to me to have my graceful shutdown logic collocated. It was also trivial to implement a shutdown timeout external to this library.
Thanks for your work on this very usable project, looking forward to seeing these updates!
@hibiken commented on GitHub (Apr 15, 2020):
@zsiec Awesome to hear your feedback! Thank you.
I'm currently drafting a PR for this change, which includes a few breaking changes (I'll make sure to document the upgrade paths in CHANGELOG for those who are on the current version).
@hibiken commented on GitHub (Apr 19, 2020):
Closed via #135