Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating custom CLI tools for a Meteor app

I'm working on a Meteor app (a port from a PHP project) and I need to be able to run commands on my app from the server for various operations like clearing caches, aggregating data, etc. These commands need to be run from shell scripts and crontab. I've seen other people ask this question and apparently there's no official way to do it yet.

I read a suggestion of using Meteor methods and just calling them from the client's JS console with a password. This doesn't solve my problem of running them from the CLI, but it did give me an idea:

Would it be possible to use a headless browser (like PhantomJS) to connect to my app and execute Meteor.call() to simulate a CLI tool with arguments passed to the method? If possible, does anyone know how I might accomplish this?

Thanks!

like image 495
Chris Brantley Avatar asked Jul 05 '13 19:07

Chris Brantley


1 Answers

EDIT: Updated to use Iron Router, the successor to Meteor Router.

There's no need for a headless browser or anything complicated. Use Meteorite to install Iron Router and define a server-side route:

Router.map(function () {
  this.route('clearCache', {
    where: 'server',
    action: function () {
      // Your cache-clearing code goes here.
    }
  });
});

Then have your cronjob trigger an HTTP GET request to that URI:

curl http://yoursite.com/clearCache

When the Meteor server receives the GET request, the router will execute your code.

For a little bit of security, add a check for a password:

Router.map(function () {
  this.route('clearCache', {
    path: '/clearCache/:password',
    where: 'server',
    action: function () {
      if (this.params.password == '2d1QZuK3R3a7fe46FX8huj517juvzciem73') {
        // Your cache-clearing code goes here.
      }
    }
  });
});

And have your cronjob add that password to the URI:

curl http://yoursite.com/clearCache/2d1QZuK3R3a7fe46FX8huj517juvzciem73

Original Post:

There's no need for a headless browser or anything complicated. Use Meteorite to install Meteor Router and define a server-side route:

Meteor.Router.add('/clearCache', function() {
  // Your cache-clearing code goes here.
});

Then have your cronjob trigger an HTTP GET request to that URI:

curl http://yoursite.com/clearCache

When the Meteor server receives the GET request, the router will execute your code.

For a little bit of security, add a check for a password:

Meteor.Router.add('/clearCache/:password', function(password) {
  if (password == '2d1QZuK3R3a7fe46FX8huj517juvzciem73') {
    // Your cache-clearing code goes here.
  }
});

And have your cronjob add that password to the URI:

curl http://yoursite.com/clearCache/2d1QZuK3R3a7fe46FX8huj517juvzciem73
like image 67
Geoffrey Booth Avatar answered Sep 29 '22 06:09

Geoffrey Booth