DB bindings work for `fetch` but do not work for `scheduled`

For Workes & Pages, what is the name of the domain?

example.com

What is the error number?

None

What is the error message?

None

What is the issue or error you’re encountering

I would like to have the same worker triggered by cron and HTTP fetch events. However, the workers exits silently while awaiting DB query results. I’ve wrapped the await query in a try/catch but it doesn’t catch an error and no further processing is performed by the worker.

What are the steps to reproduce the issue?

  • Create a worker that extends WorkerEntrypoint
  • implement a schedule function that queries the D1 this.env.DB binding
  • implement a fetch function that queries the D1 this.env.DB binding
  • notice the cron trigger fires but the worker exits while trying to execute query
  • notice the fetch trigger works and the query results are available to the worker

Example worker code:

export default class extends WorkerEntrypoint {

  async fetch(request) {
    // results are returned
    const { results } = await this.env.DB.prepare('SELECT * FROM table').all();
    return new Response(`ok`);
  }

  async scheduled(event) {
    try {
      // no results returned, worker exits
      const { results } = await this.env.DB.prepare('SELECT * FROM table').all();
      // this does not execute
      console.log("got results");
    } catch (err) {
      // this does not execute either
      console.log(err)
    }
  }
}

I wrote a simplified worker setup and couldn’t reproduce the issue. In the original code (which was more complex than the above) the issue was due to me omitting an await :see_no_evil: