Maintaining JS Libs – Changing bower path

If you are web developer it becomes hard to manage all js libraries if you aren’t using any package manager.
Few folks created bower to make the job easier. (As far as functionality is concerned, it works same as npm, if you have used.)

Steps for installations are just for linux guys – windows people – DIY.

First you need to install npm sudo apt-get install npm
You can install it by sudo npm install -g bower

Once you have installed bower, You can install a web package, just by executing following command in the directory of project –

bower install jquery

You can even use your own git repository as source or any other url

bower install https://github.com/jquery/jquery.git#2.0.3

Above command will install jquery for your project.

Once you have installed it successfully things will be kept in
bower_components/jquery
you can refer it by
bower_components/jquery/dist/jquery.js. Simple. That’t it ?

No.

if you are like me – possessive about your directory structure – You want everything inside something like assets/lib/jquery/dist/jquery.js, or simply want it inside assets/lib directory instead of too many file/directory in root.

You can create a file in root of project .bowerrc, put following content inside the file
{
"directory" : "assets/lib"
}

Since filename begins with . it will be hidden in file browsers, clean.

Again – do you find these long assets path annoying ? – Grunt to rescue – check http://bower.io/docs/tools/#grunt

Optimal way of scheduling long job – nodejs/js

If you are using nodejs for some backend stuff to schedule some-work, probably you will hate scheduling because of two factors

  1. You can’t set long timeout
  2. Timers are removed when process restart

You would probably favor cronjobs for scheduling – or may be polling database for regular interval. Loading nodejs script every 5min is a costly job – developers understand, so you can’t follow php/short interval scheduling.

First problem can be solved by of setting a recursive call to a function, with time out. Many solutions on web.
But there lies a problem – You can’t cancel the timeout if you needed to after a recursive call. To solve the recursive call problem i wrote few lines of code.

Above code behaves same as setTimeout if you don’t need timer object updates, else you can register for that.

Second problem – Timers are removed when process are restarted. – That shouldn’t be hard – You can create a function to execute atomic functions,
Note atomic – atomic because if your setTimeout code will depend on variables state you will need to load those variables from database which will make job harder – better way is to schedule something like – send email to [email protected] instead of send message to currently loggedin users.

Solution of second problem really depends on your problem – but if you analyze your scheduled jobs closely you will find that they are really atomic, you made them non-atomic !.

if you really need that your jobs are never lost from memory better way is to run a stable nodeserver & use IPC. – but that would be practically hard to maintain.