Permalink
Browse files

first commit

  • Loading branch information...
neyric committed Jul 19, 2012
1 parent 161bed5 commit 072f71ea55a1ffaf16e6653646f8dbede2ffaec7
View
@@ -11,5 +11,6 @@ pids
logs
results
+config.js
node_modules
npm-debug.log
View
@@ -1,4 +1,53 @@
-aws-swf-activities
-==================
+# AWS-SWF activities
-Collection of Activity workers for aws-swf
+This repository contains a collection of activities in Node.js for the AWS-SWF toolkit.
+
+````sh
+$ cd activities
+$ swf-activity
+````
+
+Most of the activities expect parameters to be passed in JSON format
+
+
+## How to create an activity
+
+
+### 1. Create the package.json
+
+ * name of the activity type
+ * main: worker module
+
++ add dependencies
+
+### 2. create the worker module
+
+````javascript
+exports.worker = function(task) {
+
+ var input_parameters = task.config.input; // or JSON.parse(task.config.input)
+
+ var result = f(input_parameters); // might be asynchronous
+
+ // Send the response to AWS SWF
+ task.respondCompleted(result, function(err) {
+ if(err) { console.error(err); return; }
+ console.log("echo: respondComplete");
+ });
+
+};
+````
+
+### 3. Config files (optional)
+
+Write config files into config.js.
+
+They should contains private stuff: credentials, passwords, oauth token, etc...
+
+## TODO
+
+ * System to build html documentation of the activities (to add in the gh-pages branch)
+
+ * script to npm install recursively in all folders
+
+
View
@@ -0,0 +1,12 @@
+
+exports.worker = function(task) {
+
+ var result = task.config.input; // yes, echo is pretty dumb....
+
+ task.respondCompleted(result, function(err) {
+ if(err) { console.error(err); return; }
+ console.log("echo: respondComplete");
+ });
+
+};
+
@@ -0,0 +1,2 @@
+{ "name" : "echo",
+ "main" : "./echo.js" }
@@ -0,0 +1,133 @@
+
+var http = require('http'),
+ https = require('https'),
+ querystring = require('querystring'),
+ URL = require('url'),
+ xml2js = require('xml2js-expat');
+
+
+exports.worker = function(task) {
+
+ var input = JSON.stringify(task.config.input)
+
+ var url = URL.parse(input["url"]);
+ var method = input.method || "GET";
+
+ var port = url.port;
+ if(!port) { port = 80; }
+
+ var encoding = input.encoding || "application/x-www-form-urlencoded";
+
+ var ssl = false;
+ if( input["url"].substr(0,5) == "https" ) {
+ ssl = true;
+ port = 443;
+ }
+
+ //console.log( url );
+
+ //var client = http.createClient(port, url.hostname, ssl);
+
+ var path = url.pathname || '/';
+
+ if(method == "GET" || method == "DELETE") {
+ path += '?'+querystring.stringify(input.data);
+ }
+
+ var headers = {
+ 'host': url.hostname,
+ 'accept': '*/*'
+ };
+
+ var body;
+ if(method == "POST") {
+ if(encoding == "application/x-www-form-urlencoded") {
+ body = querystring.stringify(input.data);
+ }
+ else if (encoding == "application/json") {
+ body = JSON.stringify(input.data);
+ }
+ headers["content-length"] = body.length;
+ headers["content-type"] = encoding;
+ }
+
+ var options = {
+ port: port,
+ host: url.hostname,
+ method: method,
+ path: path,
+ headers: headers
+ };
+
+ if(url.auth) {
+ options.auth = url.auth;
+ }
+
+ console.log(options);
+
+
+ var request = (ssl ? https : http).request(options);
+
+ if(method == "POST" || method == "PUT") {
+ request.write(body);
+ }
+
+ request.end();
+
+ request.on('response', function (response) {
+
+ /*console.log("Got response !");
+
+ console.log('STATUS: ' + response.statusCode);
+ console.log('HEADERS: ' + JSON.stringify(response.headers) );*/
+
+ response.setEncoding('utf8');
+ var complete = "";
+ response.on('data', function (chunk) {
+ complete += chunk;
+ });
+ response.on('end', function () {
+
+ r = complete;
+
+ // content-type:
+ var contentType = response.headers["content-type"].split(';')[0];
+
+ if(contentType) {
+
+ //console.log("Content-Type "+contentType);
+
+ if( contentType == "application/json" ||
+ contentType == "text/javascript" ||
+ contentType == "application/javascript") {
+ r = JSON.parse(complete);
+ }
+
+ if(contentType == "text/xml") {
+
+ var parser = new xml2js.Parser();
+ parser.addListener('end', function(result) {
+
+ task.respondCompleted({out: result }, function(err) { // TODO: wrong output
+ if(err) { console.error(err); return; }
+ console.log("echo: respondComplete");
+ });
+
+ });
+ parser.parseString(complete);
+ return;
+
+ }
+
+ }
+
+ task.respondCompleted({headers: response.headers, body: r}, function(err) {
+ if(err) { console.error(err); return; }
+ console.log("echo: respondComplete");
+ });
+
+ });
+
+ });
+
+};
@@ -0,0 +1,8 @@
+{
+ "name" : "http-request",
+ "main" : "./http-request.js",
+
+ "dependencies": {
+ "xml2js-expat": "0.2.2"
+ }
+}
@@ -0,0 +1,44 @@
+var nodemailer = require("nodemailer");
+
+var gmailAuth = require('./config');
+
+exports.worker = function(task) {
+
+ console.log("Task input : ", task.config.input);
+
+ var input = JSON.parse(task.config.input);
+
+ // create reusable transport method (opens pool of SMTP connections)
+ var smtpTransport = nodemailer.createTransport("SMTP",{
+ service: "Gmail",
+ auth: gmailAuth
+ });
+
+ // setup e-mail data with unicode symbols
+ var mailOptions = input;
+
+ // send mail with defined transport object
+ smtpTransport.sendMail(mailOptions, function(err, response){
+
+ if(err) {
+ task.respondFailed(err, "", function(err) {
+ if(err) { console.error(err); return; }
+ console.log("hello: respondFailed");
+ });
+ return;
+ }
+
+ console.log("email worker sent: " + response.message);
+
+ smtpTransport.close();
+ var result = JSON.stringify({message: response.message});
+
+ task.respondCompleted(result, function(err) {
+ if(err) { console.error(err); return; }
+ console.log("hello: respondComplete");
+ });
+
+ });
+
+};
+
@@ -0,0 +1,8 @@
+{
+ "name" : "nodemailer",
+ "main" : "./nodemailer.js",
+
+ "dependencies": {
+ "nodemailer": "0.3.20"
+ }
+}
@@ -0,0 +1,8 @@
+{
+ "name" : "salesforce-query",
+ "main" : "./salesforce-query.js",
+
+ "dependencies": {
+ "node-salesforce": "0.3.1"
+ }
+}
@@ -0,0 +1,20 @@
+var sf = require('node-salesforce');
+
+exports.worker = function(task, input, cb) {
+
+ var conn = new sf.Connection({
+ loginUrl: input.loginUrl || 'https://login.salesforce.com'
+ });
+
+ conn.login( input.username, input.password, function(err) {
+
+ if(err) { cb(err); return; }
+
+ console.log("SALESFORCE QUERY: Logged In !");
+
+ var records = [];
+ conn.query( input.query, cb);
+
+ });
+
+};
@@ -0,0 +1,7 @@
+{
+ "name" : "soap",
+ "main" : "./soap.js",
+ "dependencies": {
+ "soap": "0.2.4"
+ }
+}
View
@@ -0,0 +1,26 @@
+var soap = require('soap');
+
+
+exports.worker = function(task, input, cb) {
+
+ try {
+
+ soap.createClient( input.wsdlURL, function(err, client) {
+
+ if(err) { cb(err); return; }
+
+
+ try {
+ client[input.soapMethod](input.soapParams, cb);
+ }
+ catch(ex) {
+ cb(ex);
+ }
+ });
+
+ }
+ catch(ex) {
+ cb(ex);
+ }
+};
+
@@ -0,0 +1,8 @@
+{
+ "name" : "xmlrpc",
+ "main" : "./xmlrpc.js",
+
+ "dependencies": {
+ "xmlrpc": "1.0.1"
+ }
+}
@@ -0,0 +1,14 @@
+var xmlrpc = require('xmlrpc'),
+ URL = require('url');
+
+exports.worker = function(task, input, cb) {
+
+ var url = URL.parse(input.url);
+ var client = xmlrpc.createClient({ host: url.host, port: url.port || 80, path: url.path})
+ client.methodCall(input.xmlrpcMethod, input.xmlrpcParams, function(err, value) {
+ if(err) { cb(err); return; }
+ cb(null, {results: value});
+ });
+
+};
+

0 comments on commit 072f71e

Please sign in to comment.