diff --git a/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/package.json b/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/package.json index a97214214f5..8045bfb0411 100644 --- a/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/package.json +++ b/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/package.json @@ -28,6 +28,7 @@ "knex": "^2.4.2", "moment": "^2.29.4", "morgan": "^1.10.0", + "node-cron": "^3.0.3", "ts-results": "^3.3.0" }, "devDependencies": { @@ -37,6 +38,7 @@ "@types/jest": "^26.0.22", "@types/morgan": "^1.9.2", "@types/node": "^15.12.2", + "@types/node-cron": "^3.0.11", "@types/node-fetch": "^2.6.4", "@types/supertest": "^2.0.11", "esbuild": "^0.14.38", diff --git a/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/app.ts b/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/app.ts index 16fb7839f9d..3aef09c7f21 100644 --- a/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/app.ts +++ b/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/app.ts @@ -11,6 +11,7 @@ import fetch from 'node-fetch' import axios from 'axios' import { WebClient } from '@slack/web-api' import { formatDisbursementTable } from './slack' +import { discoveryDb } from './utils' // TODO: move something like this into App so results are commonplace for handlers export const disburseTrendingRewards = async ( @@ -26,7 +27,7 @@ export const onDisburse = async ( app: App, dryRun: boolean ): Promise> => { - const db = app.getDnDb() + const db = discoveryDb const libs = app.viewAppData().libs const token = process.env.SLACK_BOT_TOKEN if (token === undefined) return new Err('SLACK_BOT_TOKEN undefined') diff --git a/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/config.ts b/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/config.ts index d9fcf28a4c5..5cca309125b 100644 --- a/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/config.ts +++ b/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/config.ts @@ -15,7 +15,11 @@ export type SharedData = { dateToRun: string } -export const initSharedData = async (): Promise> => { +let sharedData: SharedData | undefined = undefined + +export const initSharedData = async (): Promise => { + if (sharedData !== undefined) return sharedData + dotenv.config({ path: './tcr.env' }) const libs = await initAudiusLibs() @@ -33,15 +37,14 @@ export const initSharedData = async (): Promise> => { const dateToRun = process.env.dateToRun if (oracleEthAddress === undefined) - return new Err('oracleEthAddress undefined') - if (AAOEndpoint === undefined) return new Err('AAOEndpoint undefined') + throw new Error('oracleEthAddress undefined') + if (AAOEndpoint === undefined) throw new Error('AAOEndpoint undefined') if (feePayerOverride === undefined) - return new Err('feePayerOverride undefined') - if (localEndpoint === undefined) return new Err('localEndpoint undefined') - if (dateToRun === undefined) return new Err('dateToRun undefined') + throw new Error('feePayerOverride undefined') + if (localEndpoint === undefined) throw new Error('localEndpoint undefined') + if (dateToRun === undefined) throw new Error('dateToRun undefined') - // @ts-ignore - return new Ok({ + sharedData = { oracleEthAddress, AAOEndpoint, feePayerOverride, @@ -49,7 +52,9 @@ export const initSharedData = async (): Promise> => { localEndpoint, dryRun, dateToRun - }) + } + // @ts-ignore + return sharedData } export const condition = (app: App): boolean => { diff --git a/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/main.ts b/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/main.ts index 5b0fd13e1c4..653008675ef 100644 --- a/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/main.ts +++ b/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/main.ts @@ -1,20 +1,26 @@ +import cron from "node-cron" import { App } from '@pedalboard/basekit' -import { SharedData, condition, initSharedData } from './config' +import { SharedData, initSharedData } from './config' import { disburseTrendingRewards } from './app' -import { establishSlackConnection, initSlack } from './slack' +import { establishSlackConnection } from './slack' import { announceTopFiveTrending } from './trending' export const main = async () => { - const dataRes = await initSharedData() - if (dataRes.err) { - console.error('SETUP ERROR = ', dataRes) - return - } - const data = dataRes.unwrap() + const data = await initSharedData() await new App({ appData: data }) - .cron(condition, disburseTrendingRewards) - .cron(condition, announceTopFiveTrending) .task(establishSlackConnection) .run() } + +// Friday at 12:05 pm PST, extra five minutes for trending to calculate +cron.schedule('5 12 * * 5', () => { + initSharedData().then((data) => { + // make new appdata instance to satisfy types + const appData = new App({ appData: data }) + announceTopFiveTrending(appData).catch(e => console.error("Announcement failed: ", e)) + disburseTrendingRewards(appData).catch(e => console.error("Disbursment failed: ", e)) + }) +}, { + timezone: "America/Los_Angeles" +}); diff --git a/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/trending.ts b/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/trending.ts index f3e93dce91d..962a16479d1 100644 --- a/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/trending.ts +++ b/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/trending.ts @@ -1,7 +1,7 @@ import { App } from '@pedalboard/basekit' import { Knex } from 'knex' import { SharedData } from './config' -import { intoResult } from './utils' +import { discoveryDb, identityDb, intoResult } from './utils' import { WebClient } from '@slack/web-api' import moment from 'moment' import { Table, TrendingResults } from '@pedalboard/storage' @@ -21,13 +21,6 @@ export const announceTopFiveTrending = async ( app: App, maybeWeek?: string ) => { - const identityDbRes = await intoResult(async () => app.getIdDb()) - if (identityDbRes.err) { - console.error('Identity connection error: ', identityDbRes) - return - } - const identityDb = identityDbRes.unwrap() - const discoveryDb = app.getDnDb() const week = maybeWeek || moment().format('YYYY-MM-DD') diff --git a/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/utils.ts b/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/utils.ts index 1f747d0a5a5..b2f0435697e 100644 --- a/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/utils.ts +++ b/packages/discovery-provider/plugins/pedalboard/apps/trending-challenge-rewards/src/utils.ts @@ -1,5 +1,9 @@ +import { initializeDiscoveryDb, initializeIdentityDb } from "@pedalboard/basekit"; import { Err, Ok, Result } from "ts-results"; +export const identityDb = initializeIdentityDb() +export const discoveryDb = initializeDiscoveryDb() + /// catches possibles errors and converts them to an appropriate result type /// this is useful when using a library that may throw and you want to catch it safely export const intoResult = async (