diff --git a/cloudflare/cron/scheduler.go b/cloudflare/cron/scheduler.go index e4d3c40..d0485f6 100644 --- a/cloudflare/cron/scheduler.go +++ b/cloudflare/cron/scheduler.go @@ -12,7 +12,10 @@ import ( type Task func(ctx context.Context) error -var scheduledTask Task +var ( + scheduledTask Task + doneCh = make(chan struct{}) +) func runScheduler(eventObj js.Value) error { ctx := runtimecontext.New(context.Background(), eventObj) @@ -51,7 +54,7 @@ func init() { func ScheduleTask(task Task) { scheduledTask = task workers.Ready() - WaitForCompletion() + <-Done() } // ScheduleTaskNonBlock sets the Task to be executed but does not signal readiness or block @@ -60,8 +63,8 @@ func ScheduleTaskNonBlock(task Task) { scheduledTask = task } -// WaitForCompletion blocks until the task set by ScheduleTaskWithNonBlock is completed. -// Currently, this function never returns to support cloudflare.WaitUntil feature. -func WaitForCompletion() { - select {} +// Done returns a channel which is closed when the task is done. +// Currently, this channel is never closed to support cloudflare.WaitUntil feature. +func Done() <-chan struct{} { + return doneCh } diff --git a/handler_js.go b/handler_js.go index 9faa52f..54f1fdf 100644 --- a/handler_js.go +++ b/handler_js.go @@ -16,7 +16,7 @@ import ( var ( httpHandler http.Handler - closeCh = make(chan struct{}) + doneCh = make(chan struct{}) ) func init() { @@ -52,7 +52,7 @@ type appCloser struct { } func (c *appCloser) Close() error { - defer close(closeCh) + defer close(doneCh) return c.ReadCloser.Close() } @@ -89,7 +89,7 @@ func handleRequest(reqObj js.Value) (js.Value, error) { func Serve(handler http.Handler) { ServeNonBlock(handler) Ready() - WaitForCompletion() + <-Done() } // ServeNonBlock sets the http.Handler to be served but does not signal readiness or block @@ -109,7 +109,7 @@ func Ready() { ready() } -// WaitForCompletion blocks until the handler set by ServeNonBlock is completed. -func WaitForCompletion() { - <-closeCh +// Done returns a channel which is closed when the handler is done. +func Done() <-chan struct{} { + return doneCh }