Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** MySQL Table structure:
- --
- -- Table structure for table `actors`
- --
- CREATE TABLE IF NOT EXISTS `actors` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT 'test',
- `creation` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
- --
- -- Dumping data for table `actors`
- --
- INSERT INTO `actors` (`id`, `name`, `creation`) VALUES
- (1, 'test', '2012-01-11 04:27:10'),
- (2, 'test', '2012-01-11 04:27:20'),
- (3, 'test', '2012-01-11 04:27:20'),
- (4, 'test', '2012-01-11 04:27:20');
- npm install async mysql memcached colors trycatch
- */
- var http = require("http"),
- async = require("async"),
- fs = require("fs"),
- mysql = require("mysql"),
- memcached = require("memcached"),
- colors = require("colors"),
- trycatch = require("trycatch");
- // We put our server instances (local server and connections to remote ones) in here
- var server = {};
- // I want the query to fail one out of five times. see below
- var incr = 0;
- // Getting some data from memcached and mysql
- function getData(cb){
- console.log("getting mysql data".green);
- server.mysql.query("select * from actors", function(err, mySqlData){
- console.log("got mysql data".green);
- if(err)
- throw err;
- console.log("getting memcached data".green);
- server.memcached.get("test", function(err, memcachedData){
- console.log("Formatting data".green);
- // Increment the number of time this function has been called
- incr++;
- // Here we want the call to fail. We simulate that something
- // went wrong, and expect it to bubble back to the top
- if(incr % 5 == 0)
- throw new Error("Congrats, you are the 5th user!");
- if(err)
- throw err;
- var data = {
- mysql : mySqlData,
- memcached : memcachedData
- }
- console.log("returning data".green);
- cb(data);
- });
- });
- }
- // We setup membase connection, mysql connection, and a web server on
- // a local socket file (I was using nGinx as a fronted)
- async.waterfall([
- // Set MySQL
- function(cb){
- console.log("Connecting to MySQL...".grey);
- server.mysql = mysql.createClient({
- user: 'user',
- password: 'pass',
- });
- server.mysql.query("USE errors", function(err){
- if(err)
- return cb("Could not initialize MySQL Database: " + err)
- console.log("MySQL connected.".grey);
- return cb(null);
- });
- },
- // Set Memcached
- function(cb){
- console.log("Connecting to Membase...".grey);
- server.memcached = new memcached('localhost:11211');
- console.log("Membase connected.".grey);
- return cb(null);
- },
- // Set Web Server
- function(cb){
- console.log("Creating Web Server...".grey);
- server.http = http.createServer(function(req, res){
- console.log("Receiving call...".green);
- trycatch(function(){
- console.log("Calling getData...".green);
- // We add a timestamp to the response object
- res.timestamp = Date.now();
- console.log(res.timestamp);
- getData(function dataReturn(data){
- console.log("Sending data...".green);
- res.end(JSON.stringify(data));
- });
- },
- function(err, res){
- console.log("Sending error data...", arguments);
- // We print the timestamp; if you notice, this is the timestamp
- // of the first request, not the current.
- console.log(res.timestamp);
- res.writeHead(500);
- res.end("nok");
- console.error(err.stack);
- }, res);
- });
- server.http.listen("server.sock", function(err){
- if(err){
- console.error("Could not set unix socket: ".red.bold);
- return cb(err);
- }
- // Make the socket writable
- fs.chmodSync("server.sock", "0777");
- console.log("Web Server created succesfully.".grey);
- cb(null);
- });
- },
- ], function(err){
- if(err){
- console.error("Exiting?".red.bold, err);
- throw err;
- }
- else
- console.log("Setup complete".green);
- });
Add Comment
Please, Sign In to add comment