Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

fix: remove components respect dependencies Close #16 #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
setDependencies,
createGraph,
executeGraph,
executeGraphRemove,
syncState,
getOutputs,
createCustomMethodHandler
Expand Down Expand Up @@ -71,11 +72,32 @@ class Template extends Component {
return outputs
}

async remove() {
async remove(inputs = {}) {
this.context.status('Removing')

this.context.debug('Flushing template state and removing all components.')
await syncState({}, this)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why syncState({}, this) is not enough to properly remove components?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because syncState() just remove all components at the same time, not analysis the dependency tree. For some relative components, should remove the relative component firstly, otherwise it will fail to remove.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because syncState() just remove all components at the same time,

But you've fixed that in that PR here: https://github.com/serverless/template/pull/17/files/bc2003dd824351b04cc40cb3ff9f3c1e436985c5#diff-14ef994fa167d355bdaf9b44add54b87R337

So my question is why is that fix not enough (?)


const template = await getTemplate(inputs)

this.context.debug(`Resolving the template's static variables.`)

const resolvedTemplate = resolveTemplate(template)

this.context.debug('Collecting components from the template.')

const allComponents = await getAllComponents(resolvedTemplate)

this.context.debug(`Analyzing the template's components dependencies.`)

const allComponentsWithDependencies = setDependencies(allComponents)

this.context.debug(`Creating the template's components graph.`)

const graph = createGraph(allComponentsWithDependencies)

this.context.debug(`Executing the template's components graph remove.`)

await executeGraphRemove(allComponentsWithDependencies, graph, this)

// todo should we return the removed components outputs here?!
return {}
Expand Down
69 changes: 69 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ const traverse = require('traverse')
const { utils } = require('@serverless/core')
const newMetric = require('@serverless/component-metrics')

function sleep(ms) {
return new Promise((res) => {
setTimeout(() => {
res(true)
}, ms)
})
}

const getComponentMetric = async (componentPath, componentMethod = 'default', instance) => {
const metric = newMetric()
metric.componentMethod(componentMethod)
Expand Down Expand Up @@ -341,6 +349,66 @@ const syncState = async (allComponents, instance) => {
await instance.save()
}

const executeGraphRemove = async (allComponents, graph, instance) => {
const leaves = graph.sinks()
if (isEmpty(leaves)) {
return allComponents
}

const preorderGraph = (gp, res = []) => {
const lvs = gp.sinks()
if (lvs && lvs.length > 0) {
lvs.forEach((lv) => {
if (res.indexOf(lv) === -1) {
res.push(lv)
}
gp.removeNode(lv)
})

return preorderGraph(gp, res)
}
return res.reverse()
}

const componets = preorderGraph(graph)

for (const alias of componets) {
let loopTime = 5
const fn = async () => {
const component = await instance.load(instance.state.components[alias], alias)
instance.context.status('Removing', alias)
const metric = await getComponentMetric(instance.state.components[alias], 'remove', instance)

// try remove, even through remove main node first,
// but the network status may need time to feel it.
// So should try to remove for loop
const tryRemove = async () => {
try {
await component.remove()
} catch (e) {
await metric.publish()
// on error, publish error metric
if (loopTime <= 0) {
metric.componentError(e.message)
throw e
} else {
await sleep(5000)
loopTime--
await tryRemove()
}
}
}

await tryRemove()
await metric.publish()
}

await fn()
}

await instance.save()
}

const createCustomMethodHandler = (instance, method) => {
return async ({ component, template, ...inputs }) => {
let components = Array.isArray(component) ? component : [component]
Expand Down Expand Up @@ -400,6 +468,7 @@ module.exports = {
setDependencies,
createGraph,
executeGraph,
executeGraphRemove,
syncState,
getOutputs,
createCustomMethodHandler
Expand Down