This app runs in the background and simply prints timestamp every minute. This app uses cron
and time
modules to schedule jobs and print timestamps. To run this on Cloud Foundry, you must use standalone
feature/framework (see down below).
//package.json
{
"name":"background",
"description":"A simple cron app showing Cloud Foundry's standalone or background apps feature",
"version":"0.0.1",
"private":true,
"scripts":{
"start":"node app.js"
},
"dependencies":{
"cron": "*",
"time": "*"
}
}
//app.js
var cronJob = require('cron').CronJob;
new cronJob('* * * * *', function(){
console.log('Time now: ' + new Date());
}, null, true, 'America/Los_Angeles');
##Running Standalone / background apps ## Running an app as a standalone / background app is very similar to running regular apps with the following exceptions.
-
The framework should be
standalone
. Sometimesvmc
auto-recognizes the app asnode
orruby
orjava
etc. In that case, selectother
option and then selectstandalone
option. -
You need to specify how to start the app (
Startup command> node app.js
) -
Select
none
toURL
question if your app doesn't run as a web-app like below.
1: background.cloudfoundry.com
2: none
URL> 2 <--- Choose 'none' as we don't have a web-UI
##Running it on Cloud Foundry ##
[~/success/git/background]
> vmc push background <--- Upload app w/ 'background'-app name
Instances> 4 <--- Push app to run on 4 instances
1: node
2: other
Framework> 2 <--- Choose 'other' framework
1: node
2: lift
3: standalone
4: rack
5: sinatra
6: spring
7: play
8: java_web
9: grails
10: rails3
Framework> 3 <---- Choose 'standalone' framework (for background processing)
Startup command> node app.js <--- Specify how to start the app
1: node
2: node06
3: node08
4: other
Runtime> 3 <--- Use Node.js 0.8v (current latest)
1: 64M
2: 128M
3: 256M
Memory Limit> 64M
Creating background... OK
1: background.cloudfoundry.com
2: none
URL> 2 <--- Choose 'none' as we don't have a web-UI
Create services for application?> n
Bind other services to application?> n
Save configuration?> n
Uploading background... OK
Starting background... OK
Checking background... OK
####Result:####
If you look into the logs vmc logs <appname> --all
, you'll see timestamp being printed every minute by workers running on each app instance.