From e7e8ca79edddc99c8f80c17afc7f63f261b5810f Mon Sep 17 00:00:00 2001 From: ericxstone Date: Mon, 30 Aug 2021 18:01:38 +0800 Subject: [PATCH 1/4] Add schedule event --- index.d.ts | 1 + lib/cloudworker.js | 18 ++++++++++++++++++ lib/runtime.js | 3 +++ lib/runtime/schedule-event.js | 15 +++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 lib/runtime/schedule-event.js diff --git a/index.d.ts b/index.d.ts index 6ced750..00541f9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -13,6 +13,7 @@ declare interface CloudworkerInit { declare class Cloudworker { constructor (script: string, opts?: CloudworkerInit) + triggerCronJob (): Promise dispatch (request: Request): Promise listen (...args: any[]): http.Server } diff --git a/lib/cloudworker.js b/lib/cloudworker.js index 09a3e26..3c3e67a 100644 --- a/lib/cloudworker.js +++ b/lib/cloudworker.js @@ -27,6 +27,24 @@ class Cloudworker { this._load(workerScript, this.context) } + async triggerCronJob () { + const promise = new Promise((resolve, reject) => { + + setTimeout(() => { + resolve(); + }, 1000) + + const error = async (error) => { + reject(error) + } + + const event = new runtime.ScheduleEvent('scheduled', {scheduledTime: 0}) + event.onError = error + this.dispatcher.emit('scheduled', event) + }) + return promise + } + async dispatch (request) { if (!(request instanceof runtime.Request)) { throw new TypeError('argument must be a Request') diff --git a/lib/runtime.js b/lib/runtime.js index cfb1e0f..f53e6cb 100644 --- a/lib/runtime.js +++ b/lib/runtime.js @@ -2,6 +2,7 @@ const { Request, Response, fetch, Headers, freezeHeaders, bindCfProperty } = req const { URL, URLSearchParams } = require('./runtime/url') const { ReadableStream, WritableStream, TransformStream } = require('./runtime/stream') const { FetchEvent } = require('./runtime/fetch-event') +const { ScheduleEvent } = require('./runtime/schedule-event') const { crypto } = require('./runtime/crypto') const { TextDecoder, TextEncoder } = require('./runtime/text-encoder') const { atob, btoa } = require('./runtime/base64') @@ -23,6 +24,7 @@ class Context { this.WritableStream = WritableStream this.TransformStream = TransformStream this.FetchEvent = FetchEvent + this.ScheduleEvent = ScheduleEvent this.caches = cacheFactory this.crypto = crypto this.TextDecoder = TextDecoder @@ -98,6 +100,7 @@ module.exports = { Context, fetch, FetchEvent, + ScheduleEvent, freezeHeaders, bindCfProperty, Headers, diff --git a/lib/runtime/schedule-event.js b/lib/runtime/schedule-event.js new file mode 100644 index 0000000..eb21ea0 --- /dev/null +++ b/lib/runtime/schedule-event.js @@ -0,0 +1,15 @@ +class ScheduleEvent { + constructor (type, init) { + this.scheduledTime = init.scheduledTime + } + + waitUntil (promise) { + + } + + passThroughOnException () { + + } +} + +module.exports.ScheduleEvent = ScheduleEvent From 038ed5b8bf7fe8b10782bac33be753e360180895 Mon Sep 17 00:00:00 2001 From: ericxstone Date: Mon, 30 Aug 2021 19:30:07 +0800 Subject: [PATCH 2/4] Added test cases on schedule events --- lib/__tests__/cloudworker.test.js | 17 +++++++++++++++++ lib/__tests__/runtime.test.js | 1 + 2 files changed, 18 insertions(+) diff --git a/lib/__tests__/cloudworker.test.js b/lib/__tests__/cloudworker.test.js index 0032933..bdd09d1 100644 --- a/lib/__tests__/cloudworker.test.js +++ b/lib/__tests__/cloudworker.test.js @@ -1,6 +1,7 @@ const Cloudworker = require('../cloudworker') const runtime = require('../runtime') const httpMocks = require('node-mocks-http') +const { KeyValueStore } = require('../kv') const simpleScript = `addEventListener('fetch', event => { event.respondWith(new Response('hello', {status: 200})) }) @@ -165,4 +166,20 @@ describe('cloudworker', () => { expect(res.headers.get('x-colo')).toEqual('YYZ') done() }) + + test('triggerCronJob sends scheduled event to worker', async done => { + const store = new KeyValueStore() + store.put('hello', 'world') + const bindings = {TestNamespace: store} + const scheduleScript = ` + addEventListener('scheduled', async(event) => { + await TestNamespace.put("hello", "test"); + }) + ` + const cw = new Cloudworker(scheduleScript, {bindings}) + await cw.triggerCronJob(); + const valueTest = await store.get('hello', 'text') + expect(valueTest).toEqual("test"); + done(); + }) }) diff --git a/lib/__tests__/runtime.test.js b/lib/__tests__/runtime.test.js index 3de5317..94db2c1 100644 --- a/lib/__tests__/runtime.test.js +++ b/lib/__tests__/runtime.test.js @@ -46,6 +46,7 @@ describe('runtime', () => { 'RegExp', 'Request', 'Response', + 'ScheduleEvent', 'Set', 'SharedArrayBuffer', 'String', From fb7704d886dd06f13153e398bfc9188d67ad0b73 Mon Sep 17 00:00:00 2001 From: ericxstone Date: Tue, 31 Aug 2021 09:55:11 +0800 Subject: [PATCH 3/4] Update lint format --- lib/cloudworker.js | 3 +-- lib/runtime/schedule-event.js | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/cloudworker.js b/lib/cloudworker.js index 3c3e67a..5f5cf6b 100644 --- a/lib/cloudworker.js +++ b/lib/cloudworker.js @@ -29,9 +29,8 @@ class Cloudworker { async triggerCronJob () { const promise = new Promise((resolve, reject) => { - setTimeout(() => { - resolve(); + resolve() }, 1000) const error = async (error) => { diff --git a/lib/runtime/schedule-event.js b/lib/runtime/schedule-event.js index eb21ea0..a54d14c 100644 --- a/lib/runtime/schedule-event.js +++ b/lib/runtime/schedule-event.js @@ -4,7 +4,7 @@ class ScheduleEvent { } waitUntil (promise) { - + } passThroughOnException () { From 9388ef96f2c51957cbd2bc85b7f27c785fe8d615 Mon Sep 17 00:00:00 2001 From: ericxstone Date: Tue, 31 Aug 2021 09:59:37 +0800 Subject: [PATCH 4/4] Update lint formatting in test case --- lib/__tests__/cloudworker.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/__tests__/cloudworker.test.js b/lib/__tests__/cloudworker.test.js index bdd09d1..c00b15b 100644 --- a/lib/__tests__/cloudworker.test.js +++ b/lib/__tests__/cloudworker.test.js @@ -177,9 +177,9 @@ describe('cloudworker', () => { }) ` const cw = new Cloudworker(scheduleScript, {bindings}) - await cw.triggerCronJob(); + await cw.triggerCronJob() const valueTest = await store.get('hello', 'text') - expect(valueTest).toEqual("test"); - done(); + expect(valueTest).toEqual('test') + done() }) })