Jake task access controllers example

12 views
Skip to first unread message

jkinsman

unread,
Oct 15, 2015, 3:03:09 AM10/15/15
to GeddyJS
Trying to create a Jake task that I could run from cron but I would like to be able to access the controllers in my app from this task.

I have read the documentation on setting up a Jake task, but I am still a little fuzzy about how I would access my controllers from this task.

Could someone point me in the direction of an example of accessing controllers and models from a jake task?

Much appreciated!

Jumpei Ogawa

unread,
Oct 15, 2015, 3:33:17 AM10/15/15
to GeddyJS
Hi,

I believe it is not good design to call Jake task from conrtoller.
How about creating function in a js file under lib, and call it from controller and Jake file?

Here's example:

★app/controllers/yourcontroller.js

var yourFunc = require("lib/common").yourFunc;

exports.YourController = function() {
  this.create = function(req, resp, params) {
    yourFunc();
  };
};

★lib/common.js

exports.yourFunc = function() {
  // Your function here
};

★Jakefile.js

desc("Your Task");
task("yourtask", function (params) {
  var yourFunc = require("lib/common").yourFunc;
  yourFunc();
});

If this doesn't suit your requirement, let me know more details ;)
--

Jumpei Ogawa (小川 純平)
Software Engineer, GrowAsPeople <http://growaspeople.org>

jkinsman

unread,
Oct 15, 2015, 12:18:14 PM10/15/15
to GeddyJS
I am not trying to call a jake task from the controller. I would like to create a Jake task to be run from a Cron scheduler. The Cron would be run from the server. 

The Jake task itself is going to collect some data every hour, parse it, then insert it into the database. That is why I would like to have access to the controllers.

Perhaps a Jake task is not the right tool for this? If not, could you suggest something?

I could run the script to collect and parse the data separate from Geddy, and use HTTP calls to insert the data into Geddy, but it seems like there has to be a better way to do this. 

Thanks for the help!

Matthew Eernisse

unread,
Oct 15, 2015, 1:14:40 PM10/15/15
to ged...@googlegroups.com
It lets you run Jake tasks as if you're in the Geddy console.

--
The official community discussion group.
website: geddyjs.org, source: https://github.com/mde/geddy, group: https://groups.google.com/d/forum/geddyjs?hl=en
---
You received this message because you are subscribed to the Google Groups "GeddyJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geddyjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jesse Kinsman

unread,
Oct 15, 2015, 1:16:46 PM10/15/15
to ged...@googlegroups.com
Right. That is what I am looking for, but I am unsure of how to access the controllers in the Jake task?

jkinsman

unread,
Oct 15, 2015, 3:59:45 PM10/15/15
to GeddyJS
Apologies for this basic of a question. Through some fiddling I think I figured out how simple this is. Not sure if I am doing this correctly but it appears to work.

Actually accessing the model directly instead of the controller but I imagine you can access the controller in the same way.

Here is what I have come up with for anyone else that has this same question:

in Jakefile, I create the task, right after the TestTask function

task('testJake', function() {
  geddy.model.Test.all(function(err, data) {
    if (err) {
      console.error(err);
    }
    console.log(data);
  })
});

Then I can run my jake task like so from command line or cron, like this:
geddy jake testJake


Matthew Eernisse

unread,
Oct 19, 2015, 1:34:18 PM10/19/15
to GeddyJS
Yes, this is correct. In general you probably shouldn't be instantiating controllers to use in async (Jake) tasks -- you should have common business logic you import into your controllers or async tasks -- this also makes your code more testable.

jkinsman

unread,
Oct 19, 2015, 3:58:20 PM10/19/15
to GeddyJS
Thanks for responding. Yes as I started working on this I realized that accessing the controller was not the right way to go. 
Reply all
Reply to author
Forward
0 new messages