Spawn a process from a storage snapshot and continue to snapshot at regular intervals
npm install @hyperdivision/snapshot-spawn
const Spawn = require('@hyperdivision/snapshot-spawn')
const Snapshot = require('@hyperdivision/s3-snapshot')
const S3 = require('s3-storage')
const s3 = new S3('fs://fake-s3')
const snapshot = new Snapshot('data', 'data-snapshot.tar', s3)
const s = new Spawn('node', ['-e', `
let tick = 0
try {
tick = Number(fs.readFileSync('data/tick'), 'utf-8') || 0
} catch (_) {}
setInterval(function () {
console.log('tick', tick++)
if (!fs.existsSync('data')) fs.mkdirSync('data')
fs.writeFileSync('data/tick', '' + tick)
}, 1000)
`], {
snapshot,
interval: 3000
})
s.on('stdout', data => process.stdout.write(data))
s.on('stderr', data => process.stderr.write(data))
s.on('download', () => console.log('Downloading a snapshot'))
s.on('snapshot', () => console.log('Taking a snapshot'))
Create a new instance.
-
cmd
is the command you want to spawn. -
args
is the array of arguments you want to pass.
Options include
{
snapshot: <s3-snapshot-instance>, // required
interval: ..., // how often should it snapshot in ms? defaults to 24h
rollbacks: 0 // how many rollback to previous versions should be attempted?
}
Kill the process with a signal. Waits for a pending snapshot to finish.
The PID of the running process.
Emitted when there data from stdout.
Emitted when there data from stderr.
Emitted when a critical error happens.
Emitted when the process exits fully.
Emitted when a snapshot is being downloaded.
Emitted when a snapshot is being performed. Before a snapshot happens your process will be killed with SIGTERM.
Emitted when a process is spawned.
Emitted when a rollback is attempted.
Exit the process with this code (114) to attempt a rollback.