Fixed caching bugs

This commit is contained in:
Mr. Stallion 2021-09-10 18:02:50 -05:00
parent 8ddb2fe708
commit 73c8cf217b
7 changed files with 54 additions and 9 deletions

View File

@ -255,7 +255,7 @@ export default class CharacterPreview extends Vue {
this.age = a.age ? this.readable(`${a.age}`) : (rawAge && /[0-9]/.test(rawAge.string || '') && rawAge.string) || undefined;
this.species = a.species ? this.readable(Species[a.species]) : (rawSpecies && rawSpecies.string) || undefined;
this.gender = a.gender ? this.readable(Gender[a.gender]) : undefined;
this.gender = (a.gender && a.gender !== Gender.None) ? this.readable(Gender[a.gender]) : undefined;
this.furryPref = a.furryPreference ? this.readable(furryPreferenceMapping[a.furryPreference]) : undefined;
this.subDomRole = a.subDomRole ? this.readable(SubDomRole[a.subDomRole]) : undefined;
this.sexualOrientation = a.orientation ? this.readable(Orientation[a.orientation]) : undefined;

View File

@ -184,6 +184,7 @@ export default class Connection implements Interfaces.Connection {
'api.query.start',
{
endpoint,
data,
character: core.characters.ownCharacter?.name,
deltaToLastApiCall: Date.now() - lastFetch,
deltaToLastApiTicket: Date.now() - lastApiTicketFetch
@ -218,6 +219,7 @@ export default class Connection implements Interfaces.Connection {
{
error: res.error,
endpoint,
data,
character: core.characters.ownCharacter?.name,
deltaToLastApiCall: Date.now() - lastFetch,
deltaToLastApiTicket: Date.now() - lastApiTicketFetch
@ -233,6 +235,7 @@ export default class Connection implements Interfaces.Connection {
'api.query.success',
{
endpoint,
data,
character: core.characters.ownCharacter?.name,
deltaToLastApiCall: Date.now() - lastFetch,
deltaToLastApiTicket: Date.now() - lastApiTicketFetch

View File

@ -21,6 +21,8 @@ import { PermanentIndexedStore } from './store/types';
import * as path from 'path';
// import * as electron from 'electron';
import log from 'electron-log'; //tslint:disable-line:match-default-export-name
export interface ProfileCacheQueueEntry {
name: string;
@ -29,6 +31,7 @@ export interface ProfileCacheQueueEntry {
gender?: Gender;
score: number;
channelId?: string;
retryCount: number;
}
@ -53,6 +56,8 @@ export class CacheManager {
protected lastFetch = Date.now();
protected fetchLog: Record<string, number> = {};
protected ongoingLog: Record<string, true> = {};
markLastPostTime(): void {
this.lastPost = new Date();
@ -67,6 +72,7 @@ export class CacheManager {
return;
}
log.info('profile.cache.queue', { name, skipCacheCheck, channelId });
if (!skipCacheCheck) {
const c = await this.profileCache.get(name);
@ -87,7 +93,8 @@ export class CacheManager {
key,
channelId,
added: new Date(),
score: 0
score: 0,
retryCount: 0
};
this.queue.push(entry);
@ -102,7 +109,6 @@ export class CacheManager {
await methods.fieldsGet();
const c = await methods.characterData(name, -1, true);
const r = await this.profileCache.register(c);
this.updateAdScoringForProfile(c, r.match.matchScore);
@ -168,6 +174,11 @@ export class CacheManager {
const entry = this.queue.pop() as ProfileCacheQueueEntry;
if (entry) {
// just in case - remove duplicates
this.queue = _.filter(this.queue, (q) => q.name !== entry.name);
}
// console.log('PopFromQueue', entry.name, this.queue.length);
return entry;
@ -253,17 +264,42 @@ export class CacheManager {
// console.log('Next in queue', next.name, (Date.now() - d) / 1000.0);
try {
let skipFetch = false;
if (
(next.name in this.ongoingLog) ||
((next.name in this.fetchLog) && (Date.now() - this.fetchLog[next.name] < 120000))
) {
skipFetch = true;
}
// tslint:disable-next-line: binary-expression-operand-order
if ((false) && (next)) {
console.log(`Fetch '${next.name}' for channel '${next.channelId}', gap: ${(Date.now() - this.lastFetch)}ms`);
this.lastFetch = Date.now();
}
await this.fetchProfile(next.name);
if (!skipFetch) {
this.ongoingLog[next.name] = true;
await this.fetchProfile(next.name);
this.fetchLog[next.name] = Date.now();
}
// just in case - remove duplicates
this.queue = _.filter(this.queue, (q) => q.name !== next.name);
delete this.ongoingLog[next.name];
} catch (err) {
console.error('Profile queue error', err);
this.queue.push(next); // return to queue
delete this.ongoingLog[next.name];
next.retryCount += 1;
if (next.retryCount < 10) {
this.queue.push(next); // return to queue
}
}
// console.log('Completed', next.name, (Date.now() - d) / 1000.0);

View File

@ -91,7 +91,7 @@ export class ProfileCache extends AsyncCache<CharacterCacheRecord> {
cacheRecord.added = new Date(pd.firstSeen * 1000);
cacheRecord.meta = {
lastMetaFetched: pd.lastMetaFetched ? new Date(pd.lastMetaFetched) : null,
lastMetaFetched: pd.lastMetaFetched ? new Date(pd.lastMetaFetched * 1000) : null,
groups: pd.groups,
friends: pd.friends,
images: pd.images,

View File

@ -59,7 +59,12 @@ export class WorkerClient {
}
if (res.state === 'ok') {
// log.silly('store.worker.client.msg.ok', { t: (Date.now() - waiter.initiated) / 1000, req: waiter.request });
const t = Date.now() - waiter.initiated;
if (t > 200) {
log.info('store.worker.client.msg.slow', { t: t / 1000, req: waiter.request, res });
}
waiter.resolve(res.result);
} else {
log.error('store.worker.client.msg.err', { t: (Date.now() - waiter.initiated) / 1000, msg: res.msg, req: waiter.request });

View File

@ -8,7 +8,6 @@ type IndexedCallback = (params: Record<string, any>) => Promise<any>;
let indexed: IndexedStore;
const reply = (req: IndexedRequest, result?: any, err?: string | Error): void => {
const res: any = {
type: 'res',

View File

@ -394,10 +394,12 @@
(cache && !skipCache)
&& (cache.meta)
&& (cache.meta.lastMetaFetched)
&& (Date.now() - cache.meta.lastMetaFetched.getTime() > CHARACTER_META_CACHE_EXPIRE)
&& (Date.now() - cache.meta.lastMetaFetched.getTime() < CHARACTER_META_CACHE_EXPIRE)
) {
// do nothing
} else {
log.debug('profile.updateMeta', { timestamp: cache?.meta?.lastMetaFetched, diff: Date.now() - (cache?.meta?.lastMetaFetched?.getTime() || 0) });
// No await on purpose:
// tslint:disable-next-line no-floating-promises
this.updateMeta(this.name).catch(err => console.error('profile.updateMeta', err));