diff --git a/cache.js b/cache.js index 039666e..076fc97 100644 --- a/cache.js +++ b/cache.js @@ -1,12 +1,19 @@ import {createClient} from "redis"; -export const redis = createClient({ - url: process.env.DEXORDER_REDIS_URL || 'redis://localhost:6379', - returnBuffers: false, -}) -redis.on('error', (err) => console.log('Redis Client Error', err)); -await redis.connect(); +async function createRedisClient() { + const client = createClient({ + url: process.env.DEXORDER_REDIS_URL || 'redis://localhost:6379', + returnBuffers: false, + }) + client.on('error', (err) => console.log('Redis Client Error', err)); + await client.connect(); + return client +} + + +export const redis = await createRedisClient() +export const redisSubscriber = await createRedisClient() export class CacheSet { diff --git a/chain.js b/chain.js index cabfd0b..4db2385 100644 --- a/chain.js +++ b/chain.js @@ -5,11 +5,15 @@ export const chainInfo = JSON.parse(fs.readFileSync('../contract/version.json')) console.log('chainInfo', chainInfo) export async function joinChain( socket, chainId ) { - if (socket.chainId) - socket.leave(socket.chainId) - socket.join(chainId) - socket.chainId = chainId - const items = await marks.items(chainId); - for (let [token,mark] of items) - socket.emit('mark.usd', chainId, token, mark) + try { + if (socket.chainId) + socket.leave(socket.chainId) + socket.join(chainId) + socket.chainId = chainId + const items = await marks.items(chainId); + for (let [token, mark] of items) + socket.emit('mark.usd', chainId, token, mark) + } catch (e) { + console.error('joinChain', e) + } } diff --git a/faucet.js b/faucet.js index ddc5838..c09c749 100644 --- a/faucet.js +++ b/faucet.js @@ -6,6 +6,16 @@ import {metadata} from "./metadata.js"; export async function gib( chainId, owner, vault, tokenAmounts ) { + try { + return await doGib( chainId, owner, vault, tokenAmounts ) + } + catch (e) { + console.error('gib failed', e) + } +} + + +export async function doGib( chainId, owner, vault, tokenAmounts ) { if (!owner || !vault) return if (chainId === 421614) { // Arbitrum-Sepolia diff --git a/init.js b/init.js new file mode 100644 index 0000000..756fe6d --- /dev/null +++ b/init.js @@ -0,0 +1,11 @@ +export function initLog(app) { + process.on('uncaughtException', (err) => { + console.error('Uncaught Exception:', err); + process.exit(1); // Exit with code 1 + }); + + process.on('unhandledRejection', (reason, promise) => { + console.error('Unhandled Rejection at:', promise, 'reason:', reason); + process.exit(1); // Exit with code 1 + }); +} diff --git a/io.js b/io.js index 27e00fe..d343536 100644 --- a/io.js +++ b/io.js @@ -1,7 +1,7 @@ import {createServer} from "http"; import {Server} from "socket.io"; import { createAdapter } from "@socket.io/redis-adapter"; -import {redis} from "./cache.js"; +import {redis, redisSubscriber} from "./cache.js"; import {fileURLToPath} from "url"; import path from "path"; import express from "express"; @@ -29,12 +29,14 @@ app.set('views', path.join(__dirname, 'views')); // Set the views directory app.use(express.static(path.join(__dirname, 'public'))); app.use(cors()) +app.use((err, req, res, next) => { + console.error(err.stack); + res.status(500).send('Something went wrong!'); +}); initSnapShare(app) export const httpServer = createServer(app) export const io = new Server(httpServer, socketIoOptions) -const pubClient = redis.duplicate(); -await pubClient.connect() -const adapter = createAdapter(pubClient, redis, {/*key:'socket.io'*/}) +const adapter = createAdapter(redis, redisSubscriber, {/*key:'socket.io'*/}) io.adapter(adapter) diff --git a/main.js b/main.js index 3aa21cd..47d36e4 100644 --- a/main.js +++ b/main.js @@ -1,8 +1,10 @@ import 'dotenv/config' import {httpServer} from "./io.js"; import {initIO} from "./route.js"; +import {initLog} from "./init.js"; +initLog(); initIO(); const port = parseInt(process.env.DEXORDER_PORT) || 3001; diff --git a/pool.js b/pool.js index 0ced68b..110af65 100644 --- a/pool.js +++ b/pool.js @@ -19,17 +19,22 @@ export function unsubPools( socket, chainId, addresses ) { export async function subOHLCs( socket, chainId, poolPeriods) { - console.log('subOHLCs', chainId, poolPeriods) - for(const key of poolPeriods) { - const room = `${chainId}|${key}`; - socket.join(room) - let ohlc = await ohlcs.get(chainId,key); - console.log('got ohlc', ohlc) - if (typeof(ohlc)==='string') { - ohlc = JSON.parse(ohlc) + try { + console.log('subOHLCs', chainId, poolPeriods) + for (const key of poolPeriods) { + const room = `${chainId}|${key}`; + socket.join(room) + let ohlc = await ohlcs.get(chainId, key); + console.log('got ohlc', ohlc) + if (typeof (ohlc) === 'string') { + ohlc = JSON.parse(ohlc) + } + socket.emit('ohlc', chainId, key, ohlc) + console.log('joined room', room) } - socket.emit('ohlc', chainId, key, ohlc) - console.log('joined room', room) + } + catch (e) { + console.error('subOHLCs', e) } }