From 14cae298bc06151427bef34cfbcbfa4a1a5188a1 Mon Sep 17 00:00:00 2001 From: komal-rs Date: Fri, 13 Dec 2024 17:39:19 +0530 Subject: [PATCH 1/2] global feed cache periodic job --- .github/workflows/deploy-workers.yml | 2 +- Cargo.lock | 11 +++++++++ Cargo.toml | 2 +- package.json | 3 ++- workers/global-feed-cache-job/Cargo.toml | 23 ++++++++++++++++++ workers/global-feed-cache-job/package.json | 10 ++++++++ workers/global-feed-cache-job/src/lib.rs | 26 +++++++++++++++++++++ workers/global-feed-cache-job/wrangler.toml | 16 +++++++++++++ 8 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 workers/global-feed-cache-job/Cargo.toml create mode 100644 workers/global-feed-cache-job/package.json create mode 100644 workers/global-feed-cache-job/src/lib.rs create mode 100644 workers/global-feed-cache-job/wrangler.toml diff --git a/.github/workflows/deploy-workers.yml b/.github/workflows/deploy-workers.yml index d258635..850738b 100644 --- a/.github/workflows/deploy-workers.yml +++ b/.github/workflows/deploy-workers.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - worker: [yral-ml-feed-cache] # add more workers here + worker: [global-feed-cache-job] # add more workers here defaults: run: working-directory: workers/${{ matrix.worker }} diff --git a/Cargo.lock b/Cargo.lock index fd96e3a..e7afc6f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -643,6 +643,17 @@ version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" +[[package]] +name = "global-feed-cache-job" +version = "0.1.0" +dependencies = [ + "cfg-if 0.1.10", + "console_error_panic_hook", + "serde", + "serde_json", + "worker", +] + [[package]] name = "group" version = "0.13.0" diff --git a/Cargo.toml b/Cargo.toml index db29bd3..bdfa43c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = [ "workers/sample-worker", "workers/yral-ml-feed-cache","workers/yral-onchain-backend"] +members = [ "workers/global-feed-cache-job", "workers/sample-worker", "workers/yral-ml-feed-cache","workers/yral-onchain-backend"] [workspace.dependencies] worker = { version = "0.4.2" } diff --git a/package.json b/package.json index ade18fd..f18a6ea 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "test": "npm run test --workspaces", "dev:sample-worker": "cd workers/sample-worker && npm run dev", "deploy:sample-worker": "cd workers/sample-worker && npm run deploy", - "dev:yral-ml-feed-cache": "cd workers/yral-ml-feed-cache && npm run dev" + "dev:yral-ml-feed-cache": "cd workers/yral-ml-feed-cache && npm run dev", + "dev:global-feed-cache-job": "cd workers/global-feed-cache-job && npm run dev" }, "devDependencies": { "wrangler": "^3.93.0" diff --git a/workers/global-feed-cache-job/Cargo.toml b/workers/global-feed-cache-job/Cargo.toml new file mode 100644 index 0000000..3b59319 --- /dev/null +++ b/workers/global-feed-cache-job/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "global-feed-cache-job" +version = "0.1.0" +edition = "2021" + +[package.metadata.wasm-pack.profile.release] +wasm-opt = false + +[lib] +crate-type = ["cdylib"] + +[dependencies] +worker.workspace = true +serde.workspace = true +serde_json.workspace = true +cfg-if = "0.1.2" +console_error_panic_hook.workspace = true + + +[profile.release] +lto = true +strip = true +codegen-units = 1 diff --git a/workers/global-feed-cache-job/package.json b/workers/global-feed-cache-job/package.json new file mode 100644 index 0000000..32238a6 --- /dev/null +++ b/workers/global-feed-cache-job/package.json @@ -0,0 +1,10 @@ +{ + "name": "global-feed-cache-job", + "version": "0.1.0", + "private": true, + "scripts": { + "dev": "wrangler dev --local --test-scheduled", + "dev:remote": "wrangler dev --remote", + "deploy": "wrangler deploy" + } + } \ No newline at end of file diff --git a/workers/global-feed-cache-job/src/lib.rs b/workers/global-feed-cache-job/src/lib.rs new file mode 100644 index 0000000..a433913 --- /dev/null +++ b/workers/global-feed-cache-job/src/lib.rs @@ -0,0 +1,26 @@ +pub use console_error_panic_hook::set_once as set_panic_hook; +use worker::*; + +#[event(scheduled)] +pub async fn scheduled_event(_event: ScheduledEvent, env: Env, _ctx: ScheduleContext) { + console_log!("Hello from a scheduled event!"); + set_panic_hook(); + + // Create a new request + let req = Request::new( + "https://icp-off-chain-agent.fly.dev/update-global-ml-feed-cache", + Method::Get, + ) + .unwrap(); + + // Make the HTTP request using Fetch API + match Fetch::Request(req).send().await { + Ok(resp) => { + // Log the response status + console_log!("Request completed with status: {}", resp.status_code()); + } + Err(e) => { + console_error!("Request failed: {}", e); + } + } +} diff --git a/workers/global-feed-cache-job/wrangler.toml b/workers/global-feed-cache-job/wrangler.toml new file mode 100644 index 0000000..39e9ee3 --- /dev/null +++ b/workers/global-feed-cache-job/wrangler.toml @@ -0,0 +1,16 @@ +name = "global-feed-cache-job" +main = "build/worker/shim.mjs" +compatibility_date = "2024-01-01" + +[build] +command = "cargo install -q worker-build && worker-build --release" + +[vars] +ENVIRONMENT = "production" + +[env.development] +vars = { ENVIRONMENT = "development" } + + +[triggers] +crons = ["*/5 * * * *"] From f9872bb735a32be580cb79aad1cff2122fab02f1 Mon Sep 17 00:00:00 2001 From: komal-rs Date: Fri, 13 Dec 2024 17:40:09 +0530 Subject: [PATCH 2/2] cron every hour --- workers/global-feed-cache-job/wrangler.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workers/global-feed-cache-job/wrangler.toml b/workers/global-feed-cache-job/wrangler.toml index 39e9ee3..9a2e308 100644 --- a/workers/global-feed-cache-job/wrangler.toml +++ b/workers/global-feed-cache-job/wrangler.toml @@ -13,4 +13,4 @@ vars = { ENVIRONMENT = "development" } [triggers] -crons = ["*/5 * * * *"] +crons = ["0 * * * *"]