1.0.3 • Published 4 years ago

@jacobbubu/job-list v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

@jacobbubu/job-list

Build Status Coverage Status npm

Implementation of job data structure with scuttlebutt protocol.

Intro.

job-list is a data structure developed based on the scuttlebutt protocol to express job status and operations.

Usage

Sync version

import { link } from '@jacobbubu/scuttlebutt-pull'
import { JobList } from '../src'

const a = new JobList({ id: 'A' })
const b = new JobList({ id: 'B' })

const job = a.create({ id: 'job-1', initial: 'META-1' })
job.progress('STEP-1')
job.progress('STEP-2')
job.extra('EXTRA-1')
job.done(null, 'DONE')
job.extra('EXTRA-2')

console.log('job@A:', job.toJSON())

const s1 = a.createStream({ name: 's1' })
const s2 = b.createStream({ name: 's2' })

link(s1, s2)

b.on('createdByPeer', (jobId, initial, jobList, update) => {
  console.log(`job(${jobId}) created@B`, jobList.getJob(jobId)?.toJSON())
})

b.on('progressByPeer', (jobId, progress, jobList, update) => {
  console.log(`job(${jobId}) progress:`, progress)
})

b.on('extraByPeer', (jobId, progress, jobList, update) => {
  console.log(`job(${jobId}) extra:`, progress)
})

b.on('doneByPeer', (jobId, err, res, jobList, update) => {
  console.log(`job(${jobId}) done:`, [err, res])
})

Async version

import { link } from '@jacobbubu/scuttlebutt-pull'
import { AsyncJobList, SQLiteStore } from '../src'

const main = async () => {
  const tableName = 'JobList'

  const a = new AsyncJobList({
    id: 'A',
    store: new SQLiteStore({ filename: ':memory:' }, tableName),
  })
  const b = new AsyncJobList({
    id: 'B',
    store: new SQLiteStore({ filename: ':memory:' }, tableName),
  })

  const job = await a.create({ id: 'job-A', initial: 'META-1' })
  await job.progress('STEP-1')
  await job.progress('STEP-2')
  await job.extra('EXTRA-1')
  await job.done(null, 'DONE!')
  await job.extra('EXTRA-2')

  console.log('job@A:', job.toJSON())

  const s1 = a.createStream({ name: 's1' })
  const s2 = b.createStream({ name: 's2' })

  link(s1, s2)

  b.on('createdByPeer', async (jobId, initial, jobList, update) => {
    console.log(`job(${jobId}) created@B`, (await jobList.getJob(jobId))?.toJSON())
  })

  b.on('progressByPeer', async (jobId, progress, jobList, update) => {
    console.log(`job(${jobId}) progress:`, progress)
  })

  b.on('extraByPeer', async (jobId, progress, jobList, update) => {
    console.log(`job(${jobId}) extra:`, progress)
  })

  b.on('doneByPeer', async (jobId, err, res, jobList, update) => {
    console.log(`job(${jobId}) done:`, [err, res])
  })
}

// tslint:disable-next-line no-floating-promises
main()