moar logs

This commit is contained in:
ntr committed 2022-08-30 18:39:22 +10:00
1 parent 70cb07ee27
commit 44c83d3b33
9 files changed
+113 -22

No files matched your search

-2
View File
@@ -69,8 +69,6 @@ model Offer {
total Decimal @db.Money
comparison Decimal @db.Money
sellerId Int
listingId Int
listing Listing @relation(fields: [listingId], references: [id])
+4 -1
View File
@@ -7,7 +7,7 @@ import Bluebird from 'bluebird';
import type Log from 'bunyan';
import type { PrismaClient, Want, User, Listing } from '@prisma/client';
import { logAxiosError } from './utils';
import { logAxiosError, wantDetails } from './utils';
import { Pg } from './pg';
// margh
@@ -39,6 +39,8 @@ export default function createDiscogs(log: Log, pg: Pg) {
const { data: { wants, pagination }} = await api.get(`${apiBase}/users/${username}/wants`);
const releases = await Promise.all(wants.map(want => release(want.id)));
log.info({ wants: wants.length, user: username, userId: user.id }, 'fetched discogs wantlist');
// TODO filter releases to be vinyl format only
// convert want object to actual release and ignore duplicate errors
@@ -83,6 +85,7 @@ export default function createDiscogs(log: Log, pg: Pg) {
// preferentially take listings from sellers if we hit req limits
log.info({ listings: ids.length, ...wantDetails(want)}, 'found listings');
return Bluebird.mapSeries(ids, async id => listing(id, want));
}
+15 -11
View File
@@ -1,9 +1,11 @@
import { EventEmitter } from 'node:events';
import { setTimeout } from 'timers/promises';
import type Log from 'bunyan';
import type { Want } from '@prisma/client';
import { Pg } from './pg';
import { wantDetails } from './utils';
import routerAuth from './routers/router.auth';
export enum Events {
@@ -19,9 +21,10 @@ export default function createEvents(log: Log, pg: Pg, discogs) {
});
events.on(Events.WantsAdded, async function wantsAdded(wants: Want[]) {
log.info({ event: Events.WantsAdded, wants: wants.length });
for (const want of wants) {
const listings = await discogs.listings(want);
log.info({ count: listings.length, title: want.id }, 'listings added')
log.info({ count: listings.length, ...wantDetails(want) }, 'listings added')
}
log.info('finished fetching listings', { wants: wants.length });
@@ -29,20 +32,21 @@ export default function createEvents(log: Log, pg: Pg, discogs) {
events.on(Events.StartupSync, async function wantsAdded() {
const wants = await pg.wantsToSync();
log.info({ event: Events.StartupSync, wants: wants.length });
try {
for (const want of wants) {
for (const want of wants) {
try {
const listings = await discogs.listings(want);
log.info({ count: listings.length, title: want.id }, 'listings added')
}
log.info('finished syncing', { wants: wants.length });
} catch (e) {
if (e.message === 'rate limited') {
log.warn('rate limited, trying again in 60s');
return setTimeout(() => events.emit(Events.StartupSync), 60 * 1000);
log.info({ count: listings.length, ...wantDetails(want) }, 'listings added')
} catch (e) {
if (e.message === 'rate limited') {
log.warn('rate limited, trying again in 60s');
await setTimeout(60 * 1000);
const listings = await discogs.listings(want);
}
}
}
log.info('finished syncing', { wants: wants.length });
});
+13
View File
@@ -1,3 +1,5 @@
import type { Want } from '@prisma/client';
export function logAxiosError(e) {
console.warn({
status: e.response.status,
@@ -11,3 +13,14 @@ export function logAxiosError(e) {
},
});
}
export function wantDetails(want: Want) {
const data = want.data as any;
return {
title: data.title,
release: data.id,
artist: data.artists_sort,
id: want.id
}
}