11#!/usr/bin/env node
2+
3+ var redisURL = process . env . REDIS_URL ? { url : process . env . REDIS_URL } : { } ;
4+
5+
26var docopt = require ( 'docopt' ) . docopt ,
37 spawn = require ( 'child_process' ) . spawn ,
4- fs = require ( 'fs' ) ,
8+ fs = require ( 'fs' ) ,
59 pathtool = require ( 'path' ) ,
610 redis = require ( 'redis' ) ,
7- rcl = redis . createClient ( ) ,
11+ rcl = redis . createClient ( redisURL ) ,
812 wflib = require ( '../wflib' ) . init ( rcl ) ,
913 Engine = require ( '../engine2' ) ,
1014 async = require ( 'async' ) ,
11- dbId = 0 ;
15+ dbId = 0 ,
16+ plugins = [ ] ;
1217
1318var doc = "\
1419Usage:\n\
15- hflow run <workflow_dir_or_file> [-s]\n\
16- hflow send <wf_id> ( <signal_file> | -d <signal_data> )\n\
20+ hflow run <workflow_dir_or_file> [-s] [--with-server] [-p <plugin_module_name> ...]\n\
21+ hflow start-server [-p <plugin_module_name> ...]\n\
22+ hflow send <wf_id> ( <signal_file> | -d <signal_data> ) [-p <plugin_module_name> ...]\n\
1723 hflow -h | --help | --version" ;
1824
1925var opts = docopt ( doc ) ;
26+ var hfroot = pathtool . join ( require ( 'path' ) . dirname ( require . main . filename ) , ".." ) ;
2027
21- var hfroot = process . env . HFLOW_PATH ;
28+ if ( opts [ '-p' ] ) {
29+ opts [ '<plugin_module_name>' ] . forEach ( load_plugin ) ;
30+ }
2231
2332if ( opts . run ) {
2433 hflow_run ( ) ;
2534} else if ( opts . send ) {
2635 hflow_send ( ) ;
36+ } else if ( opts [ 'start-server' ] ) {
37+ hflow_start ( ) ;
38+ }
39+
40+ function load_plugin ( plugin_name ) {
41+ try {
42+ var Plugin = require ( plugin_name ) ;
43+ plugins . push ( new Plugin ( ) ) ;
44+ } catch ( err ) {
45+ console . log ( "Plugin module:" , plugin_name , "not found!" ) ;
46+ console . log ( err ) ;
47+ process . exit ( 1 ) ;
48+ }
49+ }
50+
51+ function hflow_start ( ) {
52+ var server = require ( '../server/hyperflow-server.js' ) ( rcl , wflib , plugins ) ;
53+ server . listen ( process . env . PORT , function ( ) { } ) ;
54+ console . log ( "HyperFlow server started, app factory URI: http://%s:%d/apps" , server . address ( ) . address , server . address ( ) . port ) ;
2755}
2856
2957function hflow_run ( ) {
3058 var wfpath = opts [ '<workflow_dir_or_file>' ] ,
3159 wfstats = fs . lstatSync ( wfpath ) ,
3260 wffile ;
33-
61+
62+ if ( opts [ '--with-server' ] ) {
63+ hflow_start ( ) ; // start the HTTP server
64+ }
65+
3466 if ( wfstats . isDirectory ( ) ) {
3567 wffile = pathtool . join ( wfpath , "workflow.json" ) ;
3668 } else if ( wfstats . isFile ( ) ) {
3769 wffile = wfpath ;
3870 wfpath = pathtool . dirname ( wfpath ) ;
3971 }
4072
41- var runWf = function ( wfId ) {
73+ var runWf = function ( wfId ) {
4274 var config = { "emulate" :"false" , "workdir" : pathtool . resolve ( wfpath ) } ;
4375 var engine = new Engine ( config , wflib , wfId , function ( err ) {
4476 // This represent custom plugin listening on event from available eventServer
4577 // engine.eventServer.on('trace.*', function(exec, args) {
4678 // console.log('Event captured: ' + exec + ' ' + args + ' job done');
4779 // });
80+ plugins . forEach ( function ( plugin ) {
81+ plugin . init ( rcl , wflib , engine ) ;
82+ } ) ;
4883 engine . runInstance ( function ( err ) {
4984 console . log ( "Wf id=" + wfId ) ;
5085 if ( opts [ '-s' ] ) {
5186 // Flag -s is present: send all input signals to the workflow -> start execution
5287 wflib . getWfIns ( wfId , false , function ( err , wfIns ) {
53- engine . wflib . getSignalInfo ( wfId , wfIns , function ( err , sigs ) {
88+ engine . wflib . getSignalInfo ( wfId , wfIns , function ( err , sigs ) {
5489 engine . emitSignals ( sigs ) ;
5590 } ) ;
5691 } ) ;
@@ -61,11 +96,11 @@ function hflow_run() {
6196
6297 var createWf = function ( cb ) {
6398 rcl . select ( dbId , function ( err , rep ) {
64- rcl . flushdb ( function ( err , rep ) {
99+ // rcl.flushdb(function(err, rep) { // flushing db here deletes the global 'hfid' entry (created earlier)
65100 wflib . createInstanceFromFile ( wffile , '' , function ( err , id ) {
66- cb ( err , id ) ;
101+ cb ( err , id ) ;
67102 } ) ;
68- } ) ;
103+ // });
69104 } ) ;
70105 }
71106
0 commit comments