Skip to content

Commit 40e7318

Browse files
Егор КоноваловЕгор Коновалов
authored andcommitted
imp(): fetch workspaces one by one
1 parent d1c7af4 commit 40e7318

3 files changed

Lines changed: 28 additions & 16 deletions

File tree

workers/limiter/src/dbHelper.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ export class DbHelper {
3535
}
3636

3737
/**
38-
* Method that returns all workspaces with their tariff plans
38+
* Method that yields all workspaces with their tariff plans
3939
*/
40-
public async getWorkspacesWithTariffPlans():Promise<WorkspaceWithTariffPlan[]>;
40+
public getWorkspacesWithTariffPlans(): AsyncGenerator<WorkspaceWithTariffPlan>;
4141
/**
4242
* Method that returns workspace with its tariff plan by its id
4343
*
4444
* @param id - id of the workspace to fetch
4545
*/
46-
public async getWorkspacesWithTariffPlans(id: string):Promise<WorkspaceWithTariffPlan>;
46+
public async getWorkspacesWithTariffPlans(id: string): Promise<WorkspaceWithTariffPlan>;
4747
/**
4848
* Returns workspace with its tariff plan by its id
4949
*
5050
* @param id - workspace id
5151
*/
52-
public async getWorkspacesWithTariffPlans(id?: string):Promise<WorkspaceWithTariffPlan[] | WorkspaceWithTariffPlan> {
53-
/* eslint-disable-next-line */
52+
public async *getWorkspacesWithTariffPlans(id?: string): AsyncGenerator<WorkspaceWithTariffPlan> | Promise<WorkspaceWithTariffPlan> {
53+
/* eslint-disable-next-line */
5454
const queue: any[] = [
5555
{
5656
$lookup: {
@@ -75,9 +75,15 @@ export class DbHelper {
7575
});
7676
}
7777

78-
const workspacesArray = await this.workspacesCollection.aggregate<WorkspaceWithTariffPlan>(queue).toArray();
78+
const workspaces = this.workspacesCollection.aggregate<WorkspaceWithTariffPlan>(queue);
7979

80-
return (id !== undefined) ? workspacesArray[0] : workspacesArray;
80+
if (id !== undefined) {
81+
return await workspaces.next();
82+
}
83+
84+
for await (const workspace of workspaces) {
85+
yield workspace;
86+
}
8187
}
8288

8389
/**

workers/limiter/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ export default class LimiterWorker extends Worker {
189189
private async handleRegularWorkspacesCheck(): Promise<void> {
190190
let message = '';
191191

192-
const workspaces = await this.dbHelper.getWorkspacesWithTariffPlans();
192+
const workspaces = this.dbHelper.getWorkspacesWithTariffPlans();
193193

194194
const updatedWorkspaces: WorkspaceWithTariffPlan[] = [];
195195

196-
await Promise.all(workspaces.map(async (workspace) => {
196+
for await (const workspace of workspaces) {
197197
/**
198198
* If workspace is already blocked - do nothing
199199
*/
@@ -226,7 +226,7 @@ export default class LimiterWorker extends Worker {
226226
this.redis.appendBannedProjects(projectIds);
227227
message += this.formSingleWorkspaceMessage(updatedWorkspace, projectsToUpdate, 'blocked');
228228
}
229-
}));
229+
};
230230

231231
this.dbHelper.updateWorkspacesEventsCountAndIsBlocked(updatedWorkspaces);
232232

workers/limiter/tests/dbHelper.test.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,22 @@ describe('DbHelper', () => {
159159
/**
160160
* Act
161161
*/
162-
const result = await dbHelper.getWorkspacesWithTariffPlans();
162+
const cursor = dbHelper.getWorkspacesWithTariffPlans();
163+
164+
const workspaces = []
165+
166+
for await (const workspace of cursor) {
167+
workspaces.push(workspace)
168+
}
163169

164170
/**
165171
* Assert
166172
*/
167-
expect(result).toHaveLength(2);
168-
expect(result[0].tariffPlan).toBeDefined();
169-
expect(result[1].tariffPlan).toBeDefined();
170-
expect(result[0].tariffPlan.eventsLimit).toBe(10);
171-
expect(result[1].tariffPlan.eventsLimit).toBe(10000);
173+
expect(workspaces).toHaveLength(2);
174+
expect(workspaces[0].tariffPlan).toBeDefined();
175+
expect(workspaces[1].tariffPlan).toBeDefined();
176+
expect(workspaces[0].tariffPlan.eventsLimit).toBe(10);
177+
expect(workspaces[1].tariffPlan.eventsLimit).toBe(10000);
172178
});
173179

174180
test('Should return single workspace with its tariff plan by id', async () => {

0 commit comments

Comments
 (0)