rename WaitForCompletion to Done

This commit is contained in:
syumai 2024-11-08 02:33:28 +09:00
parent 87cf65a213
commit 68bb0c7e08
2 changed files with 15 additions and 12 deletions

View File

@ -12,7 +12,10 @@ import (
type Task func(ctx context.Context) error type Task func(ctx context.Context) error
var scheduledTask Task var (
scheduledTask Task
doneCh = make(chan struct{})
)
func runScheduler(eventObj js.Value) error { func runScheduler(eventObj js.Value) error {
ctx := runtimecontext.New(context.Background(), eventObj) ctx := runtimecontext.New(context.Background(), eventObj)
@ -51,7 +54,7 @@ func init() {
func ScheduleTask(task Task) { func ScheduleTask(task Task) {
scheduledTask = task scheduledTask = task
workers.Ready() workers.Ready()
WaitForCompletion() <-Done()
} }
// ScheduleTaskNonBlock sets the Task to be executed but does not signal readiness or block // ScheduleTaskNonBlock sets the Task to be executed but does not signal readiness or block
@ -60,8 +63,8 @@ func ScheduleTaskNonBlock(task Task) {
scheduledTask = task scheduledTask = task
} }
// WaitForCompletion blocks until the task set by ScheduleTaskWithNonBlock is completed. // Done returns a channel which is closed when the task is done.
// Currently, this function never returns to support cloudflare.WaitUntil feature. // Currently, this channel is never closed to support cloudflare.WaitUntil feature.
func WaitForCompletion() { func Done() <-chan struct{} {
select {} return doneCh
} }

View File

@ -16,7 +16,7 @@ import (
var ( var (
httpHandler http.Handler httpHandler http.Handler
closeCh = make(chan struct{}) doneCh = make(chan struct{})
) )
func init() { func init() {
@ -52,7 +52,7 @@ type appCloser struct {
} }
func (c *appCloser) Close() error { func (c *appCloser) Close() error {
defer close(closeCh) defer close(doneCh)
return c.ReadCloser.Close() return c.ReadCloser.Close()
} }
@ -89,7 +89,7 @@ func handleRequest(reqObj js.Value) (js.Value, error) {
func Serve(handler http.Handler) { func Serve(handler http.Handler) {
ServeNonBlock(handler) ServeNonBlock(handler)
Ready() Ready()
WaitForCompletion() <-Done()
} }
// ServeNonBlock sets the http.Handler to be served but does not signal readiness or block // ServeNonBlock sets the http.Handler to be served but does not signal readiness or block
@ -109,7 +109,7 @@ func Ready() {
ready() ready()
} }
// WaitForCompletion blocks until the handler set by ServeNonBlock is completed. // Done returns a channel which is closed when the handler is done.
func WaitForCompletion() { func Done() <-chan struct{} {
<-closeCh return doneCh
} }