Skip to content

Commit

Permalink
scenarios and syncSubs to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
howardchung committed Dec 2, 2023
1 parent 5a19565 commit caa42b7
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 43 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if (process.env.PROVIDER === 'gce') {
}
if (process.env.ROLE) {
// if role variable is set just run that script
const app = apps.find(app => app.group === process.env.ROLE);
const app = apps.find((app) => app.group === process.env.ROLE);
import('./' + app?.script);
} else if (group) {
pm2.connect(() => {
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
"instances": 1
},
{
"script": "svc/scenarios.js",
"script": "svc/scenarios.mjs",
"watch": true,
"ignore_watch": [".git", "node_modules"],
"group": "backend",
Expand Down Expand Up @@ -225,7 +225,7 @@
"instances": 1
},
{
"script": "svc/syncSubs.js",
"script": "svc/syncSubs.mjs",
"watch": true,
"ignore_watch": [".git", "node_modules"],
"group": "backend",
Expand Down
10 changes: 6 additions & 4 deletions svc/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ function cassandraUsage(cb) {
async function redisUsage(cb) {
try {
const info = await redis.info();
const line = info.split('\n').find(line => line.startsWith('used_memory'));
const line = info
.split('\n')
.find((line) => line.startsWith('used_memory'));
return cb(null, {
metric: Number(line.split(':')[1]),
threshold: 4 * 10 ** 9,
});
} catch(e) {
cb(e);
}
} catch (e) {
cb(e);
}
}
const health = {
steamApi,
Expand Down
8 changes: 4 additions & 4 deletions svc/parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ async function runParse(match, url) {
url = `https://odota.github.io/testfiles/${match.match_id}_1.dem`;
}
console.log(new Date(), url);
const {stdout} = await execPromise(
const { stdout } = await execPromise(
`curl --max-time 180 --fail ${url} | ${
url && url.slice(-3) === 'bz2' ? 'bunzip2' : 'cat'
} | curl -X POST -T - ${
PARSER_HOST
} | node processors/createParsedDataBlob.js ${match.match_id}`,
} | curl -X POST -T - ${PARSER_HOST} | node processors/createParsedDataBlob.js ${
match.match_id
}`,
{ shell: true, maxBuffer: 10 * 1024 * 1024 }
);
const result = { ...JSON.parse(stdout), ...match };
Expand Down
2 changes: 1 addition & 1 deletion svc/proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* */
import httpProxy from 'http-proxy';
import http from 'http';
import {PORT, PROXY_PORT} from '../config.js';
import { PORT, PROXY_PORT } from '../config.js';

const PORT = PORT || PROXY_PORT;
const proxy = httpProxy.createProxyServer({
Expand Down
16 changes: 7 additions & 9 deletions svc/scenarios.js → svc/scenarios.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const async = require('async');
const util = require('util');
const queue = require('../store/queue');
const buildMatch = require('../store/buildMatch');
const db = require('../store/db');
const utility = require('../util/utility');
const su = require('../util/scenariosUtil');

import async from 'async';
import util from 'util';
import queue from '../store/queue.js';
import buildMatch from '../store/buildMatch.js';
import db from '../store/db.js';
import utility from '../util/utility.js';
import su from '../util/scenariosUtil.js';
async function processScenarios(matchID, cb) {
try {
const match = await buildMatch(matchID);
Expand Down Expand Up @@ -48,5 +47,4 @@ async function processScenarios(matchID, cb) {
return cb(err);
}
}

queue.runQueue('scenariosQueue', 1, processScenarios);
15 changes: 4 additions & 11 deletions svc/syncSubs.js → svc/syncSubs.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
/**
* Function to sync subs between Stripe and DB
* */
const db = require('../store/db');
const utility = require('../util/utility');
const config = require('../config');
const stripeLib = require('stripe');

import db from '../store/db.js';
import utility from '../util/utility.js';
import config from '../config.js';
import stripeLib from 'stripe';
const stripe = stripeLib(config.STRIPE_SECRET);

const { invokeInterval } = utility;

async function run(cb) {
// Get list of current subscribers
const result = [];
Expand All @@ -34,5 +28,4 @@ async function run(cb) {
}
await db.raw('COMMIT');
}

invokeInterval(run, 60 * 1000);
5 changes: 3 additions & 2 deletions svc/web.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ app.use((req, res, cb) => {
}
const command = redis.multi();

command.hincrby('rate_limit', res.locals.usageIdentifier, pathCosts[req.path] || 1)
command
.hincrby('rate_limit', res.locals.usageIdentifier, pathCosts[req.path] || 1)
.expireat('rate_limit', utility.getStartOfBlockMinutes(1, 1));

if (!res.locals.isAPIRequest) {
Expand Down Expand Up @@ -401,4 +402,4 @@ function gracefulShutdown() {
process.once('SIGTERM', gracefulShutdown);
// listen for INT signal e.g. Ctrl-C
process.once('SIGINT', gracefulShutdown);
export default app;
export default app;
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ async function startServices(cb) {
try {
app = (await import('../svc/web.mjs')).default;
await import('../svc/parser.mjs');
} catch(e) {
} catch (e) {
console.log(e);
}
cb();
Expand Down
15 changes: 7 additions & 8 deletions util/getGcData.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ async function getGcDataFromRetriever(match) {
const urls = retrieverArr.map(
(r) => `http://${r}?key=${secret}&match_id=${match.match_id}`
);
const body = await getDataPromise({ url: urls, noRetry: match.noRetry, timeout: 5000 });
if (
!body ||
!body.match ||
!body.match.replay_salt ||
!body.match.players
) {
const body = await getDataPromise({
url: urls,
noRetry: match.noRetry,
timeout: 5000,
});
if (!body || !body.match || !body.match.replay_salt || !body.match.players) {
// non-retryable error
// redis.lpush('nonRetryable', JSON.stringify({ matchId: match.match_id, body }));
// redis.ltrim('nonRetryable', 0, 10000);
Expand All @@ -40,7 +39,7 @@ async function getGcDataFromRetriever(match) {
);
// TODO (howard) add discovered account_ids to database and fetch account data/rank medal
// NOTE: extra player fields won't be set on repeated parses unless we manually delete match_gcdata row
// if someone requests a reparse we won't have this data
// if someone requests a reparse we won't have this data
const players = body.match.players.map((p, i) => ({
player_slot: p.player_slot,
party_id: p.party_id?.low,
Expand Down

0 comments on commit caa42b7

Please sign in to comment.