Skip to content

Instantly share code, notes, and snippets.

@nrempel
Last active May 25, 2017 05:30
Show Gist options
  • Save nrempel/92115befb48e37b02445b866a2bc414b to your computer and use it in GitHub Desktop.
Save nrempel/92115befb48e37b02445b866a2bc414b to your computer and use it in GitHub Desktop.
const SimpleCron = require('simple-cron');
const cron = new SimpleCron();
const amqp = require('amqplib/callback_api');
cron.schedule('* * * * *', () => {
amqp.connect(process.env.RABBIT_URL, (err, conn) => {
conn.createChannel((err, ch) => {
const q = 'clock';
ch.assertQueue(q, { durable: false });
ch.sendToQueue(q, Buffer.from('hi.'));
});
console.log('Queuing new job!');
});
});
cron.run();
const express = require('express');
const pg = require('pg');
const redis = require('redis');
const amqp = require('amqplib/callback_api');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!')
});
// Test Postgres connection
app.get('/postgres/:blurb', (req, res) => {
const ip = req.connection.remoteAddress;
const db = new pg.Pool({ connectionString: process.env.DATABASE_URL });
db.connect((err, client, done) => {
client.query('create table if not exists "blurbs" ("id" serial primary key, "text" varchar(255))', (err, result) => {
client.query('insert into "blurbs" ("text") values ($1)', [req.params.blurb], (err, result) => {
client.query('select * from "blurbs"', (err, result) => {
const blurbs = result.rows.map((o) => o.text);
res.send(`List of blurbs:\n${blurbs.join(' ')}`);
client.end();
done();
});
});
});
});
});
// Test Redis connection
app.get('/redis', (req, res) => {
const client = redis.createClient(process.env.REDIS_URL);
client.incr('count', (err, reply) => {
res.send(`Request count: ${reply}`);
});
});
// Test RabbitMQ connection
app.get('/rabbit/:msg', (req, res) => {
amqp.connect(process.env.RABBIT_URL, (err, conn) => {
conn.createChannel((err, ch) => {
const q = 'web';
ch.assertQueue(q, { durable: false });
ch.sendToQueue(q, Buffer.from(req.params.msg));
});
res.send('Message sent to worker process; check your terminal!');
});
});
app.listen(process.env.PORT, () => {
console.log(`Example app listening on port ${process.env.PORT}!`)
});
const amqp = require('amqplib/callback_api');
amqp.connect(process.env.RABBIT_URL, (err, conn) => {
conn.createChannel((err, ch) => {
// Consume messages from web queue
var q1 = 'web';
ch.assertQueue(q1, { durable: false });
ch.consume(q1, (msg) => {
console.info('Message received from web process:', msg.content.toString());
}, {noAck: true});
// Consume messages from clock queue
var q2 = 'clock';
ch.assertQueue(q2, { durable: false });
ch.consume(q2, (msg) => {
console.info('Message received from clock process:', msg.content.toString());
}, {noAck: true});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment