From 33613b27b0258a6cf8469d2cb0e9029e1fe5de70 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Tue, 11 Mar 2025 15:00:56 +0200 Subject: [PATCH] [#6481] allowed calling cronAdd and cronRemove from inside other JSVM handlers --- CHANGELOG.md | 9 +++++---- plugins/jsvm/binds.go | 21 ++++++++++----------- plugins/jsvm/binds_test.go | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1662add..91ddec34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,14 +9,15 @@ For example, to disable MFA for the OAuth2 auth could be expressed as `@request.context != "oauth2"` MFA rule. (@todo docs) -- Added `$os.stat(file)` JSVM helper ([#6407](https://github.com/pocketbase/pocketbase/discussions/6407)). - -- Added log warning for `async` marked JSVM handlers and resolve when possible the returned `Promise` as fallback ([#6476](https://github.com/pocketbase/pocketbase/issues/6476)). - - Added `store.Store.SetFunc(key, func(old T) new T)` to set/update a store value with the return result of the callback in a concurrent safe manner. - Added `subscription.Message.WriteSSE(w, id)` for writing an SSE formatted message into the provided writer interface (_used mostly to assist with the unit testing_). +- Added `$os.stat(file)` JSVM helper ([#6407](https://github.com/pocketbase/pocketbase/discussions/6407)). + +- Added log warning for `async` marked JSVM handlers and resolve when possible the returned `Promise` as fallback ([#6476](https://github.com/pocketbase/pocketbase/issues/6476)). + +- Allowed calling `cronAdd`, `cronRemove` from inside other JSVM handlers ([#6481](https://github.com/pocketbase/pocketbase/discussions/6481)). - Bumped the default request read and write timeouts to 5mins (_old 3mins_) to accommodate slower internet connections and larger file uploads/downloads. _If you want to change them you can modify the `OnServe` hook's `ServeEvent.ReadTimeout/WriteTimeout` fields as shown in [#6550](https://github.com/pocketbase/pocketbase/discussions/6550#discussioncomment-12364515)._ diff --git a/plugins/jsvm/binds.go b/plugins/jsvm/binds.go index a725fde7..43595464 100644 --- a/plugins/jsvm/binds.go +++ b/plugins/jsvm/binds.go @@ -101,7 +101,7 @@ func hooksBinds(app core.App, loader *goja.Runtime, executors *vmsPool) { } func cronBinds(app core.App, loader *goja.Runtime, executors *vmsPool) { - loader.Set("cronAdd", func(jobId, cronExpr, handler string) { + cronAdd := func(jobId, cronExpr, handler string) { pr := goja.MustCompile(defaultScriptPath, "{("+handler+").apply(undefined)}", true) err := app.Cron().Add(jobId, cronExpr, func() { @@ -121,28 +121,27 @@ func cronBinds(app core.App, loader *goja.Runtime, executors *vmsPool) { if err != nil { panic("[cronAdd] failed to register cron job " + jobId + ": " + err.Error()) } - }) + } + loader.Set("cronAdd", cronAdd) - // note: it is not necessary needed but it is here for consistency - loader.Set("cronRemove", func(jobId string) { + cronRemove := func(jobId string) { app.Cron().Remove(jobId) - }) + } + loader.Set("cronRemove", cronRemove) // register the removal helper also in the executors to allow removing cron jobs from everywhere oldFactory := executors.factory executors.factory = func() *goja.Runtime { vm := oldFactory() - vm.Set("cronRemove", func(jobId string) { - app.Cron().Remove(jobId) - }) + vm.Set("cronAdd", cronAdd) + vm.Set("cronRemove", cronRemove) return vm } for _, item := range executors.items { - item.vm.Set("cronRemove", func(jobId string) { - app.Cron().Remove(jobId) - }) + item.vm.Set("cronAdd", cronAdd) + item.vm.Set("cronRemove", cronRemove) } } diff --git a/plugins/jsvm/binds_test.go b/plugins/jsvm/binds_test.go index 8f163e11..10f5e967 100644 --- a/plugins/jsvm/binds_test.go +++ b/plugins/jsvm/binds_test.go @@ -1438,7 +1438,7 @@ func TestCronBindsCount(t *testing.T) { testBindsCount(vm, "this", 2, t) pool.run(func(poolVM *goja.Runtime) error { - testBindsCount(poolVM, "this", 1, t) + testBindsCount(poolVM, "this", 2, t) return nil }) }