Skip to content

Conversation

@nyannyacha
Copy link
Contributor

@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 from cddcac1 to f9aab1d Compare June 3, 2024 05:37
@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 force-pushed the feat-emit-worker-event branch from 5ef1122 to 399fd97 Compare November 23, 2024 01:17
@nyannyacha nyannyacha marked this pull request as ready for review November 23, 2024 05:14
@nyannyacha nyannyacha requested a review from laktek November 23, 2024 05:56
@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
@github-actions
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