Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit c716697

Browse files
Merge pull request #866 from deckgo/sequential-publish
fix: sequential publish
2 parents be61d81 + 7e80b76 commit c716697

File tree

3 files changed

+65
-28
lines changed

3 files changed

+65
-28
lines changed

cloud/functions/src/model/data/task.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface TaskData {
44
deckId: string;
55
token: string | firestore.FieldValue;
66

7-
type: 'publish-deck' | 'push-github';
7+
type: 'publish-all' | 'publish-deck' | 'push-github';
88

99
status: 'scheduled' | 'failure' | 'successful';
1010

cloud/functions/src/request/publish/schedule-publish-task.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export function schedulePublish(request: functions.Request): Promise<ScheduledPu
1818
try {
1919
const token: string | undefined = await geToken(request);
2020
const deckId: string | undefined = request.body.deckId;
21-
const ownerId: string | undefined = request.body.ownerId;
2221

2322
if (!deckId) {
2423
reject('No deck information provided.');
@@ -30,11 +29,6 @@ export function schedulePublish(request: functions.Request): Promise<ScheduledPu
3029
return;
3130
}
3231

33-
if (!ownerId) {
34-
reject('No owner ID provided.');
35-
return;
36-
}
37-
3832
const publish: boolean = request.body.publish !== undefined && request.body.publish;
3933
const github: boolean = request.body.github !== undefined && request.body.github;
4034

@@ -44,25 +38,10 @@ export function schedulePublish(request: functions.Request): Promise<ScheduledPu
4438
}
4539

4640
// We tell the frontend to wait
47-
await updateDeckDeploy(deckId, ownerId, publish, github);
41+
await updateDeckDeploy(deckId, publish, github);
4842

4943
// We schedule internally / cloud the job so we keep secret the token
50-
51-
if (publish) {
52-
await scheduleTask({
53-
deckId,
54-
token,
55-
type: 'publish-deck',
56-
});
57-
}
58-
59-
if (github) {
60-
await scheduleTask({
61-
deckId,
62-
token,
63-
type: 'push-github',
64-
});
65-
}
44+
await schedule(deckId, publish, github, token);
6645

6746
resolve({
6847
deckId,
@@ -76,7 +55,35 @@ export function schedulePublish(request: functions.Request): Promise<ScheduledPu
7655
});
7756
}
7857

79-
function updateDeckDeploy(deckId: string, ownerId: string, publish: boolean, github: boolean): Promise<void> {
58+
async function schedule(deckId: string, publish: boolean, github: boolean, token: string) {
59+
if (publish && github) {
60+
await scheduleTask({
61+
deckId,
62+
token,
63+
type: 'publish-all',
64+
});
65+
66+
return;
67+
}
68+
69+
if (publish) {
70+
await scheduleTask({
71+
deckId,
72+
token,
73+
type: 'publish-deck',
74+
});
75+
}
76+
77+
if (github) {
78+
await scheduleTask({
79+
deckId,
80+
token,
81+
type: 'push-github',
82+
});
83+
}
84+
}
85+
86+
function updateDeckDeploy(deckId: string, publish: boolean, github: boolean): Promise<void> {
8087
return new Promise<void>(async (resolve, reject) => {
8188
try {
8289
if (!deckId || deckId === undefined || !deckId) {

cloud/functions/src/watch/publish/publish.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,45 @@ function publishJob(snap: DocumentSnapshot): Promise<void> {
5555
return;
5656
}
5757

58-
if (task.type === 'publish-deck') {
58+
if (task.type === 'publish-all') {
59+
const newPublish: boolean = deck.data.api_id === undefined || deck.data.api_id === null;
60+
61+
// If we do both, currently we need the API first as we are getting the content from the published deck
62+
await publishToApi(deck, task.token as string);
63+
64+
// Even if we fixed the delay to publish to Cloudfare CDN (#195), sometimes if too quick, the presentation will not be correctly published
65+
// Therefore, to avoid such problem, we add a bit of delay in the process but only for the first publish
66+
setTimeout(
67+
async () => {
68+
await delayPublishToGitHub(deck.id);
69+
resolve();
70+
},
71+
newPublish ? 7000 : 0
72+
);
73+
} else if (task.type === 'publish-deck') {
5974
await publishToApi(deck, task.token as string);
75+
resolve();
6076
} else if (task.type === 'push-github') {
6177
await publishToGitHub(deck.id, deck.data);
78+
resolve();
6279
}
63-
64-
resolve();
6580
} catch (err) {
6681
reject(err);
6782
}
6883
});
6984
}
85+
86+
async function delayPublishToGitHub(deckId: string) {
87+
// It has been changed by the publish to the API
88+
const refreshDeck: Deck = await findDeck(deckId);
89+
90+
if (!refreshDeck || !refreshDeck.data) {
91+
throw new Error('Updated published deck cannot be found');
92+
}
93+
94+
try {
95+
await publishToGitHub(refreshDeck.id, refreshDeck.data);
96+
} catch (err) {
97+
throw err;
98+
}
99+
}

0 commit comments

Comments
 (0)