0.2.2 • Published 4 years ago

airblast v0.2.2

Weekly downloads
45
License
NoHarm
Repository
-
Last release
4 years ago

Airblast provides a simple framework to get cloud function jobs with retries up and running quickly so you can focus on building your jobs with minimal effort on interacting with cloud infrastructure.

Airblast handles dealing with pubub, express and datastore and you just handle processing the data.

The framework allows for building controllers that send jobs to one another for processing so you can logically breakup your background processing by logical concerns.

See the samples for examples of setting up simple controllers

Example of job chains

class JobController extends AirblastController {
  // Will return 400 if someone tries to post an invalid payload
  validate({ data }) {
    if (!data.about) {
      throw new this.AppError(400, 'validation', 'Data must contain about attribute');
    }
  }

  // Send the job to the email and airtable controller for processing
  async process({ key }) {
    return Promise.all([
      // Pass the key of this record to the other controllers
      this.controllers.email.enqueue({ key }),
      this.controllers.airtable.enqueue({ key }),      
    ])
  }
}

class EmailController extends AirblastController {
  async process({ data }) {
    // Load the record that was passed in
    const payload = await this.load(data.key);
    sendEmail({
      to: 'admin@myco.example',
      subject: 'Job received',
      body: `The job is about ${data.about}`
    });
  }
}

class AirtableController extends AirblastController {
  async process({ data }) {
    const payload = await this.load(data.key);
    airtables.insert(payload);
  }
}

That's it, once you've written your process function, you've written cloud functions that are ready to deploy! See the sample directory for scripts to deploy them.

Setting up for deployment

  1. Install gcloud command line
gcloud auth login
gcloud config set project <your project id>
# List available zones to deploy functions
gcloud functions regions list
gcloud config set functions/region <preferred region>
# If you are also deploying the retry cron server, you will
# probably want to set the default app engine zone to the same
gcloud compute project-info add-metadata --metadata google-compute-default-region=<preferred region>
# Re-run gcloud init to make use of your new defaults
gcloud init

Testing

Most of the time you can test your controller simply by running controller.process(body) but if you need to test the webhook endpoint with authorization and validation, there is a helper to help you:

const runController = require('airblast/test/runController');

await runController(new MyController(), {
  body: { my: 'value' },
});

Options and default values that you can pass to

{
  // The controller function to execute (defaults to the function that receives and routes
  // the http requests, usuall you don't need to change this)
  function: 'http',
  // Mock the enqueue (avoids the need for datastore & pubsub emulators)
  mockEnqueue: true,
  // Throw an error if the response status is not 200
  throwOnError: true,
}
0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago