Created
April 14, 2014 18:04
-
-
Save cadecairos/10670044 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. COPY TO WEBMAKER-EVENTS-SERVICE ROOT DIRECTORY | |
// 2. RUN `NPM INSTALL AWS-SDK` | |
// 3. RUN `SQS_QUEUE_REGION=<QUEUE_REGION> SQS_QUEUE_URL=<QUEUE_URL> node create_event_log.js | |
// 4. RUN `RM -R NODE_MODULES/AWS-SDK` | |
var loginConnString = process.env.WMLOGIN_CONNECTION_STRING + "/user/username/"; | |
var Habitat = require('habitat'); | |
Habitat.load(); | |
// Configuration | |
var env = new Habitat(); | |
var hyperquest = require("hyperquest"); | |
var Event = require("./models")(env.get("db")).event; | |
var AWS = require("aws-sdk"); | |
var async = require("async"); | |
var sqs = new AWS.SQS({ | |
region: process.env.SQS_QUEUE_REGION | |
}); | |
function hatchetDoppleganger(event_type, timestamp, data, cb) { | |
if (typeof data !== "object") { | |
return; | |
} | |
if (!process.env.SQS_QUEUE_URL) { | |
return; | |
} | |
var wrapper = { | |
app: "login", | |
event_type: event_type, | |
timestamp: timestamp, | |
data: data | |
}; | |
var body = JSON.stringify(wrapper); | |
sqs.sendMessage({ | |
MessageBody: body, | |
QueueUrl: process.env.SQS_QUEUE_URL | |
}, cb); | |
} | |
function getUserId(username, cb) { | |
var get = hyperquest({ | |
headers: { | |
"Content-Type": "application/json" | |
}, | |
uri: loginConnString + username | |
}); | |
get.on( "error", cb); | |
get.on( "response", function( resp ) { | |
if (resp.statusCode === 404) { | |
return cb(null, null); | |
} else if ( resp.statusCode !== 200 ) { | |
return cb(new Error("got a " + resp.statusCode)); | |
} | |
var bodyParts = []; | |
var bytes = 0; | |
resp.on( "data", function( data ) { | |
bodyParts.push( data ); | |
bytes += data.length; | |
}); | |
resp.on( "end", function() { | |
var body = Buffer.concat( bodyParts, bytes ).toString( "utf8" ); | |
var json; | |
try { | |
json = JSON.parse(body); | |
} catch ( e ) { | |
return cb(new Error("Error parsing login server response")); | |
} | |
cb( null, json.user.id ); | |
}); | |
}); | |
} | |
Event.findAll({ | |
where: { | |
createdAt: { | |
gte: "2014-01-01" | |
} | |
} | |
}).success(function(events){ | |
async.eachSeries( events, function(event, cb) { | |
if (!event.getDataValue("organizerId")) { | |
// skip event | |
console.log("Skipping Event# " + event.getDataValue("id") + " - no organizerId"); | |
return cb(); | |
} | |
getUserId( event.getDataValue("organizerId"), function(err, userId) { | |
if (err) { | |
return cb(err || "Missing userId for event#" + event.getDataValue("id")); | |
} | |
if ( !userId ) { | |
console.log("Skipping event# " + event.getDataValue("organizerId") + " - Account Removed"); | |
return cb(); | |
} | |
hatchetDoppleganger("create_event", (new Date(event.getDataValue("createdAt"))).toISOString(), { | |
eventId: event.getDataValue("id"), | |
userId: userId, | |
email: event.getDataValue("organizer"), | |
sendEventCreationEmails: false, | |
sendMofoStaffEmail: false | |
}, cb); | |
}); | |
}, function(err) { | |
if ( err ) { | |
console.log(err); | |
process.exit(1) | |
} | |
console.log( "Completed pushing " + events.length + " User Creation Events to " + process.env.SQS_QUEUE_URL ); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment