Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support HTTP Request and Cron Trigger in Single Worker #127

Closed
ben-krieger opened this issue Oct 29, 2024 · 4 comments · Fixed by #130
Closed

Support HTTP Request and Cron Trigger in Single Worker #127

ben-krieger opened this issue Oct 29, 2024 · 4 comments · Fixed by #130

Comments

@ben-krieger
Copy link
Contributor

Currently cron.ScheduleTask and workers.Serve cannot both be used within the same func main, because they both call ready, causing a race condition.

Also, cron.ScheduleTask blocks indefinitely and workers.Serve blocks until the request is closed, so there needs to be a way to know the trigger type in order to decide if/when to exit main.

I don't think there's a Cloudflare restriction that a Worker can only be a Cron Trigger or HTTP Request handler, but I'm new to the Workers platform, so correct me if I'm wrong.

Obviously the workaround to this issue is to create two Workers, but the problem I'm solving for is keeping "Getting Started"-type documentation concise and being able give an example with a single main.go and wrangler.toml is highly beneficial.

@meandnano
Copy link
Contributor

Yeah, this should not be a cloudflare restriction. In fact, in my implementation of Queues consumer I've introduced 2 versions of the Consume function. One that blocks (just as workers.Serve) another that doesn't, specifically to use multiple handlers in one worker. Maybe that approach could be extended to other APIs. What do you think @syumai?

@syumai
Copy link
Owner

syumai commented Nov 7, 2024

Yes, it is not a Cloudflare restriction.
All we need to do is simply expose multiple handlers from worker.mjs.
https://github.com/syumai/workers/blob/main/cmd/workers-assets-gen/assets/common/worker.mjs

The non-blocking API proposed by @meandnano looks fine to me.
The blocking functionality of the current implementation (ready(); <-closeCh) should be separated and made common, like Wait(), Ready(), or WaitForReady().

@syumai
Copy link
Owner

syumai commented Nov 7, 2024

I added multiple handlers example here:

// set up the worker
workers.ServeNonBlock(handler)
cron.ScheduleTaskNonBlock(task)
// send a ready signal to the runtime
workers.Ready()
// block until the handler or task is done
select {
case <-workers.Done():
case <-cron.Done():
}

detail: #132

@syumai
Copy link
Owner

syumai commented Nov 9, 2024

@ben-krieger @meandnano This feature has been released as v0.27.0, thanks!
https://github.com/syumai/workers/releases/tag/v0.27.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants