Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support dispatching the runtime events #351

Merged
merged 16 commits into from
Nov 23, 2024

Conversation

nyannyacha
Copy link
Collaborator

@nyannyacha nyannyacha commented May 30, 2024

What kind of change does this PR introduce?

Feature, Enhancement

Description

This PR adds support for event dispatching for the user worker lifecycle.

Type of events supported

  • load
    • It is triggered when the script is loaded and all TLA (Top level await) are resolved
  • beforeunload
    • It is triggered when the resource limit reaches a certain threshold (default value 90%).
    • It is recorded in detail.reason property as to which resource limiter triggered this event.

The threshold for the beforeunload event can be set through the CLI, and the list of flags is as follows:

  • dispatch-beforeunload-wall-clock-ratio (max: 99, default value: 90)
  • dispatch-beforeunload-cpu-ratio (max: 99, default value: 90)
  • dispatch-beforeunload-memory-ratio (max: 99, default value: 90)

The unload event

This event is not triggered at the moment because the trigger point is ambiguous.

Currently, when the resource reaches the limit, the worker is forcibly terminated by the supervisor, so there is no margin to trigger the unload event. To trigger this event, it is necessary to first clarify the point at which it will be triggered.

Sample (beforeunload)

addEventListener("beforeunload", (ev) => {
    console.log(ev);
});

export default {
    fetch() {
        return new Response("meow");
    }
}

Output

// console.log(ev);
CustomEvent {
  bubbles: false,
  cancelable: true,
  composed: false,
  currentTarget: Window {},
  defaultPrevented: false,
  eventPhase: 2,
  srcElement: null,
  target: Window {},
  returnValue: true,
  timeStamp: 0,
  type: "beforeunload",
  detail: { reason: "wall_clock" }
}

@nyannyacha nyannyacha force-pushed the feat-emit-worker-event branch 3 times, most recently from ce0d7f9 to 5ef1122 Compare November 22, 2024 23:44
@nyannyacha nyannyacha marked this pull request as ready for review November 23, 2024 05:14
@nyannyacha nyannyacha enabled auto-merge (squash) November 23, 2024 05:57
@nyannyacha nyannyacha merged commit 997f2d9 into supabase:main Nov 23, 2024
3 checks passed
Copy link

🎉 This PR is included in version 1.63.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

kallebysantos pushed a commit to kallebysantos/edge-runtime that referenced this pull request Nov 23, 2024
* chore: add dependency

* chore: update `Cargo.lock`

* stamp: add a utility function

* stamp: update flags

* stamp: add dependency

* chore: update `Cargo.lock`

* fix: update `bootstrap.js` to expose the event dispatch functions

* stamp: polishing

* feat: support dispatching the runtime events

* stamp: must be used `CallbackScope` instead of `HandleScope` while dispatching event

* stamp: oops

* stamp: use `beforeunload` instead of `willterminate`

* stamp: resolve merge conflicts

* stamp: polishing

* chore: add integration tests

* stamp: make clippy happy
kallebysantos pushed a commit to kallebysantos/edge-runtime that referenced this pull request Nov 23, 2024
* chore: add dependency

* chore: update `Cargo.lock`

* stamp: add a utility function

* stamp: update flags

* stamp: add dependency

* chore: update `Cargo.lock`

* fix: update `bootstrap.js` to expose the event dispatch functions

* stamp: polishing

* feat: support dispatching the runtime events

* stamp: must be used `CallbackScope` instead of `HandleScope` while dispatching event

* stamp: oops

* stamp: use `beforeunload` instead of `willterminate`

* stamp: resolve merge conflicts

* stamp: polishing

* chore: add integration tests

* stamp: make clippy happy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants