-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jmspring/master
Update to latest
- Loading branch information
Showing
4 changed files
with
240 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
var azure = require('azure-storage'), | ||
uuid = require('node-uuid'), | ||
crypto = require('crypto'), | ||
async = require('async'); | ||
|
||
module.exports.azure_store = function(name, key) { | ||
var accountUrl = name + ".table.core.windows.net"; | ||
var accountName = name; | ||
var accountKey = key; | ||
|
||
function sha1_hash(val) { | ||
var shasum = crypto.createHash('sha1'); | ||
shasum.update(val); | ||
return shasum.digest('hex'); | ||
} | ||
|
||
function tablename(ehnamespace, ehname) { | ||
return "tbl" + sha1_hash(ehnamespace + ":" + ehname); | ||
} | ||
|
||
function partitionkey(consumergroup) { | ||
return "pk" + sha1_hash(consumergroup); | ||
} | ||
|
||
function rowkey(partition) { | ||
return "rk" + sha1_hash("partition:" + partition); | ||
} | ||
|
||
function retrieve_partition_state(tablesvc, ehnamespace, ehname, consumergroup, partitionid, callback) { | ||
tablesvc.retrieveEntity(tablename(ehnamespace, ehname), partitionkey(consumergroup), rowkey(partitionid), function(error, result, response) { | ||
callback(error, result, response); | ||
}); | ||
} | ||
|
||
var store = { | ||
store_eventhub_state: function(ehnamespace, ehname, consumergroup, partitioninfo, callback) { | ||
var tablesvc = azure.createTableService(accountName, accountKey); | ||
console.log("HERE 1"); | ||
if(tablesvc) { | ||
var batch = new azure.TableBatch(); | ||
var entityGen = azure.TableUtilities.entityGenerator; | ||
for(var i = 0; i < partitioninfo.length; i++) { | ||
var entity = { | ||
PartitionKey: entityGen.String(partitionkey(consumergroup)), | ||
RowKey: entityGen.String(rowkey(partitioninfo[i].id)), | ||
State: entityGen.String(JSON.stringify(partitioninfo[i].state)) | ||
}; | ||
batch.insertOrReplaceEntity(entity); | ||
} | ||
tablesvc.executeBatch(tablename(ehnamespace, ehname), batch, function(error, result, response) { | ||
callback(error, result, response); | ||
}); | ||
} else { | ||
// TODO -- error out | ||
} | ||
}, | ||
|
||
retrieve_eventhub_state: function(ehnamespace, ehname, consumergroup, partitioninfo, callback) { | ||
var tablesvc = azure.createTableService(accountName, accountKey); | ||
if(tablesvc) { | ||
var asyncTasks = []; | ||
var partitionResults = []; | ||
partitioninfo.forEach(function(partition) { | ||
var partitionid = partition.id; | ||
asyncTasks.push(function(cb) { | ||
retrieve_partition_state(tablesvc, ehnamespace, ehname, consumergroup, partitionid, function(error, result, response) { | ||
var partitionState = { id: partitionid }; | ||
if(!error) { | ||
partitionState["state"] = JSON.parse(result.State["_"]); | ||
} else { | ||
if(error.statusCode && error.statusCode == 404) { | ||
partitionState["state"] = {}; | ||
} else { | ||
partitionState["error"] = error; | ||
} | ||
} | ||
cb(null, partitionState); | ||
}); | ||
}); | ||
}); | ||
async.parallel(asyncTasks, function(err, results) { | ||
callback(results); | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
return store; | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env node | ||
var eventhub = require("..").eventhub; | ||
var optimist = require('optimist') | ||
.options('e', { alias : 'eventhub' }) | ||
.options('n', { alias : 'namespace' }) | ||
.options('a', { alias : 'accessuser' }) | ||
.options('p', { alias : 'accesspass' }) | ||
.options('g', { alias : 'consumergroup', default : '$default' }) | ||
.options('s', { alias : 'storagetable', default : null }) | ||
.options('k', { alias : 'storagekey', default : null }) | ||
.demand(['e', 'n', 'a', 'p']) | ||
.usage("$0 -e eventhub -n eventhub_namespace -a eventhub_username -p eventhub_password [ -g consumergroup ] [ -s storagetable -k storagekey ]") | ||
; | ||
|
||
var ehnamespace = optimist.argv.namespace; | ||
var ehname = optimist.argv.eventhub; | ||
var access_user = optimist.argv.accessuser; | ||
var access_pass = optimist.argv.accesspass; | ||
var consumer_group = optimist.argv.consumergroup; | ||
|
||
var eh = eventhub.create_instance(ehnamespace, ehname, access_user, access_pass); | ||
|
||
var ehp = eh.event_processor_instance(consumer_group, function(message, sub) { | ||
console.log("subscription -- " + sub); | ||
if(message.body) { | ||
console.log(message.body); | ||
} | ||
if(message.properties) { | ||
console.log(message.properties); | ||
} | ||
if(message.annotations) { | ||
console.log(message.annotations); | ||
} | ||
}); | ||
|
||
// indicate which partitions were subscribed to | ||
ehp.subscribe_handler(function(url) { | ||
console.log("subscribed to -- " + url); | ||
}); | ||
|
||
// if specified, add a storage account | ||
if(optimist.argv.storagetable && optimist.argv.storagekey) { | ||
ehp.add_store(optimist.argv.storagetable, optimist.argv.storagekey); | ||
} | ||
|
||
ehp.process(); | ||
|
||
// show state of event processor every 30 seconds | ||
setInterval(function() { | ||
var state = ehp.state(); | ||
console.log(JSON.stringify(state, null, 2)); | ||
}, 30000); |