Skip to content

Commit 6a92ed7

Browse files
authored
chore(limiter): logs for block workspace (#419)
* chore(limiter): logs for block workspace * chore(limiter): add regular workspace check logs * imp(limiter): unblock updates workspace in db * imp(limiter): use methods consistently
1 parent 2cfc8fe commit 6a92ed7

3 files changed

Lines changed: 19 additions & 49 deletions

File tree

workers/limiter/src/dbHelper.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,6 @@ export class DbHelper {
109109
await this.workspacesCollection.bulkWrite(operations);
110110
}
111111

112-
/**
113-
* Method to change workspace isBlocked state
114-
*
115-
* @param workspaceId - id of the workspace to be changed
116-
* @param isBlocked - new isBlocked state of the workspace
117-
*/
118-
public async changeWorkspaceBlockedState(workspaceId: string, isBlocked: boolean): Promise<void> {
119-
await this.workspacesCollection.updateOne(
120-
{ _id: new ObjectId(workspaceId) },
121-
{
122-
$set: {
123-
isBlocked,
124-
},
125-
}
126-
);
127-
}
128-
129112
/**
130113
* Returns total event counts for last billing period
131114
*

workers/limiter/src/index.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ export default class LimiterWorker extends Worker {
108108
* @param event - event to handle
109109
*/
110110
private async handleBlockWorkspaceEvent(event: BlockWorkspaceEvent): Promise<void> {
111+
this.logger.info('handle block workspace event', event);
112+
111113
const workspace = await this.dbHelper.getWorkspacesWithTariffPlans(event.workspaceId);
112114

113115
if (!workspace) {
@@ -126,7 +128,13 @@ export default class LimiterWorker extends Worker {
126128
const workspaceProjects = await this.dbHelper.getProjects(event.workspaceId);
127129
const projectIds = workspaceProjects.map(project => project._id.toString());
128130

129-
await this.dbHelper.changeWorkspaceBlockedState(event.workspaceId, true);
131+
const { updatedWorkspace } = await this.prepareWorkspaceUsageUpdate(workspace, workspaceProjects);
132+
133+
updatedWorkspace.isBlocked = true;
134+
await this.dbHelper.updateWorkspacesEventsCountAndIsBlocked([updatedWorkspace]);
135+
136+
this.logger.info('workspace blocked in db ', event.workspaceId)
137+
130138
await this.redis.appendBannedProjects(projectIds);
131139

132140
this.sendSingleWorkspaceReport(workspaceProjects, workspace, 'blocked');
@@ -159,16 +167,18 @@ export default class LimiterWorker extends Worker {
159167
/**
160168
* If workspace should be blocked by quota - then do not unblock it
161169
*/
162-
const { shouldBeBlockedByQuota } = await this.prepareWorkspaceUsageUpdate(workspace, workspaceProjects);
170+
const { shouldBeBlockedByQuota, updatedWorkspace } = await this.prepareWorkspaceUsageUpdate(workspace, workspaceProjects);
163171

164172
if (shouldBeBlockedByQuota) {
165173
return;
166174
}
167175

168-
await this.dbHelper.changeWorkspaceBlockedState(event.workspaceId, false);
176+
updatedWorkspace.isBlocked = false;
177+
178+
await this.dbHelper.updateWorkspacesEventsCountAndIsBlocked([updatedWorkspace]);
169179
await this.redis.removeBannedProjects(projectIds);
170180

171-
this.sendSingleWorkspaceReport(workspaceProjects, workspace, 'unblocked');
181+
this.sendSingleWorkspaceReport(workspaceProjects, updatedWorkspace, 'unblocked');
172182
}
173183

174184
/**
@@ -229,6 +239,8 @@ export default class LimiterWorker extends Worker {
229239
private async prepareWorkspaceUsageUpdate(
230240
workspace: WorkspaceWithTariffPlan, projects: ProjectDBScheme[]
231241
): Promise<WorkspaceReport> {
242+
this.logger.info('prepareWorkspaceUsageUpdate');
243+
232244
/**
233245
* If last charge date is not specified, then we skip checking it
234246
* In the next time the Paymaster worker starts, it will set lastChargeDate for this workspace
@@ -247,6 +259,9 @@ export default class LimiterWorker extends Worker {
247259
const since = Math.floor(new Date(workspace.lastChargeDate).getTime() / MS_IN_SEC);
248260

249261
const workspaceEventsCount = await this.dbHelper.getEventsCountByProjects(projects, since);
262+
263+
this.logger.info(`workspace ${workspace._id} events count since last charge date: ${workspaceEventsCount}`);
264+
250265
const usedQuota = workspaceEventsCount / workspace.tariffPlan.eventsLimit;
251266
const quotaNotification = NOTIFY_ABOUT_LIMIT.reverse().find(quota => quota < usedQuota);
252267

workers/limiter/tests/dbHelper.test.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -272,34 +272,6 @@ describe('DbHelper', () => {
272272
});
273273
});
274274

275-
describe('changeWorkspaceBlockedState', () => {
276-
test('Should change workspace blocked state', async () => {
277-
/**
278-
* Arrange
279-
*/
280-
const workspace = createWorkspaceMock({
281-
plan: mockedPlans.eventsLimit10,
282-
billingPeriodEventsCount: 0,
283-
lastChargeDate: new Date(),
284-
isBlocked: false,
285-
});
286-
287-
await workspaceCollection.insertOne(workspace);
288-
289-
/**
290-
* Act
291-
*/
292-
await dbHelper.changeWorkspaceBlockedState(workspace._id.toString(), true);
293-
294-
/**
295-
* Assert
296-
*/
297-
const updatedWorkspace = await workspaceCollection.findOne({ _id: workspace._id });
298-
299-
expect(updatedWorkspace.isBlocked).toBe(true);
300-
});
301-
});
302-
303275
describe('getEventsCountByProject', () => {
304276
test('Should count events and repetitions for a project', async () => {
305277
/**

0 commit comments

Comments
 (0)