Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuy Martinez committed May 10, 2017
0 parents commit 3fafc33
Show file tree
Hide file tree
Showing 20 changed files with 1,103 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Mixmax, inc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Bull UI

A monitoring dashboard for the Bull work queue.
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "bull-ui",
"private": "true",
"version": "0.0.0",
"description": "Dashboard for the Bull worker queue",
"main": "index.js",
"author": "Chuy Martinez <[email protected]>",
"license": "MIT",
"dependencies": {
"bull": "^3.0.0-alpha.4",
"express": "^4.15.2",
"express-hbs": "^1.0.4",
"hbs-utils": "^0.0.4",
"lodash": "^4.17.4",
"synchronize": "^2.0.0"
}
}
106 changes: 106 additions & 0 deletions public/dashboard.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Base structure
*/

/* Move down content because we have a fixed navbar that is 50px tall */
body {
padding-top: 50px;
}


/*
* Global add-ons
*/

.sub-header {
padding-bottom: 10px;
border-bottom: 1px solid #eee;
}

/*
* Top navigation
* Hide default border to remove 1px line.
*/
.navbar-fixed-top {
border: 0;
}

/*
* Sidebar
*/

/* Hide for mobile, show later */
.sidebar {
display: none;
}
@media (min-width: 768px) {
.sidebar {
position: fixed;
top: 51px;
bottom: 0;
left: 0;
z-index: 1000;
display: block;
padding: 20px;
overflow-x: hidden;
overflow-y: auto; /* Scrollable contents if viewport is shorter than content. */
background-color: #f5f5f5;
border-right: 1px solid #eee;
}
}

/* Sidebar navigation */
.nav-sidebar {
margin-right: -21px; /* 20px padding + 1px border */
margin-bottom: 20px;
margin-left: -20px;
}
.nav-sidebar > li > a {
padding-right: 20px;
padding-left: 20px;
}
.nav-sidebar > .active > a,
.nav-sidebar > .active > a:hover,
.nav-sidebar > .active > a:focus {
color: #fff;
background-color: #428bca;
}


/*
* Main content
*/

.main {
padding: 20px;
}
@media (min-width: 768px) {
.main {
padding-right: 40px;
padding-left: 40px;
}
}
.main .page-header {
margin-top: 0;
}


/*
* Placeholder dashboard ideas
*/

.placeholders {
margin-bottom: 30px;
text-align: center;
}
.placeholders h4 {
margin-bottom: 0;
}
.placeholder {
margin-bottom: 20px;
}
.placeholder img {
display: inline-block;
border-radius: 50%;
}

30 changes: 30 additions & 0 deletions src/server/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const express = require('express');
const hbs = require('express-hbs');
require('./views/helpers/handlebars')(hbs);
const hbsutils = require('hbs-utils')(hbs);
const path = require('path');

const routes = require('./views/routes');

const app = express();

app.set('views', `${__dirname}/views`);
app.set('view engine', 'hbs');

app.engine('hbs', hbs.express4({
defaultLayout: `${__dirname}/views/layout.hbs`
}));

app.use(express.static(path.join(__dirname, '/../../public')));

hbsutils.registerPartials(`${__dirname}/views'`, {
match: /(^|\/)_[^\/]+\.hbs$/
});

app.use('/', routes);

app.listen(4567, () => {
console.log('Bull UI is running on port 4567');
});

module.exports = app;
46 changes: 46 additions & 0 deletions src/server/bull/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const _ = require('lodash');
const Bull = require('bull');
const config = require('../config');

/**
* This function simply throws an exception when called, this is used to protect
* some Bull functions that should never be called in this module.
*/
function protectFunction() {
throw new Error('This function is protected !');
}

const protectedFunctions = ['process','start','add','setHandler','empty','run','clean','processJob','processJobs','startMoveUnlockedJobsToWait','moveUnlockedJobsToWait','updateDelayTimer','resume','pause'];

class Queues {
constructor() {
this._queues = new Map();
}

list() {
const names = _.map(config.queues, 'name');

return names;
}

get(queueName, done) {
const once = _.once(done);
if (this._queues.has(queueName)) {
once(null, this._queues.get(queueName));
return;
}

const { name, port, host, options } = _.find(config.queues, { name: queueName });
const bull = new Bull(name, {
redis: { port, host }
});

protectedFunctions.forEach(fn => bull[fn] = protectFunction);

this._queues.set(name, bull);

bull.isReady().then(() => once(null, bull));
}
}

module.exports = new Queues();
9 changes: 9 additions & 0 deletions src/server/config/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"queues": [
{
"name": "my_queue",
"port": 6381,
"host": "127.0.0.1"
}
]
}
5 changes: 5 additions & 0 deletions src/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"synchronize": "^2.0.0"
}
}
17 changes: 17 additions & 0 deletions src/server/views/dashboard/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const router = require('express').Router();

const queueList = require('./queueList');
const queueDetails = require('./queueDetails');
const queueJobsByState = require('./queueJobsByState');
/*
const jobDetails = require('./jobDetails');
*/

router.get('/', queueList);
router.get('/:queueName', queueDetails);
router.get('/:queueName/:state', queueJobsByState);
/*
router.get('/:queueName/:jobId', jobDetails);
*/

module.exports = router;
30 changes: 30 additions & 0 deletions src/server/views/dashboard/queueDetails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const sync = require('synchronize');
const Queues = require('../../bull');

function handler(req, res) {
const name = req.params.queueName;
const queue = sync.await(Queues.get(name, sync.defer()));

sync.parallel(() => {
queue.getActiveCount().asCallback(sync.defer());
queue.getFailedCount().asCallback(sync.defer());
queue.getDelayedCount().asCallback(sync.defer());
queue.getWaitingCount().asCallback(sync.defer());
queue.getPausedCount().asCallback(sync.defer());
});

const [ activeCount, failedCount, delayedCount, waitingCount, pausedCount ] = sync.await();

res.render('dashboard/templates/queueDetails.hbs', {
name,
groups: [
['active', activeCount],
['failed', failedCount],
['delayed', delayedCount],
['waiting', waitingCount],
['paused', pausedCount]
]
});
}

module.exports = handler;
20 changes: 20 additions & 0 deletions src/server/views/dashboard/queueJobsByState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const _ = require('lodash');
const sync = require('synchronize');
const Queues = require('../../bull');

function handler(req, res) {
const { queueName, state } = req.params;
const queue = sync.await(Queues.get(queueName, sync.defer()));

// Look at this unsafe metaprogramming, fix me some time please.
// Just pull the first 10 jobs, need to support pagination.
let jobs = sync.await(queue[`get${_.capitalize(state)}`](0, 9).asCallback(sync.defer()));

jobs = _.invokeMap(jobs, 'toJSON');

res.render('dashboard/templates/queueJobsByState.hbs', {
name: queueName, state, jobs
});
}

module.exports = handler;
9 changes: 9 additions & 0 deletions src/server/views/dashboard/queueList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Queues = require('../../bull');

function handler(req, res) {
const queues = Queues.list();

res.render('dashboard/templates/queueList.hbs', { queues });
}

module.exports = handler;
10 changes: 10 additions & 0 deletions src/server/views/dashboard/templates/queueDetails.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h2>Queue {{ name }}</h2>
<ul class="list-group">
{{#each groups }}
<li class="list-group-item">
<a href="{{ ../name }}/{{ this.[0] }}">
<span class="text-capitalize">{{ this.[0] }}</span>: {{ this.[1] }}
</a>
</li>
{{/each}}
</ul>
14 changes: 14 additions & 0 deletions src/server/views/dashboard/templates/queueJobsByState.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h2>Queue: {{ name }}</h2>
<h3>List of jobs in {{ state }} state</h3>
<ul class="list-group">
{{#each jobs}}
<li class="list-group-item">
<a role="button" data-toggle="collapse" href="#collapse{{this.id}}">
Job: {{ this.id }} {{ this.name }}
</a>
<div id="collapse{{this.id}}" class="collapse">
<pre>{{{ json this true }}}</pre>
</div>
</li>
{{/each}}
</ul>
7 changes: 7 additions & 0 deletions src/server/views/dashboard/templates/queueList.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ul class="list-group">
{{#each queues}}
<li class="list-group-item">
<a href="/dashboard/{{ this }}">{{ this }}</a>
</li>
{{/each}}
</ul>
17 changes: 17 additions & 0 deletions src/server/views/helpers/handlebars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const _ = require('lodash');

const helpers = {
json(obj, pretty = false) {
if (pretty) {
var d = JSON.stringify(obj, null, 2);
return d;
}
return JSON.stringify(obj);
}
};

module.exports = function registerHelpers(hbs) {
_.each(helpers,(fn, helper) => {
hbs.registerHelper(helper, fn);
});
};
Loading

0 comments on commit 3fafc33

Please sign in to comment.