From 24f6e61dd6f70134113356c39bc7264054d3951d Mon Sep 17 00:00:00 2001 From: Tree Date: Wed, 12 Jul 2023 16:42:35 +0000 Subject: [PATCH] bgs tickers --- .env.example | 10 +++++++- Makefile | 5 +++- backend/lib/interval.js | 29 +++++++++++++++++++++++ backend/tick.js | 52 +++++++++++++++++++++++++++++++++++++++++ pm2.config.js | 15 +++++++++++- 5 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 backend/lib/interval.js create mode 100644 backend/tick.js diff --git a/.env.example b/.env.example index 63068cb..afd01fd 100644 --- a/.env.example +++ b/.env.example @@ -12,4 +12,12 @@ INFLUXDB_TOKEN=XXXX INFLUXDB_ORG=XXXX INFLUXDB_BUCKET=XXXX TYPESENSE_HOST=http://localhost:8108 -TYPESENSE_API_KEY=XXXXX \ No newline at end of file +TYPESENSE_API_KEY=XXXXX +ATSCAN_TICK_SANDBOX_HANDLE=tick-sandbox.atscan.net +ATSCAN_TICK_SANDBOX_PASSWORD=XXXXXX +ATSCAN_TICK_BLUESKY_HANDLE=tick.bsky.social +ATSCAN_TICK_BLUESKY_PASSWORD=XXXXXX +ATSCAN_TICK_BLUESKY_PDS=bsky.social +ATSCAN_TICK_BLUESKY_REPO=did:plc:kwmcvt4maab47n7dgvepg4tr +ATSACN_TICK_BLUESKY_POST=3k2djecjpk22c +ATSCAN_TICK_BLUESKY_INTERVAL=5000 \ No newline at end of file diff --git a/Makefile b/Makefile index 2cb8237..0447644 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ plc-crawl: deno run --unstable --allow-net --allow-read --allow-env --allow-sys ./backend/plc-crawler.js index: - deno run --unstable --allow-net --allow-read --allow-env --allow-sys ./backend/indexer.js + deno run --unstable --allow-net --allow-read --allow-env --allow-sys --allow-ffi ./backend/indexer.js index-daemon: deno run --unstable --allow-net --allow-read --allow-env --allow-sys ./backend/indexer.js daemon @@ -29,6 +29,9 @@ repo-worker: firehose: deno run --unstable --allow-net --allow-read --allow-env --allow-sys --allow-ffi ./backend/firehose.js +tick: + deno run --unstable --allow-net --allow-read --allow-env ./backend/tick.js + fe-rebuild: cd frontend && npm run build rm -rf frontend/prod-build diff --git a/backend/lib/interval.js b/backend/lib/interval.js new file mode 100644 index 0000000..5e92ee4 --- /dev/null +++ b/backend/lib/interval.js @@ -0,0 +1,29 @@ +export function Interval(fn, duration, ...args) { + const _this = this; + this.baseline = undefined; + + this.run = function (flag) { + if (_this.baseline === undefined) { + _this.baseline = performance.now() - duration; + } + if (flag) { + fn(...args); + } + const end = performance.now(); + _this.baseline += duration; + + let nextTick = duration - (end - _this.baseline); + if (nextTick < 0) { + nextTick = 0; + } + + //console.log(nextTick); + _this.timer = setTimeout(function () { + _this.run(true); + }, nextTick); + }; + + this.stop = function () { + clearTimeout(_this.timer); + }; +} diff --git a/backend/tick.js b/backend/tick.js new file mode 100644 index 0000000..4102c3e --- /dev/null +++ b/backend/tick.js @@ -0,0 +1,52 @@ +import * as atprotoApi from "npm:@atproto/api"; +import "https://deno.land/std@0.193.0/dotenv/load.ts"; +import { Interval } from "./lib/interval.js"; + +const ENV = Deno.env.get("ATSCAN_TICK_ENV") || "SANDBOX"; +const options = { + identifier: Deno.env.get(`ATSCAN_TICK_${ENV}_HANDLE`), + password: Deno.env.get(`ATSCAN_TICK_${ENV}_PASSWORD`), +}; +const TICK_INTERVAL = parseInt( + Deno.env.get(`ATSCAN_TICK_${ENV}_INTERVAL`) || 1000, +); +const TICK_PDS = Deno.env.get(`ATSCAN_TICK_${ENV}_PDS`) || "test-pds.gwei.cz"; +const TICK_REPO = Deno.env.get(`ATSCAN_TICK_${ENV}_REPO`) || + "did:plc:pzovq4a22hpji6pfzofgk7gc"; +//const TICK_POST = Deno.env.get(`ATSCAN_TICK_${ENV}_POST`) || '3k2bwjgozws2q'; + +const agent = new atprotoApi.default.BskyAgent({ + service: `https://${TICK_PDS}`, +}); +await agent.login(options); +console.log(`Logged in as "${options.identifier}" [pds=${TICK_PDS}] ...`); +console.log(`Ticking in interval: ${TICK_INTERVAL}ms ..`); +const repo = TICK_REPO; +//const rkey = TICK_POST; + +/*const resp = await agent.getTimeline({ limit: 100 }); +for (const { post } of resp.data.feed) { + if (post.author.did === repo) { + await agent.deletePost(post.uri) + } +}*/ + +const interval = new Interval(async () => { + const time = (new Date()).toISOString(); + await agent.com.atproto.repo.createRecord({ + repo, + collection: "app.bsky.feed.post", + //rkey, + record: { + $type: "app.bsky.feed.post", + text: time, + createdAt: time, + }, + }); + /*await agent.app.bsky.feed.post.delete({ + repo, + rkey, + })*/ +}, TICK_INTERVAL); + +interval.run(); diff --git a/pm2.config.js b/pm2.config.js index c917bb8..881f9d4 100644 --- a/pm2.config.js +++ b/pm2.config.js @@ -68,7 +68,20 @@ module.exports = { script: "./backend/repo-worker.js", interpreter: "mullvad-exclude", interpreterArgs: "deno run --unstable --allow-net --allow-read --allow-write --allow-env --allow-ffi --allow-sys ./backend/repo-worker.js", - instances: 4, + instances: 6, + }, { + name: "atscan-tick-sandbox", + script: "./backend/tick.js", + interpreter: "deno", + interpreterArgs: "run --unstable --allow-net --allow-read --allow-env" + }, { + name: "atscan-tick-bluesky", + script: "./backend/tick.js", + interpreter: "deno", + interpreterArgs: "run --unstable --allow-net --allow-read --allow-env", + env: { + ATSCAN_TICK_ENV: 'BLUESKY' + } }, { name: "bull-ui", script: "index.js",