From 3072c2fae9109c826e7c7374faf48092fa912a19 Mon Sep 17 00:00:00 2001 From: peterpeterparker Date: Sat, 5 Sep 2020 18:05:16 +0200 Subject: [PATCH 1/3] fix: sequential publish if pwa and github --- cloud/functions/src/model/data/task.ts | 2 +- .../request/publish/schedule-publish-task.ts | 55 +++++++++++-------- cloud/functions/src/watch/publish/publish.ts | 21 ++++++- 3 files changed, 50 insertions(+), 28 deletions(-) diff --git a/cloud/functions/src/model/data/task.ts b/cloud/functions/src/model/data/task.ts index 5ae7b10c2..22c9e80fd 100644 --- a/cloud/functions/src/model/data/task.ts +++ b/cloud/functions/src/model/data/task.ts @@ -4,7 +4,7 @@ export interface TaskData { deckId: string; token: string | firestore.FieldValue; - type: 'publish-deck' | 'push-github'; + type: 'publish-all' | 'publish-deck' | 'push-github'; status: 'scheduled' | 'failure' | 'successful'; diff --git a/cloud/functions/src/request/publish/schedule-publish-task.ts b/cloud/functions/src/request/publish/schedule-publish-task.ts index e9465338b..0525457ab 100644 --- a/cloud/functions/src/request/publish/schedule-publish-task.ts +++ b/cloud/functions/src/request/publish/schedule-publish-task.ts @@ -18,7 +18,6 @@ export function schedulePublish(request: functions.Request): Promise { +async function schedule(deckId: string, publish: boolean, github: boolean, token: string) { + if (publish && github) { + await scheduleTask({ + deckId, + token, + type: 'publish-all', + }); + + return; + } + + if (publish) { + await scheduleTask({ + deckId, + token, + type: 'publish-deck', + }); + } + + if (github) { + await scheduleTask({ + deckId, + token, + type: 'push-github', + }); + } +} + +function updateDeckDeploy(deckId: string, publish: boolean, github: boolean): Promise { return new Promise(async (resolve, reject) => { try { if (!deckId || deckId === undefined || !deckId) { diff --git a/cloud/functions/src/watch/publish/publish.ts b/cloud/functions/src/watch/publish/publish.ts index 677e1fb39..208221d68 100644 --- a/cloud/functions/src/watch/publish/publish.ts +++ b/cloud/functions/src/watch/publish/publish.ts @@ -55,13 +55,28 @@ function publishJob(snap: DocumentSnapshot): Promise { return; } - if (task.type === 'publish-deck') { + if (task.type === 'publish-all') { + const newPublish: boolean = deck.data.api_id === undefined || deck.data.api_id === null; + + // If we do both, currently we need the API first as we are getting the content from the published deck + await publishToApi(deck, task.token as string); + + // Even if we fixed the delay to publish to Cloudfare CDN (#195), sometimes if too quick, the presentation will not be correctly published + // Therefore, to avoid such problem, we add a bit of delay in the process but only for the first publish + setTimeout( + async () => { + await publishToGitHub(deck.id, deck.data); + resolve(); + }, + newPublish ? 5000 : 0 + ); + } else if (task.type === 'publish-deck') { await publishToApi(deck, task.token as string); + resolve(); } else if (task.type === 'push-github') { await publishToGitHub(deck.id, deck.data); + resolve(); } - - resolve(); } catch (err) { reject(err); } From 5ddf7a965aeb9e6446e5afa07080129d27ed9c12 Mon Sep 17 00:00:00 2001 From: peterpeterparker Date: Sat, 5 Sep 2020 18:13:20 +0200 Subject: [PATCH 2/3] fix: sequential publish, refresh deck before doing github --- cloud/functions/src/watch/publish/publish.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/cloud/functions/src/watch/publish/publish.ts b/cloud/functions/src/watch/publish/publish.ts index 208221d68..0e99e3fdf 100644 --- a/cloud/functions/src/watch/publish/publish.ts +++ b/cloud/functions/src/watch/publish/publish.ts @@ -65,7 +65,7 @@ function publishJob(snap: DocumentSnapshot): Promise { // Therefore, to avoid such problem, we add a bit of delay in the process but only for the first publish setTimeout( async () => { - await publishToGitHub(deck.id, deck.data); + await delayPublishToGitHub(deck.id); resolve(); }, newPublish ? 5000 : 0 @@ -82,3 +82,18 @@ function publishJob(snap: DocumentSnapshot): Promise { } }); } + +async function delayPublishToGitHub(deckId: string) { + // It has been changed by the publish to the API + const refreshDeck: Deck = await findDeck(deckId); + + if (!refreshDeck || !refreshDeck.data) { + throw new Error('Updated published deck cannot be found'); + } + + try { + await publishToGitHub(refreshDeck.id, refreshDeck.data); + } catch (err) { + throw err; + } +} From 7e80b760ff854274e652cc6677a64feee5bf292c Mon Sep 17 00:00:00 2001 From: peterpeterparker Date: Sat, 5 Sep 2020 18:26:18 +0200 Subject: [PATCH 3/3] fix: add delay for Cloudfare --- cloud/functions/src/watch/publish/publish.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud/functions/src/watch/publish/publish.ts b/cloud/functions/src/watch/publish/publish.ts index 0e99e3fdf..b92a975f2 100644 --- a/cloud/functions/src/watch/publish/publish.ts +++ b/cloud/functions/src/watch/publish/publish.ts @@ -68,7 +68,7 @@ function publishJob(snap: DocumentSnapshot): Promise { await delayPublishToGitHub(deck.id); resolve(); }, - newPublish ? 5000 : 0 + newPublish ? 7000 : 0 ); } else if (task.type === 'publish-deck') { await publishToApi(deck, task.token as string);