Skip to content

Commit

Permalink
Bind groupName to SchedulerClient
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Jun 9, 2024
1 parent 97576ac commit de3c8b4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
10 changes: 6 additions & 4 deletions examples/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ export const createSchedule =
({ log }) =>
async (scheduleName, groupName, arn, roleArn) => {
const client = new SchedulerClient({
groupName,
log
})
return client.createSchedule(scheduleName, {
scheduleExpression: 'rate(1 minute)',
flexibleTimeWindow: { mode: 'OFF' },
input: { foo: 'bar' },
groupName,
target: {
arn,
roleArn,
Expand All @@ -26,22 +26,23 @@ export const deleteSchedule =
({ log }) =>
async (scheduleName, groupName) => {
const client = new SchedulerClient({
groupName,
log
})
return client.deleteSchedule(scheduleName, { groupName })
return client.deleteSchedule(scheduleName)
}

export const updateSchedule =
({ log }) =>
async (scheduleName, groupName, arn, roleArn) => {
const client = new SchedulerClient({
groupName,
log
})
return client.updateSchedule(scheduleName, {
scheduleExpression: 'rate(2 minutes)',
flexibleTimeWindow: { mode: 'OFF' },
input: { foo: 'bar' },
groupName,
target: {
arn,
roleArn,
Expand All @@ -57,7 +58,8 @@ export const getSchedule =
({ log }) =>
async (scheduleName, groupName) => {
const client = new SchedulerClient({
groupName,
log
})
return client.getSchedule(scheduleName, { groupName })
return client.getSchedule(scheduleName)
}
1 change: 1 addition & 0 deletions lib/clients/scheduler.doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @class SchedulerClient
* @see {@link https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-eventbridge/index.html|@aws-sdk/client-scheduler}
* @param {Object} parameters
* @param {string} [parameters.groupName=default] Schedule group name.
* @param {string} [parameters.name=scheduler] Client name.
* @param {string} [parameters.reqId=<uuid>] Request id.
* @param {Object} [parameters.log=<logger>] Pino compatible logger.
Expand Down
16 changes: 12 additions & 4 deletions lib/clients/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ import { keysToCamelCase, keysToPascalCase } from '../case.js'
const createClient = createCache()

export class SchedulerClient {
#groupName
#client
#reqId
#log

constructor({
groupName = 'default',
name = 'scheduler',
reqId = uuidv4(),
log = createLogger(),
AwsSchedulerClient = AwsSdkSchedulerClient,
params = {}
}) {
this.#groupName = groupName
this.#client = createClient(name, () => new AwsSchedulerClient(params))
this.#reqId = reqId
this.#log = log.child({
groupName,
params,
client: name,
class: SchedulerClient.name,
Expand All @@ -52,7 +56,7 @@ export class SchedulerClient {
})
try {
log.info('start')
const req = formatReq({ ...params, name: scheduleName })
const req = this.#formatReq({ ...params, name: scheduleName })
const command = new GetScheduleCommand(req)

const res = await this.#client.send(command)
Expand All @@ -76,7 +80,7 @@ export class SchedulerClient {
})
try {
log.info('start')
const req = formatReq({ ...params, name: scheduleName })
const req = this.#formatReq({ ...params, name: scheduleName })
const command = new CreateScheduleCommand(req)

const res = await this.#client.send(command)
Expand All @@ -100,7 +104,7 @@ export class SchedulerClient {
})
try {
log.info('start')
const req = formatReq({ ...params, name: scheduleName })
const req = this.#formatReq({ ...params, name: scheduleName })
const command = new DeleteScheduleCommand(req)

const res = await this.#client.send(command)
Expand All @@ -125,7 +129,7 @@ export class SchedulerClient {
try {
log.info('start')

const req = formatReq({ ...params, name: scheduleName })
const req = this.#formatReq({ ...params, name: scheduleName })
const command = new UpdateScheduleCommand(req)

const res = await this.#client.send(command)
Expand All @@ -140,6 +144,10 @@ export class SchedulerClient {
throw err
}
}

#formatReq = (input) => {
return formatReq({ ...input, GroupName: this.#groupName })
}
}

const formatReq = pipe(
Expand Down
35 changes: 23 additions & 12 deletions lib/clients/scheduler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ test.beforeEach((t) => {
t.context.createClient = (t, options) => {
const client = new SchedulerClient({
name: uuidv4(),
groupName,
AwsSchedulerClient: t.context.AwsSchedulerClient,
reqId,
log: createLogger({ t }),
Expand Down Expand Up @@ -53,7 +54,7 @@ test('getSchedule: returns response', async (t) => {
td.when(
AwsSchedulerClient.prototype.send(
td.matchers.isAwsSdkCommand(
new GetScheduleCommand({ Name: scheduleName })
new GetScheduleCommand({ Name: scheduleName, GroupName: groupName })
)
)
).thenResolve(getScheduleResponse)
Expand All @@ -70,12 +71,16 @@ test('getSchedule: passes params', async (t) => {
td.when(
AwsSchedulerClient.prototype.send(
td.matchers.isAwsSdkCommand(
new GetScheduleCommand({ Name: scheduleName, GroupName: groupName })
new GetScheduleCommand({
Name: scheduleName,
GroupName: groupName,
Foo: 'bar'
})
)
)
).thenResolve(getScheduleResponse)

const data = await client.getSchedule(scheduleName, { groupName })
const data = await client.getSchedule(scheduleName, { foo: 'bar' })

t.deepEqual(data, getScheduleResponseFormatted)
})
Expand All @@ -88,7 +93,7 @@ test('getSchedule: throws error from client', async (t) => {
td.when(
AwsSchedulerClient.prototype.send(
td.matchers.isAwsSdkCommand(
new GetScheduleCommand({ Name: scheduleName })
new GetScheduleCommand({ Name: scheduleName, GroupName: groupName })
)
)
).thenReject(err)
Expand All @@ -103,7 +108,7 @@ test('deleteSchedule: returns response', async (t) => {
td.when(
AwsSchedulerClient.prototype.send(
td.matchers.isAwsSdkCommand(
new DeleteScheduleCommand({ Name: scheduleName })
new DeleteScheduleCommand({ Name: scheduleName, GroupName: groupName })
)
)
).thenResolve({})
Expand All @@ -120,12 +125,16 @@ test('deleteSchedule: passes params', async (t) => {
td.when(
AwsSchedulerClient.prototype.send(
td.matchers.isAwsSdkCommand(
new DeleteScheduleCommand({ Name: scheduleName, GroupName: groupName })
new DeleteScheduleCommand({
Name: scheduleName,
GroupName: groupName,
Foo: 'bar'
})
)
)
).thenResolve({})

const data = await client.deleteSchedule(scheduleName, { groupName })
const data = await client.deleteSchedule(scheduleName, { foo: 'bar' })

t.deepEqual(data, {})
})
Expand All @@ -138,7 +147,7 @@ test('deleteSchedule: throws error from client', async (t) => {
td.when(
AwsSchedulerClient.prototype.send(
td.matchers.isAwsSdkCommand(
new DeleteScheduleCommand({ Name: scheduleName })
new DeleteScheduleCommand({ Name: scheduleName, GroupName: groupName })
)
)
).thenReject(err)
Expand All @@ -153,7 +162,7 @@ test('createSchedule: returns response', async (t) => {
td.when(
AwsSchedulerClient.prototype.send(
td.matchers.isAwsSdkCommand(
new CreateScheduleCommand({ Name: scheduleName })
new CreateScheduleCommand({ Name: scheduleName, GroupName: groupName })
)
)
).thenResolve(createScheduleResponse)
Expand All @@ -174,6 +183,7 @@ test('createSchedule: passes params', async (t) => {
Arn: 'mock-schedule-arn',
FlexibleTimeWindow: { Mode: 'OFF' },
Name: scheduleName,
GroupName: groupName,
ScheduleExpression: 'rate(1 minute)',
Target: {
Arn: 'mock-arn',
Expand Down Expand Up @@ -214,7 +224,7 @@ test('createSchedule: throws error from client', async (t) => {
td.when(
AwsSchedulerClient.prototype.send(
td.matchers.isAwsSdkCommand(
new CreateScheduleCommand({ Name: scheduleName })
new CreateScheduleCommand({ Name: scheduleName, GroupName: groupName })
)
)
).thenReject(err)
Expand All @@ -229,7 +239,7 @@ test('updateSchedule: returns response', async (t) => {
td.when(
AwsSchedulerClient.prototype.send(
td.matchers.isAwsSdkCommand(
new UpdateScheduleCommand({ Name: scheduleName })
new UpdateScheduleCommand({ Name: scheduleName, GroupName: groupName })
)
)
).thenResolve(updateScheduleResponse)
Expand All @@ -250,6 +260,7 @@ test('updateSchedule: passes params', async (t) => {
Arn: 'mock-schedule-arn',
FlexibleTimeWindow: { Mode: 'OFF' },
Name: scheduleName,
GroupName: groupName,
ScheduleExpression: 'rate(1 minute)',
Target: {
Arn: 'mock-arn',
Expand Down Expand Up @@ -290,7 +301,7 @@ test('updateSchedule: throws error from client', async (t) => {
td.when(
AwsSchedulerClient.prototype.send(
td.matchers.isAwsSdkCommand(
new UpdateScheduleCommand({ Name: scheduleName })
new UpdateScheduleCommand({ Name: scheduleName, GroupName: groupName })
)
)
).thenReject(err)
Expand Down

0 comments on commit de3c8b4

Please sign in to comment.