Why can't i run rake -f? - ruby

I really have no idea what im doing. I'm trying to get rake to work so I can run rake workers:start but without having to have the working directory be the same as the folder of the rake file.
for example if i'm in my app directory, the above command works fine, but if i run rake -f ~/Code/my-app/Rakefile workers:start it says "Cannot load such file -- ./database
I'm using Sinatra (rack), and ultimately my goal is to try and install god so I can create a resque worker in production
require File.dirname(__FILE__) + "/main"
require 'resque/tasks'
namespace :workers do
desc "Launch single worker for processing jobs"
task :start do
ENV['QUEUE'] ||= '*'
puts "=== Launching single worker on '#{ENV['QUEUE']}' queue(s) with PID #{Process.pid}"
Rake::Task['resque:work'].invoke
end
end

rake -f ~/Code/my-app/Rakefile rake:workers:start
^ shouldn't need this one.
Also you might want to cd to appropriate dir before running rake.
cd ~/Code/my-app && rake workers:start

Related

Why isn't standard output displayed from a rake task called via backticks?

When a rake task is called via backpacks its output is suppressed:
task :two do
puts 'two'
end
task :one do
puts 'one'
`rake two`
end
-bash> bundle exec rake one
one
Whereas, the output is displayed when called via .invoke:
task :one do
puts 'one'
Rake::Task['two'].invoke
end
-bash> bundle exec rake one
one
two
Why is the output suppressed with backticks, and how can it be displayed?
The above is a contrived example, but ultimately I want to be able to run a local rake task that itself runs a remote rake task, on Heroku, and I want to see its output in real time (because the remote task is interactive, i.e. it asks questions via puts and waits for user/standard input via $stdin.gets.chomp):
task :one do
puts 'one'
`heroku run rake my_app:reset_user_passwords --app #{MY_APP_NAME}`
end
Backticks return the standard output of command so if you want to see the result, just call puts:
task :one do
puts 'one'
puts `heroku run rake my_app:reset_user_passwords --app #{MY_APP_NAME}`
end
If you want a interactive execution, you can try to use IO#expect

Custom Rake copy file to current directory in Mac

Is it possible to make a rake command that copies a file from a Mac and saves it into current directory?
I've tried using cp commands but it doesn't work.
This is what I've tried:
namespace :generate do
desc "Generate empty html5 index"
task :index do
#cp Dir['~/.rake/templates/index.html'], '.'
# cp "~/.rake/templates/index.html ."
end
end
I've just found the answer, I had to use sh command to execute shell commands in rake. Reference here
require 'fileutils'
namespace :generate do
desc "Generate empty html5 index"
task :index do
sh %{ cp ~/.rake/templates/index.html . }
end
end

Command line script that runs a method only when it is executed?

I am building a gem for command line use, and I am aware of the if __FILE__ == $0 method for determining whether the file being run is the current file (from Run code only if script called from the command line), however this doesn't work in my case. I have a module with an initialize function that I would like to run when the gem is called from the command line.
module MyApp
def self.initialize
# do command line stuff
end
def self.test
# run a rake test
end
end
MyApp::initialize
However, when running rake test it calls the initialize function which returns an error:
/Library/WebServer/Documents/myapp ❤ rake test
Options:
-v, --[no-]verbose Run verbosely
-h, --help Show this message
rake aborted!
Command failed with status (255): [ruby -I"lib" -I"/Users/bbriggs/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.1.0/lib" "/Users/bbriggs/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.1.0/lib/rake/rake_test_loader.rb" "test/test_myapp.rb" ]
/Users/bbriggs/.rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `eval'
/Users/bbriggs/.rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => test
(See full trace by running task with --trace)
I think this is because I am doing MyApp:initialize, because if I take that out of my code the rake test runs as expected, but the command line tool no longer works.
At the moment I am testing my app via bundle exec bin/myapp, and printing out the __FILE__ and $0 variables give me bin/myapp and path/to/lib/myapp.rb respectively, so I was wondering what the best way is to detect whether the module is being required or called directly. Am I even doing this right? I'm a bit of a Ruby newbie. :-)
Finally figured this out. Instead of running MyApp:initialize in lib/myapp.rb I put it in the bin/myapp file. This ensures that it is only run when the app is run from the command line and not when being tested via Rake or required by another script.

How would I modify this rake task to start a thin server with support for local debugging?

Question
I want to be able to debug my Sinatra website hosted by Thin on my local machine, and I want to be able to initiate this by using rake.
I'm not able to accept answers suggesting the use of different technologies (e.g. Windows, Rails, Java) or other servers (e.g. unicorn, passenger, puma); however, if what I'm asking for is impossible, then I will accept that answer.
What I've tried
My current Rakefile contains a task :start which starts the Thin server, but when it hits the breakpoint no output is displayed on my terminal. If I start Thin directly from the terminal, then I see the (rdb:1) prompt when it hits the breakpoint as expected. In either case, the Thin server is correctly running the site (confirmed by commenting out the breakpoint).
Gemfile
source :rubygems
gem 'sinatra'
gem 'thin'
gem 'debugger-pry'
Rakefile
task :start do
conf = File.expand_path('config.ru', File.dirname(__FILE__))
`thin -e development -R #{conf} --debug start`
end
config.ru
require File.expand_path('app', File.dirname(__FILE__))
run ModularExample::App.new
app.rb
require 'sinatra'
require 'debugger/pry'
module ModularExample
class App < Sinatra::Base
get '/' do
debugger
"Hello, world"
end
end
end
You're not outputting to STDOUT. The backticks execute the command and return the output as a string so you could do something like
puts `thin -e development -R #{conf} --debug start`
but you want to stream the processes output to STDOUT so you actually want to do:
task :start do
conf = File.expand_path('config.ru', File.dirname(__FILE__))
exec("thin -e development -R #{conf} --debug start")
end
Learn more about calling command line calls from this question.

Ruby daemon not working

I need to run a standalone ruby script as Unix (linux) daemon.
After running that daemon I need to run another Ruby method with it.
I installed the ruby-daemon gem on my machine by using the gem install daemon.
I did the test daemon program.
My test.rb file is :
module Test
def test_method
#s =" ITS WORKING !"
file=File.new("/home/username/test.txt", "w")
file.puts #s
file.close
end
end
My test_control.rb file is :
# this is myserver_control.rb
require 'rubygems' # if you use RubyGems
require 'daemons'
Daemons.run('test.rb')
After this I run the following command: ruby test_control.rb start
Now how can I check whether the daemon program has started properly?
How can I invoke a method with it?
Looks like the formatting on your post is way off, so hopefully someone can fix that, but I think the problem here is you're defining a module but not actually firing off the method you define.
The Daemons utility only executes the script provided. You should test that your "test.rb" file can be run on the command line directly before trying to diagnose what might be wrong with Daemons itself.
It may be as reworking test.rb:
module Test
def self.test_method
#s =" ITS WORKING !"
file = File.new("/home/username/test.txt", "w")
file.puts #s
file.close
end
end
Test.test_method
There are other ways to use Daemons where you pass it a module to run, but you're not using it that way.

Resources