sync constantly
This commit is contained in:
1 parent
c6bf50f963
commit
36fc3665dd
5 files changed
+37
-16
No files matched your search
@@ -31,6 +31,8 @@ async function start() {
|
||||
|
||||
await notify.listen();
|
||||
|
||||
events.emit(Events.Sync);
|
||||
|
||||
api.listen();
|
||||
}
|
||||
|
||||
|
||||
@@ -42,14 +42,6 @@ export default function createDiscogs(log: Log, pg: Pg) {
|
||||
|
||||
api.interceptors.response.use(onSuccess, onError);
|
||||
|
||||
async function handleApiError(e) {
|
||||
logAxiosError(e);
|
||||
if (e.response.status === 429) {
|
||||
throw new Error('rate limited');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function wantListSync(username: string, user: User): Promise<Want[]> {
|
||||
const { data: { wants, pagination }} = await api.get(`${apiBase}/users/${username}/wants`);
|
||||
const releases = await Promise.all(wants.map(want => release(want.id)));
|
||||
|
||||
+17
-5
@@ -30,9 +30,10 @@ export default function createEvents(log: Log, pg: Pg, discogs, offers) {
|
||||
async function populateListings(want: Want) {
|
||||
try {
|
||||
const listings = await discogs.listings(want);
|
||||
await pg.wantSynced(want);
|
||||
return listings;
|
||||
} catch (e) {
|
||||
if (e.message === 'rate limited') {
|
||||
if (e.message.includes('rate limited')) {
|
||||
log.warn('rate limited, trying again in 60s');
|
||||
await setTimeout(60 * 1000);
|
||||
return populateListings(want);
|
||||
@@ -46,7 +47,6 @@ export default function createEvents(log: Log, pg: Pg, discogs, offers) {
|
||||
const listings = await populateListings(want);
|
||||
log.info({ listings: listings.length, ...wantDetails(want) }, 'listings found');
|
||||
await offers.generate();
|
||||
log.info('offers updated');
|
||||
await notifyUser(want.userId);
|
||||
}
|
||||
|
||||
@@ -67,9 +67,21 @@ export default function createEvents(log: Log, pg: Pg, discogs, offers) {
|
||||
});
|
||||
|
||||
events.on(Events.Sync, async function sync() {
|
||||
const wants = await pg.wantsToSync();
|
||||
log.info({ event: Events.Sync, wants: wants.length });
|
||||
await updateWantListings(wants);
|
||||
try {
|
||||
const wants = await pg.wantsToSync();
|
||||
log.info({ event: Events.Sync, wants: wants.length });
|
||||
await updateWantListings(wants);
|
||||
|
||||
if (wants.length === 0) {
|
||||
log.info('all listings up to date');
|
||||
await setTimeout(60 * 1000)
|
||||
}
|
||||
} catch (e) {
|
||||
log.error(e, 'sync error')
|
||||
}
|
||||
|
||||
// forever
|
||||
events.emit(Events.Sync);
|
||||
});
|
||||
|
||||
// events.on(Events.ListingsUpdated, async function listingsUpdated() {
|
||||
|
||||
+17
-3
@@ -5,6 +5,7 @@ import * as Bluebird from 'bluebird';
|
||||
import _ from 'lodash';
|
||||
|
||||
import { Status } from './offers';
|
||||
import { wantDetails } from './utils';
|
||||
|
||||
export interface UserCreateParams {
|
||||
email: string,
|
||||
@@ -112,7 +113,12 @@ export default function createPg(log: Log, prisma: PrismaClient) {
|
||||
}
|
||||
|
||||
async function wantsToSync() {
|
||||
return prisma.want.findMany({});
|
||||
return prisma.want.findMany({
|
||||
orderBy: { updatedAt: 'asc' },
|
||||
where: {
|
||||
updatedAt: { lte: new Date(Date.now() - 1000 * 60 * 15) }
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function wantRm(id: UUID) {
|
||||
@@ -133,6 +139,13 @@ export default function createPg(log: Log, prisma: PrismaClient) {
|
||||
});
|
||||
}
|
||||
|
||||
async function wantSynced(want: Want) {
|
||||
return prisma.want.update({
|
||||
where: { id: want.id },
|
||||
data: { updatedAt: new Date() },
|
||||
});
|
||||
}
|
||||
|
||||
async function listingRmUnlisted(want: Want, currentListingIds: DiscogsListing[]) {
|
||||
return prisma.$transaction(async (prisma) => {
|
||||
log.debug({ currentListingIds });
|
||||
@@ -153,7 +166,7 @@ export default function createPg(log: Log, prisma: PrismaClient) {
|
||||
}
|
||||
});
|
||||
|
||||
if (offersRemoved > 0) log.info({ offersRemoved }, 'pending offers removed');
|
||||
if (offersRemoved > 0) log.info({ offersRemoved, ...wantDetails(want) }, 'pending offers removed');
|
||||
|
||||
// now we have to filter listingIds with confirmed offers
|
||||
// and get their listingIds to filter out
|
||||
@@ -180,7 +193,7 @@ export default function createPg(log: Log, prisma: PrismaClient) {
|
||||
},
|
||||
});
|
||||
|
||||
log.info({ count }, 'second stage open offers removed');
|
||||
if (count) log.info({ count }, 'second stage open offers removed');
|
||||
|
||||
return count;
|
||||
});
|
||||
@@ -305,6 +318,7 @@ export default function createPg(log: Log, prisma: PrismaClient) {
|
||||
wantCreate,
|
||||
wantList,
|
||||
wantsToSync,
|
||||
wantSynced,
|
||||
wantRm,
|
||||
|
||||
listingUpsert,
|
||||
|
||||
Reference in new issue
Block a user