Skip to content

Commit

Permalink
updated cron jsvm bindings and generated types
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Jul 16, 2023
1 parent 6179864 commit 2d1ad16
Show file tree
Hide file tree
Showing 10 changed files with 11,654 additions and 11,998 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ jobs:
- name: Build Admin dashboard UI
run: npm --prefix=./ui ci && npm --prefix=./ui run build

# Similar to the above, the jsvm docs are pregenerated locally
# Similar to the above, the jsvm types are pregenerated locally
# but its here to ensure that it wasn't forgotten to be executed.
- name: Generate jsvm types
run: go run ./plugins/jsvm/internal/docs/docs.go
run: go run ./plugins/jsvm/internal/types/types.go

# The prebuilt golangci-lint doesn't support go 1.18+ yet
# https://github.com/golangci/golangci-lint/issues/2649
Expand Down
12 changes: 6 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@

- **!** Renamed `*Options` to `*Config` for consistency and replaced the unnecessary pointers with their value equivalent to keep the applied configuration defaults isolated within their function calls:
```go
old: pocketbase.NewWithConfig(config *pocketbase.Config)
new: pocketbase.NewWithConfig(config pocketbase.Config)
old: pocketbase.NewWithConfig(config *pocketbase.Config) *pocketbase.PocketBase
new: pocketbase.NewWithConfig(config pocketbase.Config) *pocketbase.PocketBase

old: core.NewBaseApp(config *core.BaseAppConfig)
new: core.NewBaseApp(config core.BaseAppConfig)
old: core.NewBaseApp(config *core.BaseAppConfig) *core.BaseApp
new: core.NewBaseApp(config core.BaseAppConfig) *core.BaseApp

old: apis.Serve(app core.App, options *apis.ServeOptions)
new: apis.Serve(app core.App, config apis.ServeConfig)
old: apis.Serve(app core.App, options *apis.ServeOptions) (*http.Server, error)
new: apis.Serve(app core.App, config apis.ServeConfig) error

old: jsvm.MustRegisterMigrations(app core.App, options *jsvm.MigrationsOptions)
new: jsvm.MustRegister(app core.App, config jsvm.Config)
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ lint:
test:
go test ./... -v --cover

jsvmdocs:
go run ./plugins/jsvm/internal/docs/docs.go
jstypes:
go run ./plugins/jsvm/internal/types/types.go

test-report:
go test ./... -v --cover -coverprofile=coverage.out
Expand Down
2 changes: 1 addition & 1 deletion examples/base/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
app.RootCmd.PersistentFlags().IntVar(
&hooksPool,
"hooksPool",
100,
50,
"the total prewarm goja.Runtime instances for the JS app hooks execution",
)

Expand Down
28 changes: 16 additions & 12 deletions plugins/jsvm/binds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"reflect"
Expand Down Expand Up @@ -104,12 +103,12 @@ func hooksBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
}

func cronBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
jobs := cron.New()
scheduler := cron.New()

loader.Set("cronAdd", func(jobId, cronExpr, handler string) {
pr := goja.MustCompile("", "{("+handler+").apply(undefined)}", true)

err := jobs.Add(jobId, cronExpr, func() {
err := scheduler.Add(jobId, cronExpr, func() {
executors.run(func(executor *goja.Runtime) error {
_, err := executor.RunProgram(pr)
return err
Expand All @@ -120,19 +119,28 @@ func cronBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
}

// start the ticker (if not already)
if jobs.Total() > 0 && !jobs.HasStarted() {
jobs.Start()
if app.IsBootstrapped() && scheduler.Total() > 0 && !scheduler.HasStarted() {
scheduler.Start()
}
})

loader.Set("cronRemove", func(jobId string) {
jobs.Remove(jobId)
scheduler.Remove(jobId)

// stop the ticker if there are no other jobs
if jobs.Total() == 0 {
jobs.Stop()
if scheduler.Total() == 0 {
scheduler.Stop()
}
})

app.OnAfterBootstrap().Add(func(e *core.BootstrapEvent) error {
// start the ticker (if not already)
if scheduler.Total() > 0 && !scheduler.HasStarted() {
scheduler.Start()
}

return nil
})
}

func routerBinds(app core.App, loader *goja.Runtime, executors *vmsPool) {
Expand Down Expand Up @@ -534,10 +542,6 @@ func httpClientBinds(vm *goja.Runtime) {
}
defer res.Body.Close()

if res.StatusCode < 200 || res.StatusCode >= 400 {
return nil, fmt.Errorf("request failed with status %d", res.StatusCode)
}

bodyRaw, _ := io.ReadAll(res.Body)

result := &sendResult{
Expand Down
Loading

0 comments on commit 2d1ad16

Please sign in to comment.