Skip to content

Commit

Permalink
fix(RL-72): throttle materialized view update
Browse files Browse the repository at this point in the history
  • Loading branch information
NoodleOfDeath committed Oct 15, 2023
1 parent 34dbd98 commit 2e4724d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
6 changes: 5 additions & 1 deletion src/mobile/ios/Models/PublicSummaryAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public class Summary {
}

public var primaryImageUrl: URL? {
return URL(string: (media?["imageArticle"] ?? media?["imageAi1"] ?? imageUrl ?? "") + "@sm")
return URL(string: (media?["imageArticle@sm"] ??
media?["imageAi1@sm"] ??
media?["imageArticle"] ??
media?["imageAi1"] ??
imageUrl ?? ""))
}

public init(_ summary: PublicSummaryAttributes) {
Expand Down
4 changes: 2 additions & 2 deletions src/mobile/ios/ReadLess.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1915,7 +1915,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.17.0;
MARKETING_VERSION = 1.16.6;
OTHER_LDFLAGS = (
"$(inherited)",
"-ObjC",
Expand Down Expand Up @@ -1956,7 +1956,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.17.0;
MARKETING_VERSION = 1.16.6;
OTHER_LDFLAGS = (
"$(inherited)",
"-ObjC",
Expand Down
22 changes: 7 additions & 15 deletions src/server/src/api/v1/schema/resources/summary/Summary.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from './Summary.types';
import { SummaryInteraction } from './SummaryInteraction.model';
import { SummaryMedia } from './SummaryMedia.model';
import { SummaryMediaAttributes } from './SummaryMedia.types';
import { SummaryRelation } from './SummaryRelation.model';
import { SummarySentiment } from './SummarySentiment.model';
import { PublicSummarySentimentAttributes } from './SummarySentiment.types';
Expand Down Expand Up @@ -70,10 +69,6 @@ class Size {

}

type DownsampleOptions = Pick<SummaryMediaAttributes, 'key' | 'parentId' | 'path'> & {
sizes?: Size[];
};

function parseTimeInterval(str: string) {
const matches = str.match(/(\d+)\s*(months?|m(?:in(?:ute)?s?)?|h(?:(?:ou)?rs?)?|d(?:ays?)?|w(?:(?:ee)?ks?)?|y(?:(?:ea)?rs?)?)/i);
if (matches && matches[1] && matches[2]) {
Expand Down Expand Up @@ -514,19 +509,16 @@ export class Summary extends Post<SummaryAttributes, SummaryCreationAttributes>
}

async generateThumbnails(
{
key,
parentId,
path,
sizes = [Size.xs, Size.sm, Size.md, Size.lg],
}: DownsampleOptions,
folder: string
sizes = [Size.xs, Size.sm, Size.md, Size.lg],
folder = 'img/s'
) {
// eslint-disable-next-line no-async-promise-executor
return new Promise<SummaryMedia[]>(async (resolve, reject) => {
console.log('generating thumbnails');
const allMedia = await SummaryMedia.findAll({ where: { parentId: this.id } });
const results: SummaryMedia[] = [];
for (const [i, m] of allMedia.entries()) {
console.log(`generating thumbnails for ${m.path}`);
if (!/^img\/s/.test(m.path) || /@(?:xs|sm|md|lg|x+l)\.\w+$/.test(m.path)) {
continue;
}
Expand All @@ -536,11 +528,11 @@ export class Summary extends Post<SummaryAttributes, SummaryCreationAttributes>
return;
}
for (const [j, size] of sizes.entries()) {
const subkey = `${key}@${size.name}`;
const subkey = `${m.key}@${size.name}`;
const media = await SummaryMedia.findOne({
where: {
key: subkey,
parentId,
parentId: this.id,
},
});
if (media) {
Expand Down Expand Up @@ -569,7 +561,7 @@ export class Summary extends Post<SummaryAttributes, SummaryCreationAttributes>
});
const media = await SummaryMedia.create({
key: subkey,
parentId,
parentId: this.id,
path: response.key,
type: 'image',
url: response.url,
Expand Down
13 changes: 8 additions & 5 deletions src/server/src/worker/MediaWorker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ms from 'ms';
import sharp from 'sharp';

import { Summary, SummaryMedia } from '../api/v1/schema/models';
Expand Down Expand Up @@ -100,6 +101,13 @@ export async function doWork() {
try {
const items = await S3Service.listObjects();
console.log(items.length);
setInterval(async () => {
try {
await Summary.refreshViews();
} catch (e) {
console.error(e);
}
}, ms('3m'));
for (const item of items) {
if (/^img\/s/.test(item)) {
const media = await SummaryMedia.findOne({ where: { path: item } });
Expand All @@ -117,11 +125,6 @@ export async function doWork() {
folders.pop();
try {
await downsampleImage(media, folders.join('/'));
try {
await Summary.refreshViews();
} catch (e) {
console.error(e);
}
} catch (e) {
console.error(e);
}
Expand Down

0 comments on commit 2e4724d

Please sign in to comment.