项目作者: SplitmediaLabsLimited

项目描述 :
A Google Cloud Pubsub client for node.js geared towards queues and jobs.
高级语言: JavaScript
项目地址: git://github.com/SplitmediaLabsLimited/pubsub-queue.git
创建时间: 2019-02-26T12:33:12Z
项目社区:https://github.com/SplitmediaLabsLimited/pubsub-queue

开源协议:

下载


PubsubQueue

A Google Cloud Pubsub client for node.js geared towards queues and jobs. Inspired by ceejbot/fivebeans

Installation

Node 8+ required

  1. yarn add @splitmedialabs/pubsub-queue

Usage

Pre-requisite

  • a GCP account and project
  • a Pubsub Topic for the main jobs
    • a Subscription for this topic
  • a Pubsub topic for the failed jobs
  • a JSON keyFilename with correct IAM permissions for PubSub

Publishing jobs

  1. import PubsubQueue from '@splitmedialabs/pubsub-queue';
  2. const Pubsub = new PubsubQueue(
  3. {
  4. // connection config
  5. projectId: 'my-gcp-project-id',
  6. keyFilename: '~/gcp.json',
  7. },
  8. {
  9. // topics and subscriptions config
  10. topicName: 'worker-test', // name of the default topicName for the jobs
  11. subscriptionName: 'test-sub', // name of the subscription under the topic
  12. buriedTopicName: 'worker-test-buried', // Optional, name of the buried topics. When a job fails, it'll get published here.
  13. }
  14. );
  15. // minimal job publishing. This will publish the job to the default topicName
  16. Pubsub.Publisher.publish({
  17. type: 'hello', // name of the handler
  18. payload: {
  19. hello: 'world! simple',
  20. }, // arbitrary payload. Will be serialized to JSON
  21. });
  22. // all bells and whistle
  23. Pubsub.Publisher.publish({
  24. type: 'hello-fail', // name of the handler
  25. payload: {
  26. hello: 'world delayed',
  27. }, // arbitrary payload. Will be serialized to JSON
  28. delayed: {
  29. // job will only be executed after this date
  30. unit: 'seconds',
  31. value: '10',
  32. },
  33. });
  34. // custom topic
  35. Pubsub.Publisher.publish('custom-topic-name', {
  36. type: 'hello-fail', // name of the handler
  37. payload: {
  38. hello: 'world delayed',
  39. }, // arbitrary payload. Will be serialized to JSON
  40. // will only sttart after this date
  41. delayed: new Date(new Date().getTime() + 10000).toISOString(),
  42. });

Workers

  1. // # handlers/hello.ts
  2. export default {
  3. async work(payload) {
  4. console.log('job-handler', { payload });
  5. return; // any return means success
  6. },
  7. };
  8. // # handlers/hello-repeat.ts
  9. export default {
  10. async work(payload) {
  11. console.log('job-handler', { payload });
  12. return 'put'; // the job will be succesful but will be put back on the queue
  13. },
  14. };
  15. // # handlers/hello-fail.ts
  16. export default {
  17. retries: {
  18. count: 5, // how many times to retry this job
  19. delay: 1000, // delay between each retries
  20. },
  21. async work(payload) {
  22. console.log('job-handler', { payload });
  23. throw new Error('Fake Error!'); // throwing will fail the job
  24. },
  25. };
  26. // # index.ts
  27. import PubsubQueue from '@splitmedialabs/pubsub-queue';
  28. const Pubsub = new PubsubQueue(
  29. {
  30. // connection config
  31. projectId: 'my-gcp-project-id',
  32. keyFilename: '~/gcp.json',
  33. },
  34. {
  35. // topics and subscriptions config
  36. topicName: 'worker-test',
  37. buriedTopicName: 'worker-test-buried',
  38. subscriptionName: 'test-sub',
  39. }
  40. );
  41. const handlers = {
  42. hello: require('./handlers/hello'),
  43. 'hello-repeat': require('./handlers/hello-repeat'),
  44. 'hello-fail': require('./handlers/hello-fail'),
  45. };
  46. Pubsub.Worker.start(handlers);

Attaching events handlers to workers

This is useful for statistics

  1. const handlers = {};
  2. Pubsub.Worker.on('job.reserved', (data) => console.log(data)); // when a job is starting
  3. Pubsub.Worker.on('job.handled', (data) => console.log(data)); // when a job is done
  4. Pubsub.Worker.on('job.buried', (data) => console.log(data)); // when a job has failed
  5. Pubsub.Worker.start(handlers);