Skip to content

Commit

Permalink
fix: use DynamoDB scans instead of query
Browse files Browse the repository at this point in the history
  • Loading branch information
sammarks committed Aug 6, 2019
1 parent d2cc07b commit deec40b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 26 deletions.
6 changes: 1 addition & 5 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ provider:
- Effect: Allow
Action:
- dynamodb:UpdateItem
- dynamodb:Query
- dynamodb:Scan
- dynamodb:DeleteItem
Resource:
Fn::GetAtt: ['tasksTable', 'Arn']
Expand Down Expand Up @@ -44,13 +44,9 @@ resources:
KeySchema:
- AttributeName: taskId
KeyType: HASH
- AttributeName: executeTime
KeyType: RANGE
AttributeDefinitions:
- AttributeName: taskId
AttributeType: 'S'
- AttributeName: executeTime
AttributeType: 'N'
ProvisionedThroughput:
ReadCapacityUnits:
Ref: ReadCapacityUnits
Expand Down
4 changes: 1 addition & 3 deletions src/ingest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ module.exports.handler = async (event) => {
await documentClient.update({
TableName: process.env.TASKS_TABLE,
Key: { taskId },
UpdateExpression: 'SET #executeTime = :executeTime, #taskId = :taskId, #topicArn = :topicArn, #payload = :payload',
UpdateExpression: 'SET #executeTime = :executeTime, #topicArn = :topicArn, #payload = :payload',
ExpressionAttributeNames: {
'#executeTime': 'executeTime',
'#taskId': 'taskId',
'#topicArn': 'topicArn',
'#payload': 'payload'
},
ExpressionAttributeValues: {
':executeTime': executeTime,
':taskId': taskId,
':topicArn': topicArn,
':payload': payload
}
Expand Down
6 changes: 2 additions & 4 deletions src/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ const AWS = require('aws-sdk')
module.exports.handler = async () => {
const sns = new AWS.SNS()
const documentClient = new AWS.DynamoDB.DocumentClient()
const { Items } = await documentClient.query({
const { Items } = await documentClient.scan({
TableName: process.env.TASKS_TABLE,
KeyConditionExpression: '#taskId != :taskId AND #executeTime <= :executeTime',
FilterExpression: '#executeTime <= :executeTime',
ExpressionAttributeNames: {
'#taskId': 'taskId',
'#executeTime': 'executeTime'
},
ExpressionAttributeValues: {
':taskId': '',
':executeTime': (new Date()).getTime() / 1000
}
}).promise()
Expand Down
4 changes: 1 addition & 3 deletions test/ingest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ describe('ingest handler', () => {
expect(updateStub.mock.calls[0][0]).toEqual({
TableName: 'tasks-table',
Key: { taskId: 'test-task-id' },
UpdateExpression: 'SET #executeTime = :executeTime, #taskId = :taskId, #topicArn = :topicArn, #payload = :payload',
UpdateExpression: 'SET #executeTime = :executeTime, #topicArn = :topicArn, #payload = :payload',
ExpressionAttributeNames: {
'#executeTime': 'executeTime',
'#taskId': 'taskId',
'#topicArn': 'topicArn',
'#payload': 'payload'
},
ExpressionAttributeValues: {
':executeTime': 20,
':taskId': 'test-task-id',
':topicArn': 'arn:aws:sns:us-east-1:123456789:test-topic',
':payload': { foo: 'bar' }
}
Expand Down
20 changes: 9 additions & 11 deletions test/schedule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ afterEach(() => {
})

describe('schedule handler', () => {
let publishStub, queryStub, deleteStub
let publishStub, scanStub, deleteStub
beforeEach(() => {
process.env.TASKS_TABLE = 'tasks-table'
publishStub = jest.fn((params, callback) => callback(null, 'Success'))
Expand All @@ -22,7 +22,7 @@ describe('schedule handler', () => {
})
describe('when there are tasks in the past that need to be executed', () => {
beforeEach(() => {
queryStub = jest.fn((params, callback) => callback(null, {
scanStub = jest.fn((params, callback) => callback(null, {
Items: [
{
taskId: 'test-task-one',
Expand All @@ -36,20 +36,18 @@ describe('schedule handler', () => {
}
]
}))
AWS.mock('DynamoDB.DocumentClient', 'query', queryStub)
AWS.mock('DynamoDB.DocumentClient', 'scan', scanStub)
return handler()
})
it('queries for the items properly', () => {
expect(queryStub.mock.calls.length).toEqual(1)
expect(queryStub.mock.calls[0][0]).toEqual({
it('scans for the items properly', () => {
expect(scanStub.mock.calls.length).toEqual(1)
expect(scanStub.mock.calls[0][0]).toEqual({
TableName: 'tasks-table',
KeyConditionExpression: '#taskId != :taskId AND #executeTime <= :executeTime',
FilterExpression: '#executeTime <= :executeTime',
ExpressionAttributeNames: {
'#taskId': 'taskId',
'#executeTime': 'executeTime'
},
ExpressionAttributeValues: {
':taskId': '',
':executeTime': 20
}
})
Expand Down Expand Up @@ -79,10 +77,10 @@ describe('schedule handler', () => {
})
describe('when there are no tasks in the past that need to be executed', () => {
beforeEach(() => {
queryStub = jest.fn((params, callback) => callback(null, {
scanStub = jest.fn((params, callback) => callback(null, {
Items: []
}))
AWS.mock('DynamoDB.DocumentClient', 'query', queryStub)
AWS.mock('DynamoDB.DocumentClient', 'scan', scanStub)
return handler()
})
it('does nothing', () => {
Expand Down

0 comments on commit deec40b

Please sign in to comment.