-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
Fetcher#scheduled()
for dispatching scheduled
events
Like `Fetcher#queue()`, this is gated behind the experimental `service_binding_extra_handlers` compatibility flag. Co-authored-by: jjohnson <jjohnson@cloudflare.com>
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) 2023 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
|
||
import assert from "node:assert"; | ||
|
||
let scheduledLastCtrl; | ||
|
||
export default { | ||
async scheduled(ctrl, env, ctx) { | ||
scheduledLastCtrl = ctrl; | ||
if (ctrl.cron === "* * * * 30") ctrl.noRetry(); | ||
}, | ||
|
||
async test(ctrl, env, ctx) { | ||
// Call `scheduled()` with no options | ||
{ | ||
const result = await env.SERVICE.scheduled(); | ||
assert.strictEqual(result.outcome, /* OK */ 1); | ||
assert(!result.noRetry); | ||
assert(Math.abs(Date.now() - scheduledLastCtrl.scheduledTime) < 3_000); | ||
assert.strictEqual(scheduledLastCtrl.cron, ""); | ||
} | ||
|
||
// Call `scheduled()` with options, and noRetry() | ||
{ | ||
const result = await env.SERVICE.scheduled({ scheduledTime: 1000, cron: "* * * * 30" }); | ||
assert.strictEqual(result.outcome, /* OK */ 1); | ||
assert(result.noRetry); | ||
assert.strictEqual(scheduledLastCtrl.scheduledTime, 1000); | ||
assert.strictEqual(scheduledLastCtrl.cron, "* * * * 30"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Workerd = import "/workerd/workerd.capnp"; | ||
|
||
const unitTests :Workerd.Config = ( | ||
services = [ | ||
( name = "http-test", | ||
worker = ( | ||
modules = [ | ||
( name = "worker", esModule = embed "http-test.js" ) | ||
], | ||
bindings = [ | ||
( name = "SERVICE", service = "http-test" ) | ||
], | ||
compatibilityDate = "2023-08-01", | ||
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers"], | ||
) | ||
), | ||
], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters