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..b92a975f2 100644 --- a/cloud/functions/src/watch/publish/publish.ts +++ b/cloud/functions/src/watch/publish/publish.ts @@ -55,15 +55,45 @@ 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 delayPublishToGitHub(deck.id); + resolve(); + }, + newPublish ? 7000 : 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); } }); } + +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; + } +}