Skip to content

Commit

Permalink
fix: only set chrome-for-testing data timestamp after sync task finish (
Browse files Browse the repository at this point in the history
#653)

closes #652
  • Loading branch information
fengmk2 authored Feb 23, 2024
1 parent ae83136 commit 4bc0c9c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions app/common/adapter/binary/AbstractBinary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export abstract class AbstractBinary {
abstract initFetch(binaryName: BinaryName): Promise<void>;
abstract fetch(dir: string, binaryName: BinaryName): Promise<FetchResult | undefined>;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async finishFetch(_success: boolean, _binaryName: BinaryName): Promise<void> {
// do not thing by default
}

protected async requestXml(url: string) {
const { status, data, headers } = await this.httpclient.request(url, {
timeout: 30000,
Expand Down
23 changes: 20 additions & 3 deletions app/common/adapter/binary/ChromeForTestingBinary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AbstractBinary, FetchResult, BinaryItem, BinaryAdapter } from './Abstra
@BinaryAdapter(BinaryType.ChromeForTesting)
export class ChromeForTestingBinary extends AbstractBinary {
static lastTimestamp = '';
#timestamp = '';

private dirItems?: {
[key: string]: BinaryItem[];
Expand All @@ -16,17 +17,34 @@ export class ChromeForTestingBinary extends AbstractBinary {
this.dirItems = undefined;
}

async finishFetch(success: boolean) {
if (success && this.#timestamp && ChromeForTestingBinary.lastTimestamp !== this.#timestamp) {
ChromeForTestingBinary.lastTimestamp = this.#timestamp;
}
}

async #syncDirItems() {
this.dirItems = {};
this.dirItems['/'] = [];
const jsonApiEndpoint = 'https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json';
const { data } = await this.httpclient.request(jsonApiEndpoint, {
const { data, status, headers } = await this.httpclient.request(jsonApiEndpoint, {
dataType: 'json',
timeout: 30000,
followRedirect: true,
gzip: true,
});
if (data.timestamp === ChromeForTestingBinary.lastTimestamp) return;
if (status !== 200) {
this.logger.warn('[ChromeForTestingBinary.request:non-200-status] url: %s, status: %s, headers: %j, data: %j',
jsonApiEndpoint, status, headers, data);
return;
}
this.#timestamp = data.timestamp;
const hasNewData = this.#timestamp !== ChromeForTestingBinary.lastTimestamp;
this.logger.info('[ChromeForTestingBinary] remote data timestamp: %j, last timestamp: %j, hasNewData: %s',
this.#timestamp, ChromeForTestingBinary.lastTimestamp, hasNewData);
if (!hasNewData) {
return;
}

// "timestamp": "2023-09-16T00:21:21.964Z",
// "versions": [
Expand Down Expand Up @@ -104,7 +122,6 @@ export class ChromeForTestingBinary extends AbstractBinary {
}
}
}
ChromeForTestingBinary.lastTimestamp = data.timestamp;
}

async fetch(dir: string): Promise<FetchResult | undefined> {
Expand Down
4 changes: 3 additions & 1 deletion app/core/service/BinarySyncerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export class BinarySyncerService extends AbstractService {
logs.push(`[${isoNow()}] 🟢 log: ${logUrl}`);
logs.push(`[${isoNow()}] 🟢🟢🟢🟢🟢 "${binaryName}" 🟢🟢🟢🟢🟢`);
await this.taskService.finishTask(task, TaskState.Success, logs.join('\n'));
await binaryAdapter.finishFetch(true, binaryName);
this.logger.info('[BinarySyncerService.executeTask:success] taskId: %s, targetName: %s, log: %s',
task.taskId, task.targetName, logUrl);
} catch (err: any) {
Expand All @@ -148,6 +149,7 @@ export class BinarySyncerService extends AbstractService {
task.taskId, task.targetName, task.error);
this.logger.error(err);
}
await binaryAdapter.finishFetch(false, binaryName);
await this.taskService.finishTask(task, TaskState.Fail, logs.join('\n'));
}
}
Expand Down Expand Up @@ -229,7 +231,7 @@ export class BinarySyncerService extends AbstractService {
if (hasDownloadError) {
logs.push(`[${isoNow()}][${dir}] ❌ Synced dir fail`);
} else {
logs.push(`[${isoNow()}][${dir}] 🟢 Synced dir success`);
logs.push(`[${isoNow()}][${dir}] 🟢 Synced dir success, hasItems: ${hasItems}`);
}
await this.taskService.appendTaskLog(task, logs.join('\n'));
}
Expand Down
2 changes: 2 additions & 0 deletions test/common/adapter/binary/ChromeForTestingBinary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('test/common/adapter/binary/ChromeForTestingBinary.test.ts', () => {
beforeEach(async () => {
binary = await app.getEggObject(ChromeForTestingBinary);
});

describe('fetch()', () => {
it('should work for chrome binary', async () => {
assert.equal(ChromeForTestingBinary.lastTimestamp, '');
Expand Down Expand Up @@ -35,6 +36,7 @@ describe('test/common/adapter/binary/ChromeForTestingBinary.test.ts', () => {
assert.match(versionRes?.items[1].name, /^chromedriver\-/);
assert.match(versionRes?.items[2].name, /^chrome\-headless\-shell\-/);
}
await binary.finishFetch(true);
assert(ChromeForTestingBinary.lastTimestamp);
});

Expand Down

0 comments on commit 4bc0c9c

Please sign in to comment.