Skip to content

Commit 0972b32

Browse files
committed
fix: throttle mailing-list mirror polling
1 parent 8a9d859 commit 0972b32

4 files changed

Lines changed: 52 additions & 59 deletions

File tree

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,45 @@
11
const { isUpToDate } = require('./mailing-list-mirror')
22

3-
module.exports = async context => {
3+
const SYNC_WORKFLOW = 'sync-mailing-list-mirror.yml'
4+
const WORKFLOWS_BRANCH = 'main'
5+
const SYNC_INTERVAL_MS = 5 * 60 * 1000
6+
7+
const syncMailingListMirror = async context => {
48
try {
59
const { readFileSync } = require('fs')
610
const config = JSON.parse(readFileSync(`${__dirname}/gitgitgadget-config.json`))
7-
if (await isUpToDate(config)) {
11+
if (await isUpToDate(config, context)) {
812
context.log('Mailing-list mirror is up to date')
913
return
1014
}
1115

1216
const { owner, name } = config.workflowsRepo
13-
const workflow = 'sync-mailing-list-mirror.yml'
1417
const { getInstallationIdForRepo } = require('./get-installation-id-for-repo')
1518
const { getInstallationAccessToken } = require('./get-installation-access-token')
1619
const installationID = await getInstallationIdForRepo(context, owner, name)
1720
const token = await getInstallationAccessToken(context, installationID)
1821
const { listWorkflowRuns, triggerWorkflowDispatch } = require('./trigger-workflow-dispatch')
1922

20-
for (const status of ['queued', 'in_progress']) {
21-
const runs = await listWorkflowRuns(context, token, owner, name, workflow, 'main', status)
22-
if (runs.length) {
23-
context.log(`Skipping mirror sync; a run is already ${status}`)
24-
return
25-
}
23+
const runs = await listWorkflowRuns(context, token, owner, name, SYNC_WORKFLOW, WORKFLOWS_BRANCH)
24+
if (runs.some(run => run.status === 'queued' || run.status === 'in_progress')) {
25+
context.log('Skipping mirror sync; a run is already queued or in progress')
26+
return
27+
}
28+
const completedRecently = runs.some(run => {
29+
const completedAt = Date.parse(run.updated_at)
30+
return run.status === 'completed' && Number.isFinite(completedAt) && Date.now() - completedAt < SYNC_INTERVAL_MS
31+
})
32+
if (completedRecently) {
33+
context.log('Skipping mirror sync; a run completed within the last five minutes')
34+
return
2635
}
2736

28-
const run = await triggerWorkflowDispatch(context, token, owner, name, workflow, 'main')
37+
const run = await triggerWorkflowDispatch(context, token, owner, name, SYNC_WORKFLOW, WORKFLOWS_BRANCH)
2938
context.log(`Triggered mailing-list mirror sync: ${run.html_url}`)
3039
} catch (e) {
3140
context.log('Caught exception ', e)
3241
throw e
3342
}
3443
}
44+
45+
module.exports = syncMailingListMirror

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ You can also run/debug it via VS Code, there is a default configuration called "
4747

4848
### Keeping the mailing-list mirror current
4949

50-
The `sync-mailing-list-mirror` timer checks lore.kernel.org and the GitHub mirror once a minute. It dispatches the existing mirror workflow only when their advertised refs differ and skips the dispatch while another mirror run is queued or running. The workflow's five-minute schedule remains as a fallback.
50+
The `sync-mailing-list-mirror` timer checks lore.kernel.org and the GitHub mirror every five minutes. It dispatches the existing mirror workflow only when their advertised refs differ, skips the dispatch while another mirror run is queued or running, and waits five minutes after a completed run before dispatching again. The workflow's five-minute schedule remains as a fallback.
5151

5252
The timer uses the existing `AzureWebJobsStorage` setting for its schedule and lock. Delivery time still depends on lore.kernel.org publishing the message and Azure running the timer.
5353

‎__tests__/sync-mailing-list-mirror.test.js‎

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ jest.mock('../GitGitGadget/trigger-workflow-dispatch', () => ({
1717
}))
1818

1919
const syncMailingListMirror = require('../GitGitGadget/sync-mailing-list-mirror')
20-
21-
const makeContext = () => ({ log: jest.fn() })
20+
const context = () => ({ log: jest.fn() })
2221

2322
beforeEach(() => {
2423
jest.clearAllMocks()
@@ -29,54 +28,37 @@ beforeEach(() => {
2928
mockTriggerWorkflowDispatch.mockResolvedValue({ html_url: 'https://github.com/run/1' })
3029
})
3130

32-
test('does not dispatch a sync when the mirror is current', async () => {
33-
const context = makeContext()
34-
35-
await syncMailingListMirror(context)
36-
37-
expect(mockGetInstallationIdForRepo).not.toHaveBeenCalled()
38-
expect(mockTriggerWorkflowDispatch).not.toHaveBeenCalled()
39-
expect(context.log).toHaveBeenCalledWith('Mailing-list mirror is up to date')
40-
})
41-
42-
test('dispatches a sync when lore is ahead and no run is active', async () => {
43-
const context = makeContext()
44-
mockIsUpToDate.mockResolvedValue(false)
45-
46-
await syncMailingListMirror(context)
47-
48-
expect(mockListWorkflowRuns).toHaveBeenCalledTimes(2)
49-
expect(mockTriggerWorkflowDispatch).toHaveBeenCalledWith(
50-
context,
51-
'installation-token',
52-
'gitgitgadget-workflows',
53-
'gitgitgadget-workflows',
54-
'sync-mailing-list-mirror.yml',
55-
'main'
56-
)
31+
test('does not request a workflow run when the mirror is current', async () => {
32+
await syncMailingListMirror(context())
33+
expect(mockListWorkflowRuns).not.toHaveBeenCalled()
5734
})
5835

59-
test.each(['queued', 'in_progress'])('does not dispatch while a sync is %s', async status => {
60-
const context = makeContext()
36+
test.each(['queued', 'in_progress', 'completed'])(
37+
'does not dispatch while a run is active or recently completed (%s)',
38+
async status => {
39+
const ctx = context()
40+
mockIsUpToDate.mockResolvedValue(false)
41+
mockListWorkflowRuns.mockResolvedValue([{
42+
status,
43+
updated_at: new Date().toISOString()
44+
}])
45+
46+
await syncMailingListMirror(ctx)
47+
48+
expect(mockListWorkflowRuns).toHaveBeenCalledTimes(1)
49+
expect(mockTriggerWorkflowDispatch).not.toHaveBeenCalled()
50+
}
51+
)
52+
53+
test('dispatches once when lore is ahead and the last run is old', async () => {
54+
const ctx = context()
6155
mockIsUpToDate.mockResolvedValue(false)
62-
mockListWorkflowRuns.mockImplementation(async (_context, _token, _owner, _repo, _workflow, _branch, queriedStatus) => {
63-
return queriedStatus === status ? [{ status }] : []
64-
})
65-
66-
await syncMailingListMirror(context)
67-
68-
expect(mockTriggerWorkflowDispatch).not.toHaveBeenCalled()
69-
expect(context.log).toHaveBeenCalledWith(`Skipping mirror sync; a run is already ${status}`)
70-
})
71-
72-
test('propagates a failure to read the mailing-list refs', async () => {
73-
const context = makeContext()
74-
const error = new Error('lore is unavailable')
75-
mockIsUpToDate.mockRejectedValue(error)
56+
mockListWorkflowRuns.mockResolvedValue([
57+
{ status: 'completed', updated_at: new Date(Date.now() - 10 * 60 * 1000).toISOString() }
58+
])
7659

77-
await expect(syncMailingListMirror(context)).rejects.toThrow('lore is unavailable')
60+
await syncMailingListMirror(ctx)
7861

79-
expect(mockGetInstallationIdForRepo).not.toHaveBeenCalled()
80-
expect(mockTriggerWorkflowDispatch).not.toHaveBeenCalled()
81-
expect(context.log).toHaveBeenCalledWith('Caught exception ', error)
62+
expect(mockListWorkflowRuns).toHaveBeenCalledTimes(1)
63+
expect(mockTriggerWorkflowDispatch).toHaveBeenCalledTimes(1)
8264
})

‎sync-mailing-list-mirror/function.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"name": "timer",
55
"type": "timerTrigger",
66
"direction": "in",
7-
"schedule": "0 */1 * * * *"
7+
"schedule": "0 */5 * * * *"
88
}
99
]
1010
}

0 commit comments

Comments
 (0)