NoMethodError: undefined method `within' for main:Object - ruby-on-rails

I'm trying to make Capistrano deploy script.
In my Capfile I make sure all rake tasks are included
# Load tasks
Dir.glob('config/capistrano_tasks/*.rake').each { |r| import r }
Next I have a 'migrations.rake' containing:
namespace :fileservice do
task :migrate do
within release_path do
info 'Doing migrations'
execute :php, fetch(:symfony_console_path), 'doctrine:migrations:migrate', '--no-interaction', fetch(:symfony_console_flags)
end
end
end
In my deploy.rb I call the task at the very end with:
after 'deploy:publishing', 'fileservice:migrate'
For some reason I keep getting an error saying:
NoMethodError: undefined method `within' for main:Object
I have no idea where to look or what might be wrong... When googling I get a lot of "NoMethodError" hits but none about the 'within' method and most are general Ruby errors.
Where should "within" be defined? I dat a ruby on rails thing? Or capistrano?
Hopefully someone knows where to start looking or which library / script to include!
UPDATE: I just discovered that none of the methods work. When removing lines I got the same error for "info" and "execute".... So I guess somewhere, something is missing.....

You need to tell Capistrano where (i.e. on what servers) to run your SSH commands. Do this using an on block, as follows:
namespace :fileservice do
task :migrate do
on roles(:db) do
within release_path do
info 'Doing migrations'
execute :php, fetch(:symfony_console_path), 'doctrine:migrations:migrate', '--no-interaction', fetch(:symfony_console_flags)
end
end
end
end
Replace roles(:db) as appropriate for your task, depending on where you want the commands to be run. The expression on roles(:all) do ... end, for example, will run the commands on all servers.
You can also follow the official documentation at http://capistranorb.com, or the Capistrano README, both of which show examples of the task/on/execute syntax.

As you already got the answer but Let me put the bulb from basic.
SSHKit was actually developed and released with Capistrano 3, and it’s basically a lower-level tool that provides methods for connecting and interacting with remote servers; it does all the heavy lifting for Capistrano, in other words.
There are four main methods you need to know about.
on(): specifies the server to run on
within(): specifies the directory path to run in
as(): specifies the user to run as
with(): specifies the environment variables to run with
Typically, you’ll start a task by using an on() method to specify the server on which you want your commands to run. Then you can use any combination of as(), within(), and with() methods, which are repeatable and stackable in any order.
Hope this help you

Related

Where do I enqueue jobs into ActiveJob in Rails 4.2?

I am a beginner when it comes to Rails. I am trying to follow this example:
http://ryanselk.com/2014/09/25/using-background-jobs-in-rails-42-with-active-job/
It says:
"Jobs can be added to the job queue from anywhere. We can add a job to the queue by: ResizeImage.perform_later 'http://example.com/ex.png' "
[UPDATE] Sorry, I am stupid. I came up with this task:
namespace :simple do
# call from command line:
# rake simple:resize_images
desc "Resize images"
task resize_images: :environment do
Dir.foreach('storage') do |next_image|
puts next_image
next if next_image == '.' or next_image == '..'
ResizeImage.perform_later next_image
end
end
end
but now I do:
rake simple:resize_images
and I get:
zacek2_phpP9JGif.jpg
rake aborted!
NameError: uninitialized constant ResizeImage
I've tried:
require ResizeImage
but that did not fix the problem.
I am afraid I don't understand how loading works in Rails. How do I load ResizeImage?
Do I set it up as a cron job?
No.
From the rails guides:
Active Job is a framework for declaring jobs and making them run on a variety of queueing backends.
Active Job is an interface to queueing backends such as sidekiq, delayed_job or resque. It's simply a way for you to write background jobs where you don't have to care about which of the queueing backends will be used.
How do I start ActiveJob?
So ActiveJob doesn't run background jobs on it's own. You're still missing one of the backends. Say you have decided to use delayed_job: Get it installed and then start it via:
script/delayed_job start
I don't understand where "anywhere" is.
That means anywhere in your code, you could write something like:
user.rb
def send_registration_email
UserRegistraionMailJob.perform_later self
end

How to catch errors and send them to Bugsnag when using `rails runner`?

We're running several cron tasks in our server, and we start them all using rails runner, like this:
rails runner 'MyTask.run'
where MyTask is a class in the project. The thing is, we use Bugsnag to handle errors in case anything fails. When we run a rake task, Bugsnag saves the errors and lists them in their website. But this does not happen when using rails runner. How can I configure rails to send errors to Bugsnag when this happens?
Rails runner is very difficult to configure or to customize. That is because all it really is, is a script with this main body:
if code_or_file.nil?
$stderr.puts "Run '#{$0} -h' for help."
exit 1
elsif File.exist?(code_or_file)
$0 = code_or_file
Kernel.load code_or_file
else
eval(code_or_file, binding, __FILE__, __LINE__)
end
As you can see, it just does an eval of the code you sent, so there's no wrapper, no class you can extend, and basically nothing you can configure. It is better to create a rake task to perform things the same way as runner, but this time in an environment that will be controlled by Rake, therefore allowing you to configure everything you need:
desc 'Wraps a runner command with rake'
task :runner, [:command] => :environment do |t, args|
eval(args[:command])
end
Then, you call it using
rake 'runner["MyTask.run"]'
This will run the tasks in a very similar way to using rails runner, but in the context of rake (which will include using Bugsnag).

Delayed_job and Prawn scripts

How do I run Prawn scripts with Delayed_job.
(Currently using Bj but not supported in Rails3)
This code does not work.
/lib/report_job.rb
class ReportJob < Struct.new(:prawn_script_name , :account_id )
def perform
bundle exec rails runner "#{Rails.root}/jobs/#{prawn_script_name}.rb #{#current_user.account_id} "
end
/reports_controller.rb
def generate_report(prawn_script_name)
Delayed::Job.enqueue(ReportJob.new("#{prawn_script_name}.rb","#{#current_user.account_id}"))
end
delayed_job table is populated as expected.
--- !ruby/struct:ReportJob
prawn_script_name: statements.rb
account_id: '18'
Error in last_error field.
{undefined method `runner' for ReportJob:0xc28f080
Any suggestions?
I think there are several misunderstandings here:
you meant to call runner from outside your app, e.g., in a shell script or command line. in other words, bundle exec rails runner are all commands and arguments of commands, not ruby methods or variables. runner is the first expression that is eval'd inside your perform method, hence your error.
rails runner just brings up your apps environment and evals the string or path argument given.
note account_id within the perform task, another mistake in your code I guess.
What you wanted to do could be a simple system call.
It seems your prawn script needs the environment, so simply calling
system "ruby #{Rails.root}/jobs/#{prawn_script_name}.rb #{account_id}"
won't work.
Now you could surely execute the script with runner from your project directory.
system "bundle exec rails runner #{Rails.root}/jobs/#{prawn_script_name}.rb #{account_id}"
but doing this via a system call within your environment is quite redundant. Delayed jobs already have access to your rails environment. so just simply load them.
class ReportJob < Struct.new(:prawn_script_name , :account_id )
def perform
load "#{Rails.root}/jobs/#{prawn_script_name}.rb"
end
end
hope this helps

Vlad the Deployer - Usage Patterns?

I'm starting to use vlad for new deployments and am wondering what's the best way to set it up so I can use the same tasks for my local development and remote production servers?
I thought about defining everything as remote tasks then having dev/prod methods which set the domain variable, then I can just call rake dev/prod vlad:do_something, but this just feels totally wrong.
Many of my tasks are useful to run on my local server and on my production server and I want to avoid repeating myself by having one 'task' for local and one 'remote_task' for remote. e.g.
def do_something
run "echo something"
end
task :do_something_dev
do_something
end
remote_task do_something_prod
do_something
end
Am I missing something or are these really the only options for using the same rake tasks on both the local and remote machine?
How about this:
[:development, :test, :production].each do |environment|
namespace environment do
task :do_something do
echo "do something on #{environment}"
end
end
end
This will give you:
rake vlad:development:do_something
rake vlad:test:do_something
rake vlad:production:do_something
With just one method it is probably less verbose to do it your way. But as soon as you have 2 or three methods, the overhead can be neglected.

Ruby on Rails: Error when running a rake task from initializer file

I have the file config/initializers/custom.rb
In the file, there is only one line:
`rake thinking_sphinx:start`
I thought this was supposed to just execute the line like when typing it from a command line. With this line of code, when I run "ruby script/server", the server freezes and outputs no error messages. Am I missing something?
Thanks!
Initializers load when your application loads. Rake tasks generally load your application. If you call a Rake task from an initializer, you're going to throw your app for a loop.
If you're worried about forgetting to start Sphinx in development, just give yourself a little warning:
# config/initializers/custom.rb
begin
ThinkingSphinx::Search.search "test" # test search
rescue ThinkingSphinx::ConnectionError
puts "** Oops! ThinkingSphinx is off! **"
end
I asked about how to get it to start up in dev mode via netbeans but the idea is the same. Bounty is still open.
My guess is that your not getting to the server starting because sphinx needs it's own process so it never returns to your custom initializer.

Resources