Sails.js + PM2 - process.send is undefined - sails.js

I have a sails.js app. I am using pm2 in my production environment. According to this issue, I need to send a request from sails to pm2 indicating the app is online.
module.exports.bootstrap = function(cb) {
sails.on('lifted', function() {
process.send('ready') // process.send is undefined
});
};
How do I trigger the ready event here?

Please try:
var cp = require('child_process');
var p = cp.fork(__dirname + '/app');
sails.on('lifted', function() {
p.send('ready');
});
However, I use pm2 as well, and I just launch it like this for production without any code in the bootstrap.js file and keymetrics works just fine:
pm2 start app.js -x -- --prod
I hope this was helpful. Cheers

Related

Running gulp exec based on condition

I am working on ionic project integrated with gulp. How can run exec command based on condition?
I have tried following
var isIOSBuild = false;
if(args.iosBuild){
isIOSBuild = true;
console.log("Creating IOS Build...");
}
// Build application
gulp.task('ios_build', function (cb) {
gulp.src('', {read: false})
.pipe(gulpif(false,
exec(IOS_BUILD_COMMAND,
{
cwd : './',
maxBuffer: 1024 * 1024
},
function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
})
));
});
Input: gulp -r
I have put up condition for running exec command still the IOS_BUILD_COMMAND is running. Unable to understand why this is happening...
Alternatively, you can call the gulp task with or without arguments and run the task internally on the argument condition.
I use yargs to make my life easier.
var args = require('yargs').argv;
gulp.task('some_task', function(){
if(args.condition){
// run your task
}
})
Call gulp like so: gulp some_task --condition=true or gulp some_task --condition=false
That allows you to maintain the condition outside of gulp

Ionic serve: how to rerun gulp task

I'm kinda new to ionic and gulp.
I was able to configure the ionic.project file in order to run the gulp tasks when I first run ionic serve.
But now when I change files I want that the gulp task will run again.. But this doesn't happen.. Is there a way to do that?
This is my ionic.project file:
{
"name": "test",
"app_id": "",
"gulpStartupTasks": [
"default"
],
"watchPatterns": [
"src/**/*",
"src/*",
"www/**/*",
"!www/lib/**/*"
]
}
I expected that when some file changes that match the wtachPatterns
it will invoke the gulp watch task, but this doesnt happen (I see that ionic see that the file has changed but nothing happen.)
this the the gulp watch task:
gulp.task('watch', function() {
gulp.watch(paths.sass, ['sass']);
gulp.watch(paths.script, ['script']);
});
Basically the task is minifying all the JS files and all the sass/scss files
and the index.html is looking on the minified files. so if the gulp task isn't invoked there are no changes in the minified file and I need to run ionic serve all over again.. Is there a proper way to do that?
UPDATE:
This is the complete gulpfile
var gulp = require('gulp');
var gutil = require('gulp-util');
var bower = require('bower');
require('require-dir')('./gulp/tasks');
var paths = {
sass: ['./scss/**/*.scss'],
style: ['./src/**/*.scss'],
script: ['./src/app.js'],
html:['./src/*.html']
};
gulp.task('default', ['sass', 'script','watch', 'html', 'style']);
gulp.task('watch', function() {
gulp.watch(paths.sass, ['sass']);
gulp.watch(paths.script, ['script']);
});
gulp.task('install', ['git-check'], function() {
return bower.commands.install()
.on('log', function(data) {
gutil.log('bower', gutil.colors.cyan(data.id), data.message);
});
});
gulp.task('git-check', function(done) {
if (!sh.which('git')) {
console.log(
' ' + gutil.colors.red('Git is not installed.'),
'\n Git, the version control system, is required to download Ionic.',
'\n Download git here:', gutil.colors.cyan('http://git- scm.com/downloads') + '.',
'\n Once git is installed, run \'' + gutil.colors.cyan('gulp install') + '\' again.'
);
process.exit(1);
}
done();
});
And this is an example of one of the files who have the actual task:
var browserify = require('browserify');
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var stringify = require('stringify');
var paths = ['./src/app.js'];
gulp.task('script', function() {
return browserify(paths, {debug: true})
.transform(stringify(['.html']))
.bundle()
.pipe(source('app.js'))
.pipe(gulp.dest('./www/js'));
});
Well So the problem was with paths I watched.
I removed the ./ and now its working
First thing first, you misunderstand the watchPatterns is for livereload, which means, the web will refresh if there is any file changed on watch. It's definitely not having any relation to gulp.
Read more at: http://ionicframework.com/docs/cli/test.html
To watch for file changes with watch, update your watch task, which is
gulp.task('watch', function() {
gulp.watch(paths.sass, ['sass']); <-- any file in paths.sass changed will trigger `gulp sass`
gulp.watch(paths.script, ['script']); <-- any file in paths.script changed will trigger `gulp script`
});
So if you want to watch more files to be processed by Gulp, just add tasks and watch them in gulp watch.
Oh hey, you are watching only files in ./scss/**/*.scss and ./src/app.js. Add more if you wish.

MongoJS, Node, MongoLab - How to get the database online

I have created an hybrid application with Ionic, MongoJS, Angular JS (Mean Stack).
My application worked fine, locally. This means my mongod (Mongo Service) and my mongo ran locally on my pc. I also have a server.js (node) which is located locally.
Now I would like to use MongoLab (MongoDB as a Service) to change the location of my database from local to online.
I intented to change just the connection path, but for some reason I receive an undefined through my http get request.
My code:
server.js
var express = require('express');
var app = express();
var mongojs = require('mongojs');
//var db = mongojs('nzbaienfurtdb', ['nzbaienfurtdb']); // This is my old mongojs which ran locally and worked fine.
var databaseUrl = 'mongodb://dbuser:password#ds045604.mongolab.com:45604/nzbaienfurtdb';
var db = mongojs(databaseUrl, ['nzbaienfurtdb']); // database online with MongoLab
var bodyParser = require('body-parser');
app.use(express.static(__dirname + "/www"));
app.use(bodyParser.json());
app.get('/nzbaienfurtdb', function (req, res) {
console.log("I received a GET request")
db.nzbaienfurtdb.find(function (err, docs){
console.log(docs);
res.json(docs);
});
});
app.listen(3000);
console.log("server running on 3000");
This is a part of my get request out of a service:
service.js
return {
getUsers: function(){""
$http.get("/nzbaienfurtdb")
.success(function(data, status, headers, config){
headers("Cache-Control", "no-cache, no-store, must-revalidate");
headers("Pragma", "no-cache");
headers("Expires", 0);
users = angular.fromJson(data);
})
.error(function(data, status, headers, config){
console.log('Data could not be loaded, try again later');
})
return users;
}
MongoLab has been setup already.
My questions:
Why do I get an undefined for my http GET Request?
What happens with my server.js file when I want to deploy the Ionic App on for example an Android Phone? Is the server running on the device?
Since I have changed the var db variable i get also the following error message in my chrome console:
--------- ERROR CODE:
SyntaxError: Unexpected end of input
at Object.parse (native)
at Object.fromJson (http://localhost:3000/lib/ionic/js/ionic.bundle.js:8764:14)
at http://localhost:3000/js/userServices.js:23:27
at http://localhost:3000/lib/ionic/js/ionic.bundle.js:15737:11
at wrappedCallback (http://localhost:3000/lib/ionic/js/ionic.bundle.js:19197:81)
at wrappedCallback (http://localhost:3000/lib/ionic/js/ionic.bundle.js:19197:81)
at http://localhost:3000/lib/ionic/js/ionic.bundle.js:19283:26
at Scope.$eval (http://localhost:3000/lib/ionic/js/ionic.bundle.js:20326:28)
at Scope.$digest (http://localhost:3000/lib/ionic/js/ionic.bundle.js:20138:31)
at Scope.$apply (http://localhost:3000/lib/ionic/js/ionic.bundle.js:20430:24)
I hope somebody can help me out, I am fighting now for ages!
Thank you in advance, guys!
This issue has been resolved after ages!
I had to enable the API on the website of mongolab in my configuration.

Can I just run the filewatching portion of ember serve?

My Ember app is embedded and served out of another project. I don't need livereload or an HTTTP server, but I would like to have my files recompiled by ember-cli. How do I make that happen?
There's a --watch flag with the build command.
ember build --watch
You can use the sane npm package to watch your app directory and rebuild on changes:
var sane = require('sane'),
spawn = require('child_process').spawn;
var watcher = sane(process.cwd()+'/app', {glob: ['**/*.js']});
function rebuild(filepath) {
console.log(filepath+' changed, rebuilding');
spawn('ember', ['build'], {stdio: 'inherit'});
}
watcher.on('ready', function() {
console.log('watching '+process.cwd()+'/app');
});
watcher.on('change', function(filepath, root, stat) {
rebuild();
});
watcher.on('add', function(filepath, root, stat) {
rebuild();
});
watcher.on('delete', function(filepath, root, stat) {
rebuild();
});

parse.com or iron.io returns ssl error

I use iron.io to call the following parse.com function to get Facebook details of my user's friends.
var getDetailsForID = function (fbID) {
var thePromise = new Parse.Promise();
// TODO: maybe we can batch several users together into a single request................
console.log("Enter getDetailsForID");
FB.api('/v1.0', 'post', {
batch: [
{ method: 'get', name: 'basic', relative_url: fbID + '?fields=id,name,gender&include_headers=false', omit_response_on_success: false },
]
}, function(res) {
console.log("Enter callback in getDetailsForID");
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
console.log(" getDetailsForID res: " + res);
thePromise.resolve(res);
});
console.log("Exit getDetailsForID");
return thePromise;
}
In the iron.io log I see:
Enter callback in getDetailsForID
[Error: 139994800940864:error:0607907F:digital envelope routines:EVP_PKEY_get1_RSA:expecting an rsa key:../deps/openssl/openssl/crypto/evp/p_lib.c:288:
The following are not called:
console.log(" getDetailsForID res: " + res);
thePromise.resolve(res);
Any idea how to resolve this problem?
Since the answer to this question, IronWorker has released a Docker workflow. Feel free to use our official iron/node Docker Image. https://github.com/iron-io/dockerworker/tree/master/node
Ahh this is definitely not a problem with Iron.io but a problem with your post to the Facebook v1.0 API call.
+ '?fields=id,name,gender&include_headers=false', omit_response_on_success: false
do you really want to omit response on success? Which Facebook endpoint are you sending a Post to?
edit
IronWorker is currently set to 0.10.25 as of 07/22/2014, Use if your node version is < 0.10.25 you may receive this error.
fix: load your own version of node
in your .worker file add the following
deb "http://ppa.launchpad.net/chris-lea/node.js/ubuntu/pool/main/n/nodejs/nodejs_0.10.29-1chl1~trusty1_amd64.deb"
# OR you can download it from a local copy
deb "nodejs_0.10.29-1chl1~trusty1_amd64.deb"
You can install other missing or updated versions of binaries in a similar manner if there is a .deb for it.
Example in practice here on github
tldr.
use latest version of node, possibly openssl also.