Skip to content

Instantly share code, notes, and snippets.

@klundberg
Created November 14, 2019 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klundberg/4cfc4998d713c99e3d7337915e598808 to your computer and use it in GitHub Desktop.
Save klundberg/4cfc4998d713c99e3d7337915e598808 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
// helper functions
function getTransitionDefinitions(transitions) {
return transitions.reduce(
(transitionsSoFar, transition) => ({
...transitionsSoFar,
[transition]: TransitionDefinitions[transition]
}),
{}
);
};
const StateMachineContextNames = {
START_TIME_CURRENT_STATE: 'startTimeCurrentState',
TAG: 'tag',
SELECTED_DEPLOY_REQUEST_IDS: 'selectedDeployRequestIds',
MAESTRO: 'maestro',
ROLLBACK_START_TIME: 'rollbackStartTime',
HAS_BEEN_DEPLOYED_TO_PROD: 'hasBeenDeployedToProd'
};
const ActionNames = {
logError: 'logError',
logEnteredState: 'logEnteredState',
logExitedState: 'logExitedState',
logEventTransition: 'logEventTransition'
};
const GuardNames = {
childMachineSucceeded: 'childMachineSucceeded'
};
const States = {
waitingForInteraction: 'waitingForInteraction',
creatingBatch: 'creatingBatch',
integratingDeployRequests: 'integratingDeployRequests',
building: 'building',
reverting: 'reverting',
deployingToCanary: 'deployingToCanary',
deployingToStaging: 'deployingToStaging',
deployingToProduction: 'deployingToProduction',
finalizing: 'finalizing',
canceling: 'canceling',
restarting: 'restarting',
mergingMasterIntoBatch: 'mergingMasterIntoBatch',
rollingback: 'rollingback',
done: 'done'
};
const Actions = {
setTagInContext: 'setTagInContext',
logDeployRequestsRolledBack: 'logDeployRequestsRolledBack',
setRollbackStartTimeInContext: 'setRollbackStartTimeInContext',
logRollbackDuration: 'logRollbackDuration',
...ActionNames
};
const Guards = {
...GuardNames,
autoDeployToStaging: 'autoDeployToStaging'
};
const Services = {
createBatchMachine: 'createBatchMachine',
integrateDeployRequestsMachine: 'integrateDeployRequestsMachine',
buildMachine: 'buildMachine',
revertMachine: 'revertMachine',
deployCanaryMachine: 'deployCanaryMachine',
deployStagingMachine: 'deployStagingMachine',
deployProductionMachine: 'deployProductionMachine',
rollbackMachine: 'rollbackMachine',
finalizeMachine: 'finalizeMachine',
cancelBatchMachine: 'cancelBatchMachine',
restartBatchMachine: 'restartBatchMachine',
mergeMasterIntoBatchMachine: 'mergeMasterIntoBatchMachine'
};
const StateMachineEventMap = {
DEPLOY_TO_STAGING: 'deployToStaging',
TRIGGER_STAGING_TESTS: 'triggerStagingTests',
DEPLOY_TO_CANARY: 'deployToCanary',
DEPLOY_TO_PRODUCTION: 'deployToProduction',
ROLLBACK: 'rollback',
REVERT_DEPLOY_REQUESTS: 'revertDeployRequests',
FINALIZE_BATCH: 'finalizeBatch',
CANCEL_BATCH: 'cancelBatch',
RESTART_BATCH: 'restartBatch',
MERGE_MASTER_INTO_BATCH: 'mergeMasterIntoBatch',
RETRY_BUILD: 'retryBuild',
PUSH_TO_REMOTE: 'pushRepoToRemoteMachine',
TRIGGER_BRANCH_TESTS: 'triggeringBranchTests',
POLLING_STATUS: 'pollBuildStatusMachine',
DEPLOY_PIB_TO_STAGING: 'deployPibToStaging',
PUSH_MASTER_TO_REMOTE: 'pushMasterToRemote',
MERGE_INTEGRATION_BRANCH_INTO_MASTER: 'mergeIntegrationBranchIntoMaster'
};
const MachineNames = {
BATCH: 'batchMachine',
APP_BATCH: 'appBatchMachine',
BUILD: 'buildMachine',
DEPLOY_STAGING: 'deployStagingMachine',
FINALIZE_BATCH: 'finalizeBatchMachine',
MERGE_MASTER_INTO_BATCH: 'mergeMasterIntoBatchMachine'
};
const MachineNameEventMap = {
[MachineNames.BATCH]: [
StateMachineEventMap.DEPLOY_TO_STAGING,
StateMachineEventMap.DEPLOY_TO_CANARY,
StateMachineEventMap.DEPLOY_TO_PRODUCTION,
StateMachineEventMap.ROLLBACK,
StateMachineEventMap.REVERT_DEPLOY_REQUESTS,
StateMachineEventMap.FINALIZE_BATCH,
StateMachineEventMap.CANCEL_BATCH,
StateMachineEventMap.RESTART_BATCH,
StateMachineEventMap.MERGE_MASTER_INTO_BATCH,
StateMachineEventMap.MERGE_MASTER_INTO_BATCH
],
[MachineNames.BUILD]: [
StateMachineEventMap.RETRY_BUILD,
StateMachineEventMap.PUSH_TO_REMOTE,
StateMachineEventMap.TRIGGER_BRANCH_TESTS
],
[MachineNames.FINALIZE_BATCH]: [
StateMachineEventMap.PUSH_TO_REMOTE,
StateMachineEventMap.MERGE_INTEGRATION_BRANCH_INTO_MASTER
],
[MachineNames.DEPLOY_STAGING]: [StateMachineEventMap.TRIGGER_STAGING_TESTS]
};
const TransitionDefinitions = {
[StateMachineEventMap.DEPLOY_TO_STAGING]: {
actions: Actions.logEventTransition,
target: States.deployingToStaging
},
[StateMachineEventMap.DEPLOY_TO_CANARY]: {
actions: Actions.logEventTransition,
target: States.deployingToCanary
},
[StateMachineEventMap.DEPLOY_TO_PRODUCTION]: {
actions: Actions.logEventTransition,
target: States.deployingToProduction
},
[StateMachineEventMap.FINALIZE_BATCH]: {
actions: Actions.logEventTransition,
target: States.finalizing
},
[StateMachineEventMap.CANCEL_BATCH]: {
actions: Actions.logEventTransition,
target: States.canceling
},
[StateMachineEventMap.RESTART_BATCH]: {
actions: Actions.logEventTransition,
target: States.restarting
},
[StateMachineEventMap.REVERT_DEPLOY_REQUESTS]: {
actions: Actions.logEventTransition,
target: States.reverting
},
[StateMachineEventMap.MERGE_MASTER_INTO_BATCH]: {
actions: Actions.logEventTransition,
target: States.mergingMasterIntoBatch
},
[StateMachineEventMap.ROLLBACK]: {
actions: Actions.logEventTransition,
target: States.rollingback
}
};
const AppStateTransitionsMap = {
[States.waitingForInteraction]: getTransitionDefinitions([
StateMachineEventMap.REVERT_DEPLOY_REQUESTS,
StateMachineEventMap.ROLLBACK,
StateMachineEventMap.FINALIZE_BATCH,
StateMachineEventMap.CANCEL_BATCH,
StateMachineEventMap.RESTART_BATCH,
StateMachineEventMap.MERGE_MASTER_INTO_BATCH
]),
[States.creatingBatch]: getTransitionDefinitions([
StateMachineEventMap.CANCEL_BATCH,
StateMachineEventMap.RESTART_BATCH
]),
[States.integratingDeployRequests]: getTransitionDefinitions([
StateMachineEventMap.CANCEL_BATCH,
StateMachineEventMap.RESTART_BATCH
]),
[States.building]: getTransitionDefinitions([
StateMachineEventMap.REVERT_DEPLOY_REQUESTS,
StateMachineEventMap.CANCEL_BATCH,
StateMachineEventMap.RESTART_BATCH,
StateMachineEventMap.MERGE_MASTER_INTO_BATCH
]),
[States.reverting]: getTransitionDefinitions([
StateMachineEventMap.CANCEL_BATCH,
StateMachineEventMap.RESTART_BATCH
]),
[States.rollingback]: getTransitionDefinitions([
StateMachineEventMap.REVERT_DEPLOY_REQUESTS,
StateMachineEventMap.CANCEL_BATCH,
StateMachineEventMap.RESTART_BATCH,
StateMachineEventMap.MERGE_MASTER_INTO_BATCH
]),
[States.restarting]: getTransitionDefinitions([]),
[States.mergingMasterIntoBatch]: getTransitionDefinitions([
StateMachineEventMap.CANCEL_BATCH,
StateMachineEventMap.RESTART_BATCH
]),
[States.canceling]: getTransitionDefinitions([]),
[States.finalizing]: getTransitionDefinitions([]),
[States.done]: getTransitionDefinitions([])
};
const appBatchMachine = Machine({
strict: true,
id: MachineNames.APP_BATCH,
initiali: States.creatingBatch,
initial: States.creatingBatch,
context: {
[StateMachineContextNames.TAG]: null,
[StateMachineContextNames.SELECTED_DEPLOY_REQUEST_IDS]: null,
[StateMachineContextNames.MAESTRO]: null,
[StateMachineContextNames.START_TIME_CURRENT_STATE]: null,
[StateMachineContextNames.ROLLBACK_START_TIME]: null
},
states: {
[States.waitingForInteraction]: {
entry: Actions.logEnteredState,
exit: Actions.logExitedState,
on: AppStateTransitionsMap[States.waitingForInteraction]
},
[States.creatingBatch]: {
entry: Actions.logEnteredState,
exit: Actions.logExitedState,
invoke: {
id: 'createBatchMachine',
src: Services.createBatchMachine,
onDone: [
{
target: States.integratingDeployRequests,
cond: Guards.childMachineSucceeded,
actions: Actions.setTagInContext
},
{target: States.waitingForInteraction}
]
},
on: AppStateTransitionsMap[States.creatingBatch]
},
[States.integratingDeployRequests]: {
entry: Actions.logEnteredState,
exit: Actions.logExitedState,
invoke: {
id: 'integrateDeployRequestsMachine',
src: Services.integrateDeployRequestsMachine,
onDone: [
{
target: States.building,
cond: Guards.childMachineSucceeded
},
{target: States.waitingForInteraction}
]
},
on: AppStateTransitionsMap[States.integratingDeployRequests]
},
[States.building]: {
entry: Actions.logEnteredState,
exit: Actions.logExitedState,
invoke: {
id: 'buildMachine',
src: Services.buildMachine,
onDone: [
{
target: States.finalizing,
cond: Guards.childMachineSucceeded
},
{target: States.waitingForInteraction}
]
},
on: AppStateTransitionsMap[States.building]
},
[States.reverting]: {
entry: Actions.logEnteredState,
exit: Actions.logExitedState,
invoke: {
id: 'revertMachine',
src: Services.revertMachine,
onDone: [
{
target: States.building,
cond: Guards.childMachineSucceeded
},
{target: States.waitingForInteraction}
]
},
on: AppStateTransitionsMap[States.reverting]
},
[States.rollingback]: {
entry: [
Actions.logEnteredState,
Actions.logDeployRequestsRolledBack,
Actions.logDeployRequestsRolledBack,
Actions.setRollbackStartTimeInContext
],
exit: Actions.logExitedState,
invoke: {
id: 'rollbackMachine',
src: Services.rollbackMachine,
onDone: States.waitingForInteraction
},
on: AppStateTransitionsMap[States.rollingback]
},
[States.restarting]: {
entry: Actions.logEnteredState,
exit: Actions.logExitedState,
invoke: {
id: 'restartBatchMachine',
src: Services.restartBatchMachine,
onDone: [
{
target: States.creatingBatch,
cond: Guards.childMachineSucceeded
},
{target: States.waitingForInteraction}
]
},
on: AppStateTransitionsMap[States.restarting]
},
[States.mergingMasterIntoBatch]: {
entry: Actions.logEnteredState,
exit: Actions.logExitedState,
invoke: {
id: 'mergeMasterIntoBatchMachine',
src: Services.mergeMasterIntoBatchMachine,
onDone: [
{
target: States.building,
cond: Guards.childMachineSucceeded
},
{target: States.waitingForInteraction}
]
},
on: AppStateTransitionsMap[States.mergingMasterIntoBatch]
},
[States.canceling]: {
entry: Actions.logEnteredState,
exit: Actions.logExitedState,
invoke: {
id: 'cancelBatchMachine',
src: Services.cancelBatchMachine,
onDone: [
{
target: States.done,
cond: Guards.childMachineSucceeded
},
{target: States.waitingForInteraction}
]
},
on: AppStateTransitionsMap[States.canceling]
},
[States.finalizing]: {
entry: Actions.logEnteredState,
exit: Actions.logExitedState,
invoke: {
id: 'finalizeMachine',
src: Services.finalizeMachine,
onDone: [
{
target: States.done,
cond: Guards.childMachineSucceeded
},
{target: States.waitingForInteraction}
]
},
on: AppStateTransitionsMap[States.finalizing]
},
[States.done]: {
entry: Actions.logEnteredState,
exit: Actions.logExitedState,
type: 'final'
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment