Skip to content

feat: google cloud platform pubsub plugin integration #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config.js.docker
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ for (const x of seeds) {
// Multisig configuration and validation
const multisigPubkeys = getParam(`multisig_seed_${x}_pubkeys`);
const multisig = {
total: getParam(`multisig_seed_${x}_max_signatures`),
numSignatures: getParam(`multisig_seed_${x}_num_signatures`),
total: getIntParam(`multisig_seed_${x}_max_signatures`),
numSignatures: getIntParam(`multisig_seed_${x}_num_signatures`),
};
if (!(multisigPubkeys || multisig.total || multisig.numSignatures)) {
// No multisig parameters means this is not a multisig.
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/child.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export const hathorPlugins = {
name: 'rabbitmq',
file: 'hathor_rabbitmq.js',
},
pubsub: {
name: 'pubsub',
file: 'hathor_pubsub.js',
},
};

/**
Expand Down
78 changes: 78 additions & 0 deletions src/plugins/hathor_pubsub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright (c) Hathor Labs and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/* istanbul ignore next */
async function checkDeps() {
const requiredDeps = {
'@google-cloud/pubsub': '^4.0.1',
yargs: '^16.2.0',
};
await Promise.all(Object.keys(requiredDeps).map(async d => {
try {
await import(d);
} catch (e) {
throw new Error(`Some plugin dependencies are missing, to install them run:
$ npm install ${Object.entries(requiredDeps).map(x => [x[0], x[1]].join('@')).join(' ')}`);
}
})).catch(e => {
console.error(e.message);
process.exit(127);
});
}

export function getSettings() {
const yargs = require('yargs/yargs'); // eslint-disable-line global-require
const { hideBin } = require('yargs/helpers'); // eslint-disable-line global-require
const { argv } = yargs(hideBin(process.argv));

Check warning on line 30 in src/plugins/hathor_pubsub.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/hathor_pubsub.js#L28-L30

Added lines #L28 - L30 were not covered by tests

const projectId = argv.plugin_pubsub_project || process.env.HEADLESS_PLUGIN_PUBSUB_PROJECT;
const topicName = argv.plugin_pubsub_topic_name
|| process.env.HEADLESS_PLUGIN_PUBSUB_TOPIC_NAME || null;

const pubsubConfig = { apiVersion: '2012-11-05', projectId, topicName };

Check warning on line 36 in src/plugins/hathor_pubsub.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/hathor_pubsub.js#L36

Added line #L36 was not covered by tests

return pubsubConfig;

Check warning on line 38 in src/plugins/hathor_pubsub.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/hathor_pubsub.js#L38

Added line #L38 was not covered by tests
}

export function eventHandlerFactory(topic) {
return message => {
console.log(message);
topic.publishMessage({ json: message }, err => {

Check warning on line 44 in src/plugins/hathor_pubsub.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/hathor_pubsub.js#L42-L44

Added lines #L42 - L44 were not covered by tests
if (err) {
console.log(`plugin[pubsub] error sending to pubsub: ${err}`);

Check warning on line 46 in src/plugins/hathor_pubsub.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/hathor_pubsub.js#L46

Added line #L46 was not covered by tests
}
});
};
}

/* istanbul ignore next */
export const init = async bus => {
await checkDeps();
const settings = getSettings();
// eslint-disable-next-line global-require,import/no-extraneous-dependencies,import/no-unresolved
const { PubSub } = require('@google-cloud/pubsub');
const { projectId } = settings;
console.log(projectId);
const pubsub = new PubSub({ projectId });
const [topics] = await pubsub.getTopics();
let topic = topics
.find(t => t.name === `projects/${settings.projectId}/topics/${settings.topicName}`);

if (!topic) {
pubsub.createTopic(settings.topicName, (err, newTopic, apiResponse) => {
if (err) {
console.error(`plugin[pubsub] unable to create topic: ${err}`);
return;
}
topic = newTopic;
console.info(apiResponse);
});
}

bus.on('message', eventHandlerFactory(topic));
console.log('plugin[pubsub] loaded');
};
Loading