-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdock.attach.js
More file actions
51 lines (45 loc) · 1.8 KB
/
dock.attach.js
File metadata and controls
51 lines (45 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict'
const Promise = require('bluebird')
const WorkerStopError = require('error-cat/errors/worker-stop-error')
const DockNotDetachedError = require('../errors/dock-not-detached-error')
require('loadenv')({ project: 'shiva', debugName: 'astral:shiva:env' })
const AutoScalingGroup = require('../models/auto-scaling-group')
const AWSAlreadyPartOfASGError = require('../errors/aws-already-part-of-asg-error')
const publisher = require('../../common/models/astral-rabbitmq')
const schemas = require('../../common/models/schemas.js')
/**
* Task handler for the `dock.attach` task.
* @module astral:shiva:tasks
*/
const DockAttach = {}
module.exports = DockAttach
// takes around 3 seconds to detach so start the wait there
DockAttach.retryDelay = 3000
/**
* 1. Get the IP and OrgId from the dock disassociate task
* 2. Add that dock to the organization's auto-scaling group
*/
DockAttach.jobSchema = schemas.dockAttach
DockAttach.task = (job) => {
const targetASGName = process.env.AWS_AUTO_SCALING_GROUP_PREFIX + job.githubOrgId
return Promise.try(() => {
const instanceIds = [job.instanceId]
return AutoScalingGroup.attachInstance(targetASGName, instanceIds)
})
.catch(AWSAlreadyPartOfASGError, (err) => {
if (err.getCurrentASG() === targetASGName) {
throw new WorkerStopError('instance already attached to asg', { err, targetASGName })
}
if (!(new RegExp('is already part of AutoScalingGroup:' + process.env.DOCK_POOL_ASG_NAME).test(err.data.originalError.message))) {
throw new DockNotDetachedError('Instance still not detached from pool', err, { level: 'info' })
}
throw err;
})
.then(() => {
return publisher.publishTask('dock.initialize', {
autoScalingGroupName: targetASGName,
instanceId: job.instanceId,
githubOrgId: job.githubOrgId
})
})
}