Created
November 27, 2019 18:10
-
-
Save chikka/a0a7637a5924da39efb7f3432b8da54f to your computer and use it in GitHub Desktop.
cube.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.env.AWS_REGION = "eu-west-1" | |
const aws = require('aws-sdk'); | |
const sns = new aws.SNS(); | |
const cors = require('cors'); | |
const handler = require('serverless-express/handler'); | |
const secretManager = require('./awsSecretService.js') | |
const express = require('serverless-express/express'); | |
const ServerCore = require('@cubejs-backend/server-core'); | |
const processHandlers = { | |
queryProcess: async (queryKey, orchestrator) => { | |
const queue = orchestrator.queryCache.getQueue(); | |
await queue.processQuery(queryKey); | |
}, | |
queryCancel: async (query, orchestrator) => { | |
const queue = orchestrator.queryCache.getQueue(); | |
await queue.processCancel(query); | |
}, | |
externalQueryProcess: async (queryKey, orchestrator) => { | |
const queue = orchestrator.queryCache.getExternalQueue(); | |
await queue.processQuery(queryKey); | |
}, | |
externalQueryCancel: async (query, orchestrator) => { | |
const queue = orchestrator.queryCache.getExternalQueue(); | |
await queue.processCancel(query); | |
}, | |
preAggregationProcess: async (queryKey, orchestrator) => { | |
const queue = orchestrator.preAggregations.getQueue(); | |
await queue.processQuery(queryKey); | |
}, | |
preAggregationCancel: async (query, orchestrator) => { | |
const queue = orchestrator.preAggregations.getQueue(); | |
await queue.processCancel(query); | |
} | |
}; | |
var options = {} | |
var serverCore = null | |
var apiHandler = null | |
module.exports.api = async function (event, context) { | |
options = { | |
orchestratorOptions: (context) => ({ | |
queryCacheOptions: { | |
queueOptions: { | |
sendProcessMessageFn: async (queryKey) => sendNotificationMessage(queryKey, 'queryProcess', context), | |
sendCancelMessageFn: async (query) => sendNotificationMessage(query, 'queryCancel', context) | |
}, | |
externalQueueOptions: { | |
sendProcessMessageFn: async (queryKey) => sendNotificationMessage(queryKey, 'externalQueryProcess', context), | |
sendCancelMessageFn: async (query) => sendNotificationMessage(query, 'externalQueryCancel', context) | |
} | |
}, | |
preAggregationsOptions: { | |
queueOptions: { | |
sendProcessMessageFn: async (queryKey) => sendNotificationMessage(queryKey, 'preAggregationProcess', context), | |
sendCancelMessageFn: async (query) => sendNotificationMessage(query, 'preAggregationCancel', context) | |
} | |
} | |
}), | |
...options | |
}; | |
const fn = getApiHandler(); | |
return fn.call(event, context) | |
} | |
module.exports.process = async function (event, context) { | |
options = { | |
orchestratorOptions: (context) => ({ | |
queryCacheOptions: { | |
queueOptions: { | |
sendProcessMessageFn: async (queryKey) => sendNotificationMessage(queryKey, 'queryProcess', context), | |
sendCancelMessageFn: async (query) => sendNotificationMessage(query, 'queryCancel', context) | |
}, | |
externalQueueOptions: { | |
sendProcessMessageFn: async (queryKey) => sendNotificationMessage(queryKey, 'externalQueryProcess', context), | |
sendCancelMessageFn: async (query) => sendNotificationMessage(query, 'externalQueryCancel', context) | |
} | |
}, | |
preAggregationsOptions: { | |
queueOptions: { | |
sendProcessMessageFn: async (queryKey) => sendNotificationMessage(queryKey, 'preAggregationProcess', context), | |
sendCancelMessageFn: async (query) => sendNotificationMessage(query, 'preAggregationCancel', context) | |
} | |
} | |
}), | |
...options | |
}; | |
const fn = getApiHandler(); | |
fn.call(event, context) | |
await Promise.all(event.Records.map(async record => { | |
const message = JSON.parse(record.Sns.Message); | |
await processMessage(message); | |
})); | |
return { | |
statusCode: 200 | |
}; | |
} | |
function topicArn(topic) { | |
return `arn:aws:sns:${process.env.AWS_REGION}:${process.env.AWS_ACCOUNT_ID}:${topic}`; | |
} | |
// eslint-disable-next-line no-unused-vars | |
async function sendNotificationMessage(message, type, context) { | |
throw new Error(`sendNotificationMessage is not implemented`); | |
} | |
async function processMessage(message) { | |
const processFn = processHandlers[message.type]; | |
if (!processFn) { | |
throw new Error(`Unrecognized message type: ${message.type}`); | |
} | |
const orchestratorApi = serverCore.getOrchestratorApi(message.context); | |
await processFn(message.message, orchestratorApi.orchestrator); | |
} | |
async function sendNotificationMessage(message, type, context) { | |
const params = { | |
Message: JSON.stringify({ message, type, context }), | |
TopicArn: topicArn(`${process.env.CUBEJS_APP || 'cubejs'}-process`) | |
}; | |
await sns.publish(params).promise(); | |
} | |
// function getApiHandler() { | |
// if (!apiHandler) { | |
// console.log("getApiHandler") | |
// serverCore = new ServerCore(options); | |
// const app = express(); | |
// app.use(cors()); | |
// serverCore.initApp(app); | |
// apiHandler = handler(app); | |
// } | |
// return apiHandler; | |
// } | |
async function getApiHandler() { | |
if (!apiHandler) { | |
console.log("getApiHandler") | |
const secret = await secretManager.getSecretValue("secretName") | |
process.env.CUBEJS_DB_HOST = secret.host | |
process.env.CUBEJS_DB_NAME = "test" | |
process.env.CUBEJS_DB_USER = secret.username | |
process.env.CUBEJS_DB_PASS = secret.password | |
process.env.CUBEJS_DB_PORT = secret.port | |
const app = express(); | |
app.use(cors()); | |
serverCore = new ServerCore(options); | |
serverCore.initApp(app); | |
apiHandler = handler(app); | |
} | |
return apiHandler; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment