Skip to content

Commit

Permalink
fix: load iot endpoint async on lambda init (for lambdas that might s…
Browse files Browse the repository at this point in the history
…end messages).
  • Loading branch information
mvayngrib committed Dec 16, 2017
1 parent 53a92d3 commit 9a2b5c5
Show file tree
Hide file tree
Showing 27 changed files with 201 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/bot/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/bot/lambda/onmessage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/bot/lambda/onmessagestream.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions lib/errors.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/lambda/http/inbox.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/lambda/http/onmessage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/lambda/mqtt/onconnect.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/lambda/mqtt/onsubscribe.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions lib/push.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/task-manager.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions lib/test/utils.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/user.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 18 additions & 4 deletions lib/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions serverless-uncompiled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ functions:

inbox:
handler: lib/lambda/http/inbox.handler
memorySize: 1024
memorySize: 2048
events:
- http:
path: inbox
Expand Down Expand Up @@ -714,7 +714,7 @@ functions:
# and passes off to bot engine
onmessage:
handler: lib/lambda/mqtt/onmessage.handler
memorySize: 1024
memorySize: 2048
# BOT_ONMESSAGE: ${{self:custom.bot_onmessage}}
# SERVERLESS_PREFIX: ${{self:custom.prefix}}
# SERVERLESS_STAGE: ${{self:custom.stage}}
Expand Down Expand Up @@ -921,7 +921,7 @@ functions:
# make sure graphql route gets loaded
handler: lib/samplebot/lambda/http/graphql.handler
# handler: lib/lambda/http/default.handler
memorySize: 512
memorySize: 1024
events:
- http:
path: graphql
Expand Down
2 changes: 1 addition & 1 deletion src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const { TYPE, SIG } = constants
const promisePassThrough = data => Promise.resolve(data)

const PROXY_TO_TRADLE = [
'aws', 'objects', 'db', 'dbUtils', 'lambdaUtils', 'seals',
'aws', 'objects', 'db', 'dbUtils', 'lambdaUtils', 'iot', 'seals',
'identities', 'history', 'messages', 'friends',
'resources', 'env', 'router', 'buckets', 'tables',
'serviceMap', 'version', 'apiBaseUrl', 'tasks'
Expand Down
5 changes: 5 additions & 0 deletions src/bot/lambda/onmessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export const createLambda = (opts) => {
}

export const outfitLambda = (lambda, opts) => {
lambda.tasks.add({
name: 'getiotendpoint',
promiser: lambda.bot.iot.getEndpoint
})

lambda.use(onmessage(lambda, opts))
return lambda
}
5 changes: 5 additions & 0 deletions src/bot/lambda/onmessagestream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export const outfitLambda = (lambda, opts) => {
}
}

lambda.tasks.add({
name: 'getiotendpoint',
promiser: bot.iot.getEndpoint
})

lambda.use(async (ctx, next) => {
const { event } = ctx
event.bot = bot
Expand Down
16 changes: 10 additions & 6 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ const rethrow = (err, type) => {
}
}

const HttpError = createError('HttpError')
const _HttpError = createError('HttpError')
class HttpError extends Error {
public status: number
constructor(code, message) {
super(message)
this.status = code || 500
}
}

const errors = {
ClientUnreachable: createError('ClientUnreachable'),
NotFound: createError('NotFound'),
Expand All @@ -96,11 +104,7 @@ const errors = {
TimeTravel: createError('TimeTravel'),
ExecutionTimeout: createError('ExecutionTimeout'),
Exists: createError('Exists'),
HttpError: (code, message) => {
const err = new HttpError(message)
err.status = code
return err
},
HttpError,
Timeout: createError('Timeout'),
export: (err:Error): {
type:string,
Expand Down
5 changes: 5 additions & 0 deletions src/lambda/http/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const lambda = new Lambda({
tradle
})

lambda.tasks.add({
name: 'getiotendpoint',
promiser: tradle.iot.getEndpoint
})

lambda.use(cors())
lambda.use(bodyParser({ jsonLimit: '10mb' }))

Expand Down
5 changes: 5 additions & 0 deletions src/lambda/http/onmessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const lambda = new Lambda({
tradle
})

lambda.tasks.add({
name: 'getiotendpoint',
promiser: tradle.iot.getEndpoint
})

const messageHandler = async (ctx) => {
const { message } = ctx.event
// the user sent us a message
Expand Down
5 changes: 5 additions & 0 deletions src/lambda/mqtt/onconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const lambda = new Lambda({
tradle
})

lambda.tasks.add({
name: 'getiotendpoint',
promiser: tradle.iot.getEndpoint
})

lambda.use(async ({ event, context }) => {
lambda.logger.debug('client connected', event)
const { clientId } = event
Expand Down
5 changes: 5 additions & 0 deletions src/lambda/mqtt/onsubscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const lambda = new Lambda({
tradle
})

lambda.tasks.add({
name: 'getiotendpoint',
promiser: lambda.tradle.iot.getEndpoint
})

lambda.use(async ({ event, context }) => {
const { clientId, topics } = event
await tradle.user.onSubscribed({ clientId, topics })
Expand Down

0 comments on commit 9a2b5c5

Please sign in to comment.