Skip to content
This repository has been archived by the owner on Jul 18, 2018. It is now read-only.

Socket.IO #2

Merged
merged 12 commits into from
Oct 31, 2013
Merged
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: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject
.idea

# supervisord
supervisord.conf
47 changes: 32 additions & 15 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

var mongoose = require('mongoose');
var db = require('./database');
var server = mongoose.model('server');
var model_server = mongoose.model('server');
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');

var io = require('socket.io');


var app = express();
Expand Down Expand Up @@ -42,12 +42,12 @@ app.post('/server', function(req, res){
rpc_pass: req.body.rpc_pass,
obs: req.body.obs
});
newServer.save();
newmodel_server.save();
res.send('Servidor adicionado com sucesso');
});

app.get('/server', function(req, res){
server.find({}, function(err, server){
model_server.find({}, function(err, server){
if(server){
res.send(server);
}else{
Expand All @@ -57,7 +57,7 @@ app.get('/server', function(req, res){
});

app.get('/server/:id', function(req, res){
server.findOne({_id:req.params.id}, function(err, server){
model_server.findOne({_id:req.params.id}, function(err, server){
if(server){
res.send(server);
}else{
Expand All @@ -67,14 +67,14 @@ app.get('/server/:id', function(req, res){
});

app.put('/server', function(req, res){
server.findOne({_id: req.body._id}, function(err, server){
model_server.findOne({_id: req.body._id}, function(err, server){
if (server){
server.name = req.body.name;
server.rpc_url = req.body.rpc_url;
server.rpc_user = req.body.rpc_user;
server.rpc_pass = req.body.rpc_pass;
server.obs = req.body.obs;
server.save();
model_server.name = req.body.name;
model_server.rpc_url = req.body.rpc_url;
model_server.rpc_user = req.body.rpc_user;
model_server.rpc_pass = req.body.rpc_pass;
model_server.obs = req.body.obs;
model_server.save();
res.send('Server editado com sucesso');
}else{
res.send('Servidor não existe');
Expand All @@ -83,16 +83,33 @@ app.put('/server', function(req, res){
});

app.delete('/server/:id', function(req, res){
server.findOne({_id: req.params.id}, function(err, server){
model_server.findOne({_id: req.params.id}, function(err, server){
if(server){
server.remove();
model_server.remove();
res.send('Servidor removido com sucesso')
}else{
res.send('Servidor não existe')
}
})
});

http.createServer(app).listen(app.get('port'), function(){
var server = http.createServer(app);
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});

var server_io = io.listen(server);
server_io.sockets.on('status', function (socket) {

});

app.post('/event', function(req, res){
console.log(req.body);
res.send("ACK");
server_io.sockets.emit("status_changed", {hostname: req.body.hostname,
process: req.body.processname,
from_state: req.body.from_state,
to_state: req.body.to_state
}
);
});
Empty file added notify_plugin/__init__.py
Empty file.
40 changes: 40 additions & 0 deletions notify_plugin/notify_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python
"""
Notify any PROCESS_STATE changes in realtime
to the dashboard server
"""
import os
import socket
import sys

import requests

from supervisor import childutils


def main():
if not "SUPERVISOR_ENABLED" in os.environ:
sys.stderr.write("notify_plugin must be run as a supervisor event listener\n")
sys.stderr.flush()
sys.exit(1)

if len(sys.argv) < 2:
sys.stderr.write("notify_plugin must receive the Dashboard URL")
sys.stderr.flush()
sys.exit(1)

dashboard_url = sys.argv[1]

while 1:
headers, payload = childutils.listener.wait(sys.stdin, sys.stdout)
pheaders, pdata = childutils.eventdata(payload+'\n')
pheaders["hostname"] = socket.gethostname() # Send hostname
pheaders["to_state"] = headers["eventname"].replace("PROCESS_STATE_", "") # Send current state
if not "notify_plugin" in pheaders["processname"]:
requests.post(dashboard_url, data=pheaders)

childutils.listener.ok(sys.stdout)


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions notify_plugin/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests==2.0.1
supervisor==3.0
45 changes: 45 additions & 0 deletions notify_plugin/supervisord.conf.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[supervisord]
logfile = /tmp/supervisord.log
logfile_maxbytes = 50MB
logfile_backups=10
loglevel = debug
pidfile = /tmp/supervisord.pid
nodaemon = false
minfds = 1024
minprocs = 200
umask = 022
user = YOUR_USER
identifier = supervisor
directory = /tmp
nocleanup = true
childlogdir = /tmp
strip_ansi = false
environment = KEY1="value1",KEY2="value2"

[program:memcached]
command = memcached
numprocs = 1
autostart = true
autorestart = true
stdout_logfile = NONE
stderr_logfile = NONE

[eventlistener:notifyme]
command =./notifyme.py
process_name = %(program_name)s_%(process_num)02d
numprocs = 1
events = PROCESS_STATE
buffer_size = 10
priority = -1
autostart = true
autorestart = unexpected
exitcodes = 0
user = YOUR_USER
redirect_stderr = false
stdout_logfile = /tmp/notifyme_stdout.log
stdout_logfile_maxbytes = 1MB
stdout_logfile_backups = 10
stderr_logfile = /tmp/notifyme_stderr.log
stderr_logfile_maxbytes = 1MB
stderr_logfile_backups = 10
environment=PYTHONUNBUFFERED="1" # PYTHONBUFFERED is necessary to un-buffer stdout/stderr
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"express": "3.2.4",
"jade": "*",
"mongoose": "*",
"xmlrpc": "*"
"xmlrpc": "*",
"socket.io": "*"
}
}