scheduling rake tasks with Helicon Zoo?

Repository of web frameworks and applications for Microsoft IIS.
User avatar
Posts: 8
Joined: 29 Sep 2013, 22:46

scheduling rake tasks with Helicon Zoo?

29 Sep 2013, 22:54

first off I want think you for making Zoo as it is by far the easiest way to get Rails to run on Windows.

with the project I'm working, we are going to writing some rake tasks to do various things and need to schedule them to run at certain intervals. in the past i accomplish this using the task scheduler in windows, and calling ruby directly, however i'm wondering how i can do this with Helicon Zoo. am i able to invoke the ruby install from the command line that Helicon Zoo is using? I tried just going to the directory my application is in from the regular command line and it obviously doesn't know anything about ruby. I then tried from a ruby command line (i have 1.9.3p448 installed on my machine) and it complains that it doesn't know anything about the gems and tells me to run `bundle install`.

has anyone needed to schedule rake tasks from a Helicon Zoo project and if so, how did you accomplish it?

User avatar
Posts: 402
Joined: 06 Mar 2012, 11:59

Re: scheduling rake tasks with Helicon Zoo?

30 Sep 2013, 08:40

Hello!

Are you currently using Zoo Module version 3.x? If you are using previous version I suggest you to upgrade (simply install using web platform installer) because in new version there is web console and manager that you are supposed to use to run commands and IDEs. Please read following documentation chapter: http://www.helicontech.com/zoo/docs/console.htm and this article: http://www.helicontech.com/articles/ruby-on-rails-on-windows-in-production/ to learn more about the concept. There are instructions in article how to run 'rake' tasks and how to execute commands within application context.

As for scheduling tasks by a timer - this lays beyond Zoo applications itself because Zoo is executed inside IIS worker process model and there is no place for tasks there. IIS workers may start or die sometimes and IIS manages processes. You can use 'curl' tool with Windows scheduler to request some URLs inside your web site. Then you can program scripts under these URLs to do the tasks required - this will ensure your task is executed within your Ruby application context.

We are considering developing a separate scheduling application in future but still investigating user's needs and currently available solutions.

User avatar
Posts: 8
Joined: 29 Sep 2013, 22:46

Re: scheduling rake tasks with Helicon Zoo?

30 Sep 2013, 09:15

Thank you for the reply.

I was fearing this :P

Issuing a request from the url to run the rake task isn't exactly the greatest thing (makes me feel like I'm still using ColdFusion LOL) since I don't want someone just being able to `hit` the url and have the task run. Sure I can lock it down through code, but that doesn't seem like a great way to do it either.

The way I'm thinking of getting around it now, is to just have my local copy of ruby install and the gems and everything and then I will be able to run the rake tasks from my local installation outside of Helicon Zoo. Not the greatest solution, but it will allow me to do what I need.

User avatar
Posts: 402
Joined: 06 Mar 2012, 11:59

Re: scheduling rake tasks with Helicon Zoo?

01 Oct 2013, 09:54

Hello.

The solution with installing another version of gems globally is very bad because you will loose portability and also may face numerous incompatibility issues when different versions of gems will be installed into application and globally.

Running tasks with HTTP request isn't such a bad idea. To issue a request you can use curl tool: http://curl.haxx.se/ You will be able to log task response and other things that may be useful.
To run commands you can use code form our deploy script, something like this:

Code: Select all
def run( task, wrap = true )
  args = task.split
  cmd = args.shift

  ARGV.clear
  ARGV.unshift( *args )

  # We use 'load' because it doesn't spawn child ruby process,
  # which might be problematic in hosting environment.
  exe = File.join( RUBY_BIN, cmd )
  puts ">> #{exe} #{ARGV.inspect}"
  load( exe, wrap )
rescue Errno::EACCES => e
  puts 'Insufficient file system permissions.' +
    'Please make sure IIS application pool user has enough rights to access application files.' +
    "Usually Write permission isn't granted where it's needed. Exception: #{e}"
rescue Object => e
  puts e.to_s
  # Deploy tries to run all tasks. Some exceptions aren't relevant. See log if it matters.
end


and then call it:

Code: Select all
  run 'bundle install'
  run 'rake db:create --trace'
  run 'rake generate_session_store --trace'
  run 'rake db:migrate --trace'


To protect external requests you can test HTTP CLIENT_IP to be either '127.0.0.1' or '::1' in the code. It may seem hard but you'll need to write it only once.

Also you may want to read Zoo documentation regarding application deployment: http://www.helicontech.com/zoo/docs/deployment.htm Possibly you will need to actually initiate depoyment process instead of calling tasks from script because I'm not sure whether the tasks you run are safe to execute while other requests to the application are still running.

Surely we can do something in Zoo module to have some sort of timer and call some commands inside application context, but this would not be a perfect solution because there is no warranty for task execution as IIS may shut down waiter process for some reason. Having a dedicated scheduling service in Windows is much better way, and HTTP request technique I suggest would work just fine as a temporary solution.

User avatar
Posts: 8
Joined: 29 Sep 2013, 22:46

Re: scheduling rake tasks with Helicon Zoo?

01 Oct 2013, 23:38

thank you for the reply

i'm reading up on what exactly that script you wrote does to better understand everything. I'm thinking (and I could be wrong about this), that I could create a RACK app to take care of all preventing calls from outside ips and also run the various tasks.

my only question is, would I have access to the RAILS environment if I do that? I would like to be able to use ActiveRecord to perform the database interaction within these tasks.

am i even making sense at this point :P

User avatar
Posts: 402
Joined: 06 Mar 2012, 11:59

Re: scheduling rake tasks with Helicon Zoo?

02 Oct 2013, 08:14

Sorry, I'm not much of Ruby programmer and don't understand what you mean here. I suppose you already have Rails (Rack) application and you need to run some regular tasks inside this application's context?

If you are looking for a way to run tasks and commands manually, please use Zoo Manager or Web console to do this. You can start Zoo Manager using Windows Start -> Programs -> Helicon -> Zoo -> Zoo Manager.

Also there is no 'magic' in the words 'application context' - this is simply a set of environment variables that Zoo sets when calling Ruby engine, to tell Ruby where applications, working directory, gems, etc. are located. Many of these variables can be changed in web.config file, and you can view values of these variables using Zoo Manager and opening Ruby.Rack (or ruby.http) engine or your web application properties (some variables are for engines, some for applications).

User avatar
Posts: 8
Joined: 29 Sep 2013, 22:46

Re: scheduling rake tasks with Helicon Zoo?

03 Oct 2013, 00:13

@Yaroslav

i really appreciate all your help. I'm trying something out here and having some success.

What I did was use the clockowrk gem (http://rubygems.org/gems/clockwork) and i'm able to have some success with it. if I create the following app/clock.rb file:

Code: Select all
require 'clockwork'
require './config/boot'
require './config/environment'

include Clockwork

every(2.seconds, 'test.job') { Logger.debug('this is a test') }


I'm able to then open up the Web Console and execute: clockwork app/clock.rb and basically get an error every 2 seconds telling me and there is no `debug` method for Logger.class. Forget the error (i'm an idiot), the important thing is that my rails app still runs through iis while clockwork is outputting in the console every 2 second.... KEWL!

So i'm half way there! Now the only thing I need to figure out is how in the world I can run automatically. I don't want to have to open up the web console every time I startup the app and issue a `clockwork <script name>` command manually (obviously)

User avatar
Posts: 402
Joined: 06 Mar 2012, 11:59

Re: scheduling rake tasks with Helicon Zoo?

03 Oct 2013, 07:44

Hello!

The solution you are trying will not work again. Server side part of web console will be shut down after you close browser window with it therefore execution will stop.

But now we have an idea how to implement what you ask. We will add a feature into Helicon Zoo Manager where you will be able to export application environment settings and variables as a cmd file. You will then execute this cmd file as a first line of your Windows Scheduler job and all further commands will apply to your application.

Please give us couple of days to implement this feature and release new version of Zoo.

User avatar
Posts: 8
Joined: 29 Sep 2013, 22:46

Re: scheduling rake tasks with Helicon Zoo?

03 Oct 2013, 08:19

@Yaroslav

you guys are awesome!!!!! thank you so much!!!!

User avatar
Posts: 8
Joined: 29 Sep 2013, 22:46

Re: scheduling rake tasks with Helicon Zoo?

06 Oct 2013, 12:31

BTW, with what you guys are planning, would it be possible to use something like this in place of the web console itself?

Its a pain when you have to reload the rails app that you have to close the console, then recycle the app pool through the gui, then reopen the console. Having a standalone command prompt would be awesome

User avatar
Posts: 402
Joined: 06 Mar 2012, 11:59

Re: scheduling rake tasks with Helicon Zoo?

07 Oct 2013, 09:46

The new build is available for download now. Simply go to Web Platform Installer and install Helicon Zoo module again. After you install it launch Start -> Programs -> Helicon -> Zoo -> Helicon Zoo Manager, select your web site and click Tools -> Export Application environment. This will save environment as cmd file. Then either call this file before executing other commands for application or edit this file to include your commands directly and call it from Windows Scheduler.

As for web console alternative, I've already mentioned Start IDE button in Helicon Zoo Manager. Again - select your web site and click Start IDE. By default it starts command line interface, but you can configure it to start any IDE of your choice. It starts application in preconfigured environment and most current IDEs support this technique as well as standard command line. This means all commands called from this interface will apply to your application by default. No prior call to environment.cmd required.

User avatar
Posts: 8
Joined: 29 Sep 2013, 22:46

Re: scheduling rake tasks with Helicon Zoo?

07 Oct 2013, 10:12

this is awesome!!! can't thank you enough for everything that you've done. I will install the new version tonight and try it out.

User avatar
Posts: 8
Joined: 29 Sep 2013, 22:46

Re: scheduling rake tasks with Helicon Zoo?

07 Oct 2013, 22:32

now this is kewl!!!!

I just installed the latest version of the zoo module. Following your instructions, I exported the environment.cmd file and placed it in my rails project directory. I then wrote a tiny batch file called start.bat which opens a new persistent command prompt and calls the environment.cmd:

start.bat
=======
start cmd.exe /k environment.cmd

The '/k' switch keeps the new command prompt opened. I now have full access to my rails app environment! I can now run all my rails and rake commands from this prompt instead of using the web console.

This really is awesome. again, i can't thank you enough.

User avatar
Posts: 402
Joined: 06 Mar 2012, 11:59

Re: scheduling rake tasks with Helicon Zoo?

08 Oct 2013, 09:25

Glad you liked it.

Please note that you don't have to call environment.cmd to start command line. There is a button in Helicon Zoo Manager called Start IDE which by default starts command line in the application environment (same effect). Also I suggest you to set up it to launch some more attractive IDE that is also supports environment variables. My personal suggest would be Aptana: http://www.aptana.com/ To reassign new IDE just delete 'IDE' environment variable from your application in Zoo Manager.

Also I suggest you not to mix .bat and .cmd files in one script. They seem to be identical but in Windows use different interpreters, .cmd is more common though.

Return to Helicon Zoo

Who is online

Users browsing this forum: No registered users and 1 guest