Last active
November 10, 2018 22:49
-
-
Save KarneAsada/6935bf998e6c6a57796eef14bdac835e to your computer and use it in GitHub Desktop.
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
const config = require('../../config') | |
const cloudTasks = require('@google-cloud/tasks') | |
const queue = {} | |
queue.transcodeVideo = async (data) => { | |
const qClient = new cloudTasks.CloudTasksClient() | |
const task = { | |
appEngineHttpRequest: { | |
httpMethod : 'POST', | |
relativeUri : '/transcode/video', | |
headers : { 'Content-Type': 'application/json' }, | |
body : Buffer.from(JSON.stringify(data)).toString('base64'), | |
}, | |
} | |
if (config.VERSION) { | |
task.appEngineHttpRequest.appEngineRouting = { version: config.VERSION } | |
} | |
const parent = qClient.queuePath( | |
config.google.projectId, | |
config.google.location, | |
config.google.transcodeQ | |
) | |
const qRequest = { parent, task } | |
try { | |
const response = await qClient.createTask(qRequest) | |
console.info(`Created cloud task ${response[0].name}`) | |
return true | |
} catch (err) { | |
console.error(`Error in createTask: ${err.message || err}`) | |
return false | |
} | |
} | |
module.exports = queue |
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
const cloudTasks = require('@google-cloud/tasks') | |
const queue = require('../queue') | |
const config = require('../../../config') | |
const createTask = () => jest.fn() | |
const parent = 'mockParent' | |
const CloudTasksClient = () => ({ | |
createTask : () => createTask, | |
queuePath : () => parent | |
}) | |
jest.mock('@google-cloud/tasks', () => { CloudTasksClient }) | |
// jest.spyOn(cloudTasks, 'CloudTasksClient').mockImplementation(CloudTasksClient) | |
describe('The transcodeVideo method', () => { | |
test('should send a request to Cloud Tasks to queue a video transcode', async () => { | |
const data = { | |
videoId : 123, | |
workspaceID : 123, | |
} | |
const task = { | |
appEngineHttpRequest: { | |
httpMethod : 'POST', | |
relativeUri : '/transcode/video', | |
headers : { 'Content-Type': 'application/json' }, | |
body : Buffer.from(JSON.stringify(data)).toString('base64'), | |
}, | |
} | |
const input = { parent, task } | |
await queue.transcodeVideo(data) | |
expect(createTask).toHaveBeenCalledTimes(1) | |
expect(createTask).toHaveBeenCalledWith(input) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment