# Manual Rake Tasks Question

**URL:** <https://www.ruby-forum.com/t/manual-rake-tasks-question/194810>\
**Category:** Rails\
**Created:** [September 11, 2010, 5:54am UTC](https://www.ruby-forum.com/t/manual-rake-tasks-question/194810 "2010-09-11T05:54:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 11, 2010, 5:54am UTC](https://www.ruby-forum.com/t/manual-rake-tasks-question/194810/1 "2010-09-11T05:54:26Z")

</div>

Adapted from Ryan B.:

application\_controller.rb

def call\_rake(task, os, options = {})  
options[:rails\_env] ||= Rails.env  
args = options.map { |n, v| “#{n.to\_s.upcase}=’#{v}’” }  
system “/usr/bin/rake #{task} #{args.join(’ ')} --trace 2\>&1 \>\>  
#{Rails.root}/log/rake.log &” if os == ‘linux’  
system “START rake.bat #{task} #{args.join(’ ')} --trace” if os ==  
‘windows’  
end

special\_raketasks\_controller.rb

def rake\_espn\_results  
Rails.env == ‘development’ ? (call\_rake :espn\_results, ‘windows’) :  
(call\_rake :espn\_results, ‘linux’)  
flash[:notice] = “ESPN Results/Schedules running.”  
redirect\_to raketasks\_url  
end

In my view I click the manual rake task and if the rails environment is  
in development it opens up a dos window (I develop on windows) and runs  
the task so I can view it. If it’s in production then I’m on linux and  
it runs the task in production.

What I’d like to do is change the output from posting to a log file and  
instead posting to a text field on the same index page.

Any thoughts on what I might need to do in order to accomplish this?

Thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 11, 2010, 3:04pm UTC](https://www.ruby-forum.com/t/manual-rake-tasks-question/194810/2 "2010-09-11T15:04:05Z")

</div>

I managed to do it by using f.puts to generate the log file and then  
read the file using a render in the view with link\_to\_remote.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 11, 2010, 4:52pm UTC](https://www.ruby-forum.com/t/manual-rake-tasks-question/194810/3 "2010-09-11T16:52:36Z")

</div>

Okay, I wrote out some basic code to look at:

> <https://gist.github.com/anonymous/575243>
>
> There are more than three files. show original

This is just a basic example and here are my current issues and  
concerns:

The view houses a link that I can click to run the example rake task.

If I’m in development mode it runs in windows without opening an  
additional window.

If I’m in production mode it runs in linux.

I can click on the Read Rake Results link and it auto populated the  
f.puts from the rake task into a textarea partial.

Issue/Concern #1: In Windows the link is clicked and the task runs in  
the background (no waiting or delay exists). This is what it should be  
doing. However, in linux, the page delays as it’s running the task. If  
the task is long it does not appear to run in the background so it  
eventually errors out, yet the task continues to run.

I would like to figure out why it’s doing this.

Issue/Concern #2: Locking down anyone from being able to view the page  
(I addressed this with my authorize method which only allows a super  
admin to view or run the tasks)

Issue/Concern #3: I would rather have the textarea partial update using  
some type of observer as the file is being written. I’m unsure of what  
the best way to accomplish this would be.

Please let me know your thoughts.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 12, 2010, 3:47pm UTC](https://www.ruby-forum.com/t/manual-rake-tasks-question/194810/4 "2010-09-12T15:47:42Z")

</div>

> Issue/Concern #1: In Windows the link is clicked and the task runs in  
> the background (no waiting or delay exists). This is what it should be  
> doing. However, in linux, the page delays as it’s running the task. If  
> the task is long it does not appear to run in the background so it  
> eventually errors out, yet the task continues to run.

I figured out why this was happening only on my production server and  
not on my development one. The issue was with Phusion Passenger not  
having the following set:

PassengerUseGlobalQueue on

Without the global queue on, it couldn’t process long running background  
tasks. Now that it’s set, issue/concern #1 is fixed. So, I’m now able  
to run all manual user run tasks fine from the view using the code I  
supplied above.

However, I’m still trying to figure out how to have the view become  
updated as the background process runs. I’m thinking of different ways  
to implement it but none seem to feel very rails like so I’m still  
interested in hearing possible solutions.

The current solution:

Click manual user run task from view.  
Rake task processes in the background and writes to a log file.  
Click link to read results.  
Results are read from the log file and shown in the view.

The envisioned solution:

Click manual user run task from view.  
Rake task processes in the background.  
Results are updated every 10 seconds in the view.

I hope that helps. My example code is located here:

> <https://gist.github.com/anonymous/575243>
>
> There are more than three files. show original

Thanks,

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [September 12, 2010, 8:27pm UTC](https://www.ruby-forum.com/t/manual-rake-tasks-question/194810/5 "2010-09-12T20:27:22Z")

</div>

On 12 Sep 2010, at 15:47, Alpha B. wrote:

> Rake task processes in the background.  
> Results are updated every 10 seconds in the view.

Pretty much the same thing, just replace your link with a  
periodicalExecuter and you’re basically done.

Best regards

Peter De Berdt
