0.2.0-alpha.7 • Published 2 months ago

djorm-cloud-jobs v0.2.0-alpha.7

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

djorm-cloud-jobs

Tiny library that helps you run huge workloads as small distributed jobs in cloud environment.

For the moment, only Google Cloud Platform is supported.

Installation

npm install djorm-cloud-jobs

Configuration

Add djorm-cloud-jobs/config to apps djorm config.

const { configure } = require('djorm/config')

configure({
  apps: [
    'djorm-cloud-jobs/config'
  ], 
  jobs: {
    model: 'djorm-cloud-jobs.Job',
    local: process.env.NODE_ENV === 'local'
  }
})

jobs.model

(string, default 'gcpi-models-jobs.Job') name of the model used to store jobs. You can either use the Job model or extend the abstract JobBase model.

jobs.local

(boolean, default false) run the jobs locally (don't use that in production)

Usage

The jobs are expected to be run in Cloud Function environment. So first, you need to create an entrypoint.

const { createSubscription } = require('djorm-cloud-jobs')

module.exports = createSubscription({
  filename: __filename,
  topic: 'job-topic',
  tasks: job => {
    // process job.props
  }
})

The processing stores updates job status based on the outcome of the processing function.

Multiple job types

You can specify that the entrypoint will process multiple different types of Jobs. Good examples are scrapers, so let's scrape some pets API.

const { createSubscription } = require('djorm-cloud-jobs')

const ScrapeTriggers = {
  ownerList: 'load:owner:all', 
  ownerDetail: 'load:owner:detail',
  ownerPetList: 'load:owner:pet:all'
}

module.exports = createSubscription({
  filename: __filename,
  topic: 'job-topic',
  tasks: {
    [ScrapeTriggers.ownerList]: job => {
      // Fetch owner list and trigger details fetch for each owner
      await Promise.all(ownerList.map(owner => 
        job.spawnChild({
          props: {
            owner
          }
        })
      )
    },
    [ScrapeTriggers.ownerDetail]: job => {
      const { owner } = job.props
      // Fetch owner details for a specific owner
      // Store owner details
      // Trigger pets details fetch for each pet
      await Promise.all(owner.pets.map(pet => 
        job.spawnChild({
          props: {
            owner,
            pet
          }
        })
      )

    },
    [ScrapeTriggers.ownerPetList]: job => {
      const { owner, pet } = job.props
      // Now fetch and store the owner's pet's details
    }
  }
})

Job hooks

To make the jobs interact with each other, you can define hooks. Let's consider ScrapeTriggers from previous example. We want to trigger another job when the ScrapeTriggers.ownerList job ends successfully. Please note that it is considered successful only if all the descendants finish with success status.

const { createSubscription } = require('djorm-cloud-jobs')

const LocationTriggers = {
  petLocationHistory: 'load:pet:location-history'
}

module.exports = createSubscription({
  filename: __filename,
  topic: 'job-topic',
  tasks: {
    [ScrapeTriggers.ownerList]: {
      onRequest: job => {
        // Same as above
      },
      onSuccess: job => {
        job.constructor.debounce({
          type: LocationTriggers.fetchPetLocationHistory,
        })
      }
    }
  }
})

Deploying

  1. You need to create all the PubSub topics
  2. You need to create all the Cloud Functions with
  • all the required database env variables
  • entrypoint set to runJob
  1. Potentially other resources
0.2.0-alpha.7

2 months ago

0.2.0-alpha.4

10 months ago

0.2.0-alpha.5

10 months ago

0.2.0-alpha.2

11 months ago

0.2.0-alpha.1

11 months ago

0.2.0-alpha.3

11 months ago

0.1.19-alpha.18

11 months ago

0.2.0-alpha.0

11 months ago

0.1.19-alpha.10

1 year ago

0.1.19-alpha.5

1 year ago

0.1.19-alpha.12

1 year ago

0.1.19-alpha.11

1 year ago

0.1.19-alpha.8

1 year ago

0.1.19-alpha.7

1 year ago

0.1.19-alpha.15

1 year ago

0.1.19-alpha.4

2 years ago

0.1.19-alpha.1

2 years ago

0.1.19-alpha.0

2 years ago

0.1.19-alpha.2

2 years ago

0.1.18-alpha.1

2 years ago

0.1.18-alpha.0

2 years ago

0.1.17-alpha.1

2 years ago

0.1.17-alpha.2

2 years ago

0.1.16-alpha.6

2 years ago

0.1.16-alpha.0

2 years ago

0.1.16-alpha.1

2 years ago

0.1.16-alpha.3

2 years ago

0.1.15-alpha.0

2 years ago

0.1.16-alpha.4

2 years ago

0.1.14-alpha.8

2 years ago

0.1.14-alpha.9

2 years ago

0.1.14-alpha.6

3 years ago

0.1.14-alpha.5

3 years ago

0.1.14-alpha.4

3 years ago

0.1.14-alpha.2

3 years ago

0.1.14-alpha.0

3 years ago

0.1.14-alpha.1

3 years ago

0.1.13-alpha.7

3 years ago

0.1.13-alpha.6

3 years ago

0.1.13-alpha.5

3 years ago

0.1.13-alpha.2

3 years ago

0.1.13-alpha.0

3 years ago

0.1.12-alpha.0

3 years ago

0.1.12-alpha.1

3 years ago

0.1.11-alpha.0

3 years ago

0.1.11-alpha.1

3 years ago

0.1.10-alpha.2

3 years ago

0.1.10-alpha.1

3 years ago

0.1.10-alpha.0

3 years ago

0.1.10-alpha.3

3 years ago

0.1.9-alpha.6

3 years ago

0.1.9-alpha.5

3 years ago

0.1.9-alpha.4

3 years ago

0.1.9-alpha.3

3 years ago

0.1.9-alpha.1

3 years ago

0.1.9-alpha.0

3 years ago

0.1.8-alpha.0

3 years ago

0.1.7-alpha.12

3 years ago

0.1.7-alpha.11

3 years ago

0.1.7-alpha.10

3 years ago

0.1.7-alpha.7

3 years ago

0.1.7-alpha.8

3 years ago

0.1.7-alpha.6

3 years ago

0.1.7-alpha.5

3 years ago

0.1.7-alpha.4

3 years ago

0.1.7-alpha.3

3 years ago

0.1.7-alpha.2

3 years ago

0.1.7-alpha.1

3 years ago