split cloudflare/cron.WaitForCompletion from cron.ScheduleTask

This commit is contained in:
syumai 2024-11-08 02:05:57 +09:00
parent 5f0fcb01dd
commit 2a59c59bae

View File

@ -5,6 +5,7 @@ import (
"fmt"
"syscall/js"
"github.com/syumai/workers"
"github.com/syumai/workers/internal/jsutil"
"github.com/syumai/workers/internal/runtimecontext"
)
@ -46,14 +47,11 @@ func init() {
jsutil.Binding.Set("runScheduler", runSchedulerCallback)
}
//go:wasmimport workers ready
func ready()
// ScheduleTask sets the Task to be executed
func ScheduleTask(task Task) {
scheduledTask = task
ready()
select {}
workers.Ready()
WaitForCompletion()
}
// ScheduleTaskNonBlock sets the Task to be executed but does not signal readiness or block
@ -61,3 +59,9 @@ func ScheduleTask(task Task) {
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 {}
}