Skip to content

Commit 91bd933

Browse files
authored
refactor(label-utils): use withDatastoreLock helper (#5228)
1 parent b2f851a commit 91bd933

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

packages/label-utils/src/label-utils.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import {createHash} from 'crypto';
1616
import {logger as defaultLogger, GCFLogger} from 'gcf-utils';
17-
import {DatastoreLock} from '@google-automations/datastore-lock';
17+
import {withDatastoreLock} from '@google-automations/datastore-lock';
1818
/* eslint-disable-next-line node/no-extraneous-import */
1919
import {Octokit} from '@octokit/rest';
2020

@@ -41,16 +41,18 @@ export async function syncLabels(
4141
labels: Array<Label>,
4242
logger: GCFLogger = defaultLogger
4343
): Promise<void> {
44-
const l = new DatastoreLock('label-sync', `${owner}/${repo}`);
45-
const lockResult = await l.acquire();
46-
if (!lockResult) {
47-
logger.error(`Failed to acquire the lock for ${owner}/${repo}`);
48-
return;
49-
}
5044
try {
51-
return syncLabelsImpl(octokit, owner, repo, labels);
52-
} finally {
53-
await l.release();
45+
await withDatastoreLock(
46+
{
47+
lockId: 'label-sync',
48+
target: `${owner}/${repo}`,
49+
},
50+
async () => {
51+
return await syncLabelsImpl(octokit, owner, repo, labels, logger);
52+
}
53+
);
54+
} catch (e) {
55+
logger.error('Failed to acquire the lock:', e);
5456
}
5557
}
5658

0 commit comments

Comments
 (0)