Skip to content

Commit

Permalink
WIP - over-capacity approach
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhand committed Aug 12, 2024
1 parent 29ca64f commit 7636dca
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 44 deletions.
13 changes: 12 additions & 1 deletion functions/adjustChatCapacity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type EnvVars = {

export type Body = {
workerSid?: string;
adjustment?: 'increase' | 'decrease' | 'increaseUntilCapacityAvailable';
adjustment?: 'increase' | 'decrease' | 'increaseUntilCapacityAvailable' | 'setTo1';
request: { cookies: {}; headers: {} };
};

Expand Down Expand Up @@ -120,6 +120,17 @@ export const adjustChatCapacity = async (
return { status: 200, message: 'Successfully decreased channel capacity' };
}

if (body.adjustment === 'setTo1') {
if (channel.configuredCapacity !== 1) {
await channel.update({ capacity: channel.configuredCapacity - 1 });
// If configuredCapacity is already 1, send status 200 to avoid error on client side
return { status: 200, message: 'Successfully reset channel capacity to 1' };
}

// If configuredCapacity is already 1, send status 200 to avoid error on client side
return { status: 200, message: 'Channel capacity already 1, no adjustment made.' };
}

return { status: 400, message: 'Invalid adjustment argument' };
};

Expand Down
2 changes: 1 addition & 1 deletion functions/pullTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const handler = TokenValidator(
}
}
}
await adjustChatCapacity(context, { workerSid, adjustment: 'decrease' });
await adjustChatCapacity(context, { workerSid, adjustment: 'setTo1' });
resolve(send(404)({ message: 'No task found to pull' }));
return undefined;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import { Context } from '@twilio-labs/serverless-runtime-types/types';
import {
EventType,
RESERVATION_WRAPUP,
RESERVATION_ACCEPTED,
RESERVATION_REJECTED,
EventFields,
TaskrouterListener,
} from '@tech-matters/serverless-helpers/taskrouter';
import type { AdjustChatCapacityType } from '../adjustChatCapacity';

export const eventTypes: EventType[] = [RESERVATION_WRAPUP];
export const eventTypes: EventType[] = [RESERVATION_ACCEPTED, RESERVATION_REJECTED];

type EnvVars = {
TWILIO_WORKSPACE_SID: string;
Expand Down Expand Up @@ -61,7 +62,7 @@ export const handleEvent = async (context: Context<EnvVars>, event: EventFields)

const body = {
workerSid,
adjustment: 'decrease',
adjustment: 'setTo1',
} as const;

await adjustChatCapacity(context, body);
Expand Down
38 changes: 0 additions & 38 deletions functions/taskrouterListeners/transfersListener.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
} from '@tech-matters/serverless-helpers/taskrouter';
import type { TransferMeta, ChatTransferTaskAttributes } from '../transfer/helpers.private';
import { InteractionChannelParticipants } from '../interaction/interactionChannelParticipants.private';
import { AdjustChatCapacityType } from '../adjustChatCapacity';

export const eventTypes: EventType[] = [
RESERVATION_ACCEPTED,
Expand Down Expand Up @@ -156,37 +155,6 @@ const updateWarmVoiceTransferAttributes = async (
*/
export const shouldHandle = (event: EventFields) => eventTypes.includes(event.EventType);

const decreaseChatCapacity = async (context: Context<EnvVars>, taskSid: string) => {
const serviceConfig = await context.getTwilioClient().flexApi.configuration.get().fetch();
const {
feature_flags: { enable_backend_manual_pulling: enableBackendManualPulling },
} = serviceConfig.attributes;
if (enableBackendManualPulling) {
const task = await context
.getTwilioClient()
.taskrouter.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks(taskSid);
const reservations = await task.reservations.list();
const workerSid = reservations.find((r) => r.reservationStatus === 'accepted')?.workerSid;

if (!workerSid) {
console.warn(`No worker found for task ${taskSid} to decrease chat capacity.`);
return;
}

const { path } = Runtime.getFunctions().adjustChatCapacity;
// eslint-disable-next-line global-require,import/no-dynamic-require,prefer-destructuring
const adjustChatCapacity: AdjustChatCapacityType = require(path).adjustChatCapacity;

const body = {
workerSid,
adjustment: 'decrease',
} as const;

await adjustChatCapacity(context, body);
}
};

export const handleEvent = async (context: Context<EnvVars>, event: EventFields) => {
try {
const {
Expand All @@ -212,9 +180,6 @@ export const handleEvent = async (context: Context<EnvVars>, event: EventFields)

const { originalTask: originalTaskSid } = taskAttributes.transferMeta;

// We need to decrease chat capacity before completing the task, it until the task completed event introduces a race condition
// The worker can still be offered another task before capacity is reduced if we don't do it now
await decreaseChatCapacity(context, originalTaskSid);
const client = context.getTwilioClient();

await client.taskrouter.workspaces
Expand Down Expand Up @@ -254,9 +219,6 @@ export const handleEvent = async (context: Context<EnvVars>, event: EventFields)

const { originalTask: originalTaskSid } = taskAttributes.transferMeta;

// We need to decrease chat capacity before completing the task, it until the task completed event introduces a race condition
// The worker can still be offered another task before capacity is reduced if we don't do it now
await decreaseChatCapacity(context, originalTaskSid);
const client = context.getTwilioClient();

await client.taskrouter.workspaces
Expand Down
2 changes: 1 addition & 1 deletion functions/transferChatStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async function increaseChatCapacity(

const body = {
workerSid: worker?.sid as string,
adjustment: 'increase',
adjustment: 'increaseUntilCapacityAvailable',
} as const;

await adjustChatCapacity(context, body);
Expand Down

0 comments on commit 7636dca

Please sign in to comment.