Thor is a toolkit for building powerful command-line interfaces.
It always been used for single command line. If I want to use it in a rails project, for example:
lib/tasks/my_cli.rb
require "thor"
class MyCLI < Thor
desc "hello NAME", "say hello to NAME"
def hello(name)
puts "Hello #{name}"
end
end
Where to put the MyCLI.start(ARGV)?
If I put it under that file(lib/tasks/my_cli.rb), when I run my rspec test, it will show me the command message:
Commands:
rspec help [COMMAND] # Describe available commands or one specific command
rspec hello NAME # say hello to NAME
I don't want to see it in my bundle exec rspec, so I moved the MyCLI.start(ARGV) to bin/rails. It looks well. But after I do this:
$ ./bin/rails s -b 0.0.0.0
$ [CTRL+C]
I saw this message:
=> Booting Thin
=> Rails 4.2.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on 0.0.0.0:3000, CTRL+C to stop
^CStopping ...
Exiting
Could not find command "_b".
What does it mean:
Could not find command "_b".
So, I don't know a best practice about how to use thor in a rails project.
You have to use method_option:
https://github.com/erikhuda/thor/wiki/Method-Options
And don't pass arguments like if it would be a normal method
require "thor"
class MyCLI < Thor
desc "hello NAME", "say hello to NAME"
method_option :name, aliases: '-b', type: :string, desc: 'It`s the named passed'
def hello
puts "Hello #{options[:name]}"
end
end
Then use it with thor command:
thor mycli:hello -b Robert
Related
I have an application that contains a bunch of tasks, and every 3 minutes I want to run a cron job that sends a mail for some test. I'm using the whenever gem but it doesn't seem to be running at all.Any ideas?
config/schedule.rb
every 3.minutes do
runner "MailerClass.some_method"
end
MailerClass.rb
def some_method
mail(:to => "some email", :cc => 'some email', :subject => "Regular Email by rake task #{Time.now.strftime("%H : %m")}", :from => "default_sender#udk.com") do |format|
end
end
What I have Tried after modifing config/schedule.rb are,
whenever --update-crontab --set environment=development
sudo service cron restart
When I run
crontab -l
this is the output
0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /bin/bash -l -c 'cd /home/my_app && script/rails runner -e development '\''MailerClass.some_method'\'' >> log/notification.log 2>&1'
I have checked the log. The problem is
script/rails: line 6: syntax error near unexpected token ('
script/rails: line 6:APP_PATH = File.expand_path('../../config/application', FILE)'
I am not getting this. I am new to rails. Thats why may be :(.
Any Solution!!!
It looks like you are forgetting to actually send the email
Instead of
MailerClass.some_method
try
MailerClass.some_method.deliver
Based on your updated info i am updaing my answer
Remove last single quote form end of parenthesis form this line APP_PATH = File.expand_path('../../config/application', FILE)'
I have an initializer in my rails app which I don't want to be started with rails console or rake task.
I put this code into my initializer:
puts "start"
if defined?(Rails::Console)
puts "console"
elsif File.basename($0) == "rake"
puts "rake"
end
puts "end"
When I run 'rails console' I get this:
$ rails console
start
end
Loading development environment (Rails 4.2.0.beta2)
[1] pry(main)>
But inside console:
[1] pry(main)> defined?(Rails::Console)
=> "constant"
However, it works for rake:
$ bundle exec rake routes
start
rake
end
Why it works inside console, but doesn't in initializer?
Or is there a better way to determine console/rake inside initializer?
Does anyone have experience/success using the whenever gem on aws opsworks? Is there a good recipe? Can I put that recipe on a separate layer and associate one instance with that additional layer? Or is there a better way to do it? Thanks!!!
EDIT:
We ended up doing it a bit differently...
Code:
Can’t really post the real code, but it’s like this:
in deploy/before_migrate.rb:
[:schedule].each do |config_name|
Chef::Log.info("Processing config for #{config_name}")
begin
template "#{release_path}/config/#{config_name}.rb" do |_config_file|
variables(
config_name => node[:deploy][:APP_NAME][:config_files][config_name]
)
local true
source "#{release_path}/config/#{config_name}.rb.erb"
end
rescue => e
Chef::Log.error e
raise "Error processing config for #{config_name}: #{e}"
end
end
in deploy/after_restart.rb:
execute 'start whenever' do
cwd release_path
user node[:deploy][:APP_NAME][:user] || 'deploy'
command 'bundle exec whenever -i APP_NAME'
end
in config/schedule.rb.erb:
<% schedule = #schedule || {} %>
set :job_template, "bash -l -c 'export PATH=/usr/local/bin:${PATH} && :job'"
job_type :runner, 'cd :path && bundle exec rails runner -e :environment ":task" :output'
job_type :five_runner, 'cd :path && timeout 300 bundle exec rails runner -e :environment ":task" :output'
set :output, 'log/five_minute_job.log'
every 5.minutes, at: <%= schedule[:five_minute_job_minute] || 0 %> do
five_runner 'Model.method'
end
We have a whenever cookbook in our repo we use that you would be more than welcome to use here: https://github.com/freerunningtech/frt-opsworks-cookbooks. I assume you're familiar with adding custom cookbooks to your opsworks stacks.
We generally run it on its own layer that also includes the rails cookbooks required for application deployment (while not the app server):
Configure: rails::configure
Deploy: deploy::rails whenever
Undeploy: deploy::rails-undeploy
However, we usually also deploy this instance as an application server, meaning we do end up serving requests from the box we're using for whenever as well.
There is one "gotcha", in that you must set your path in the env at the top of the schedule.rb like this:
env :PATH, ENV['PATH']
I am trying to run faye automatically using gem daemon_controller.
My Class
require "daemon_controller"
class FayeDaemon
def initialize
#controller = DaemonController.new(
:identifier => 'Faye server',
:start_command => "rackup faye.ru -s thin -E production",
:ping_command => [:tcp , 'localhost', 9292],
:log_file => 'log/faye.log',
:pid_file => 'tmp/pids/faye.pid',
:start_timeout => 5
)
end
def start
#controller.start
end
end
Function I use as before_filter in ApplicationController
def start_faye
fayes = FayeDaemon.new
fayes.start
end
as a result faye doesn't run with error
DaemonController::StartTimeout (Daemon 'Faye server' didn't daemonize in time.)
when fayes.start is called.
what i did wrong?
I highly recommend you to use foreman instead of deamon_controller, you can find good introduction here. Just install gem, create 'Procfile' in your rails root directory. and create two jobs, one for server and other for Faye, it could look like this:
web: bundle exec rails server webrick -b 127.0.0.1 -p 3000 -e development
faye: bundle exec rackup faye.ru -s thin -E production
and start foreman with
foreman start
On my development machine, I use port 10524. So I start my server this way :
rails s -p 10524
Is there a way to change the default port to 10524 so I wouldn't have to append the port each time I start the server?
First - do not edit anything in your gem path! It will influence all projects, and you will have a lot problems later...
In your project edit script/rails this way:
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
# THIS IS NEW:
require "rails/commands/server"
module Rails
class Server
def default_options
super.merge({
:Port => 10524,
:environment => (ENV['RAILS_ENV'] || "development").dup,
:daemonize => false,
:debugger => false,
:pid => File.expand_path("tmp/pids/server.pid"),
:config => File.expand_path("config.ru")
})
end
end
end
# END OF CHANGE
require 'rails/commands'
The principle is simple - you are monkey-patching the server runner - so it will influence just one project.
UPDATE: Yes, I know that the there is simpler solution with bash script containing:
#!/bin/bash
rails server -p 10524
but this solution has a serious drawback - it is boring as hell.
I like to append the following to config/boot.rb:
require 'rails/commands/server'
module Rails
class Server
alias :default_options_alias :default_options
def default_options
default_options_alias.merge!(:Port => 3333)
end
end
end
One more idea for you. Create a rake task that calls rails server with the -p.
task "start" => :environment do
system 'rails server -p 3001'
end
then call rake start instead of rails server
Combining two previous answers, for Rails 4.0.4 (and up, presumably), this suffices at the end of config/boot.rb:
require 'rails/commands/server'
module Rails
class Server
def default_options
super.merge({Port: 10524})
end
end
end
We're using Puma as a web server, and dotenv to set environment variables in development. This means I can set an environment variable for PORT, and reference it in the Puma config.
# .env
PORT=10524
# config/puma.rb
port ENV['PORT']
However, you'll have to start your app with foreman start instead of rails s, otherwise the puma config doesn't get read properly.
I like this approach because the configuration works the same way in development and production, you just change the value of the port if necessary.
Inspired by Radek and Spencer...
On Rails 4(.0.2 - Ruby 2.1.0 ), I was able to append this to config/boot.rb:
# config/boot.rb
# ...existing code
require 'rails/commands/server'
module Rails
# Override default development
# Server port
class Server
def default_options
super.merge(Port: 3100)
end
end
end
All other configuration in default_options are still set, and command-line switches still override defaults.
Solution for Rails 2.3 - script/server:
#!/usr/bin/env ruby
require 'rack/handler'
module Rack::Handler
class << WEBrick
alias_method :old_run, :run
end
class WEBrick
def self.run(app, options={})
options[:Port] = 3010 if options[:Port] == 3000
old_run(app, options)
end
end
end
require File.dirname(__FILE__) + '/../config/boot'
require 'commands/server'
If you're using puma (I'm using this on Rails 6+):
To change default port for all environments:
The "{3000}" part sets the default port if undefined in ENV.
~/config/puma.rb
change:
port ENV.fetch('PORT') { 3000 }
for:
port ENV.fetch('PORT') { 10524 }
To define it depending on the environment, using Figaro gem for credentials/environment variable:
~/application.yml
local_db_username: your_user_name
local_db_password: your_password
PORT: 10524
You can adapt this to you own environment variable manager.
You could install $ gem install foreman, and use foreman to start your server as defined in your Procfile like:
web: bundle exec rails -p 10524
You can check foreman gem docs here: https://github.com/ddollar/foreman for more info
The benefit of this approach is not only can you set/change the port in the config easily and that it doesn't require much code to be added but also you can add different steps in the Procfile that foreman will run for you so you don't have to go though them each time you want to start you application something like:
bundle: bundle install
web: bundle exec rails -p 10524
...
...
Cheers
For ruby > 3 and For rails > 7
in file app/config/puma.rb, update the port number.
port ENV.fetch("PORT") { 3200 }