Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const {
- send,
- } = require('xstate');
- const debug = require('debug')('TASK_STATE_MACHINE.JS');
- const runTaskStates = {
- id: 'runTaskFSM',
- initial: 'pullPackage',
- states: {
- pullPackage: {
- entry: ['reportMachineStart', 'pullPackage'],
- on: {
- MAKE_IO_DIR: {
- target: 'mkIODir',
- },
- },
- },
- mkIODir: {
- entry: ['mkIODir'],
- on: {
- WRITE_TASK_JSON: {
- target: 'writeTaskDataJSON',
- },
- },
- },
- writeTaskDataJSON: {
- entry: ['writeTaskDataJSON'],
- on: {
- DOWNLOAD_INPUTS: {
- target: 'downloadInputs',
- },
- },
- },
- downloadInputs: {
- entry: ['downloadInputs'],
- on: {
- LINK_INPUTS: {
- target: 'linkInputs',
- },
- },
- },
- linkInputs: {
- entry: ['linkInputs'],
- on: {
- DOWNLOAD_RESOURCES: {
- target: 'downloadResources',
- },
- },
- },
- downloadResources: {
- entry: ['downloadResources'],
- on: {
- TASKDIR_PERMISSIONS: {
- target: 'chmodTaskDir',
- },
- },
- },
- chmodTaskDir: {
- entry: ['chmodTaskDir'],
- on: {
- JOB_TIMEOUT: {
- target: 'jobTimeout',
- },
- },
- },
- jobTimeout: {
- entry: ['jobTimeout'],
- on: {
- INIT_HANDLER: {
- target: 'initHandler',
- },
- },
- },
- initHandler: {
- entry: ['initHandler'],
- on: {
- START_JOB: {
- target: 'startJob',
- },
- },
- },
- startJob: {
- entry: ['reportUserStart', 'taskRunning', 'startJob'],
- on: {
- TASK_RUNNING: {
- target: 'taskRunning',
- },
- },
- },
- taskRunning: {
- entry: [send('RUNNING')],
- },
- },
- };
- module.exports = {
- id: 'runner',
- initial: 'setup',
- strict: true,
- states: {
- setup: {
- entry: [() => {
- debug('State -> setup');
- }],
- on: {
- ERROR: {
- target: 'error',
- },
- TERMINATED: {
- target: 'terminated',
- },
- RUNNING: {
- target: 'running',
- },
- HANDLER_EXIT: {
- target: 'handleExit',
- },
- },
- ...runTaskStates,
- },
- running: {
- entry: [() => {
- debug('State -> running');
- }],
- on: {
- HANDLER_EXIT: {
- target: 'handleExit',
- },
- ERROR: {
- target: 'error',
- },
- TERMINATED: {
- target: 'terminated',
- },
- },
- },
- handleExit: {
- entry: [() => debug('State -> handleExit'), 'reportUserStop', 'taskHandleExit'],
- on: {
- COMPLETE: {
- target: 'complete',
- },
- ERROR: {
- target: 'error',
- },
- },
- },
- complete: {
- entry: [() => debug('State -> complete'), 'taskComplete'],
- on: {
- DESTROY: {
- target: 'destroy',
- },
- },
- },
- error: {
- entry: ['taskError'],
- on: {
- DESTROY: {
- target: 'destroy',
- },
- },
- },
- terminated: {
- entry: ['reportUserStop', 'terminate'],
- on: {
- DESTROY: {
- target: 'destroy',
- },
- },
- },
- destroy: {
- entry: ['reportMachineStop', 'destroyTask'],
- type: 'final',
- },
- },
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement