I'm using the whenever gem for Rails 5. I'm trying to troubleshoot an issue, and after my default schedule.rb file was created at config/schedule.rb, I added some logging directives
# Learn more: http://github.com/javan/whenever
set :environment, "development"
every 10.minutes do
rake "events:calc_index", :output => {:error => 'error.log', :standard => 'cron.log'}
end
I restarted my Rails server (not sure if that matters or not) but I don't see these log files created anywhere. I have verified my crontab was set up by running "crontab -e" and seeing the job
0,10,20,30,40,50 * * * * /bin/bash -l -c 'cd /Users/davea/Documents/workspace/newproj && RAILS_ENV=development bundle exec rake events:calc_index '
Where are the log files created?
In your schedule.rb file you can specify the redirection options for your commands at a global or command level by setting the 'output' variable. This example is global level:
# adds ">> /path/to/file.log 2>&1" to all commands
set :output, '/path/to/file.log'
and you should put this global level set :output above your job definition, otherwise it wouldn't work
Example:
# This works
set :output, {:error => '~/Desktop/z.error.log', :standard => '~/Desktop/z.standard.log'}
every 1.minute do
command "python ~/Desktop/whe/config/z.py"
end
Taken from: https://github.com/javan/whenever/wiki/Output-redirection-aka-logging-your-cron-jobs
According to whenever documentation
https://github.com/javan/whenever/wiki/Output-redirection-aka-logging-your-cron-jobs
you can define output like this with Rails :
set :output, "#{path}/log/cron.log"
So for your exemple you can do :
every 10.minutes do
rake "events:calc_index", output: {error: "#{path}/log/error.log", standard: "#{path}/log/cron.log"}
end
Related
So, I am trying to run the test but I am getting an error says.
Aruba::LaunchError:Command "seedly-calculator.rb" not found in PATH-variable
-seedly-calculator
-bin
-src
-seedly-calculator.rb
I have tried to change the path in rake file but it doesn't work.
My seedly-calculator.rb file is in the root directory.
require "rspec/core/rake_task"
namespace :spec do
desc "Run the functional suite against the CLI"
RSpec::Core::RakeTask.new(:functional, [] => [:set_path])
task :set_path do
project_bin_dir = File.join(File.dirname(File.expand_path(__FILE__)), '..', 'bin')
ENV['PATH'] = project_bin_dir + ':'+ ENV['PATH']
end
end
it shows error like:
Failure/Error: let(:command) { run "seedly-calculator.rb" }
Aruba::LaunchError:
Command "seedly-calculator.rb" not found in PATH-variable "/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/bin:/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/exe:/Users/bilaltariq/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/bin:/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/../bin:/Users/bilaltariq/.rbenv/versions/2.6.2/bin:/usr/local/Cellar/rbenv/1.1.1/libexec:/Users/bilaltariq/.rbenv/shims:/Users/bilaltariq/.asdf/shims:/Users/bilaltariq/.asdf/bin:/usr/local/bin:/Users/bilaltariq/.bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin".
I expect it to hit the file so i can write some test.
am i doing something wrong?
require 'spec_helper'
RSpec.describe 'Command Validation', type: :aruba do
let(:command) { run "seedly-calculator.rb" }
it "wrong/missing arguments" do
command.write("lookup\n")
stop_all_commands
expect(command.output).to end_with("Missing bank_name argument.\n")
end
end
seedly-calculator.rb:
#!/usr/bin/env ruby
# Complete bin/setup so that after it is
# run, ruby seedly-calculator.rb can be used to launch
# it.
# frozen_string_literal: true
require_relative './src/runner'
if !ARGV.length.zero?
input = ARGV
Runner.new.send('process_input', input)
else
puts "Arguments required!."
end
Update
To run a ruby script using run you need to make sure your ruby script is executable and contains a shebang so your system knows to run it with ruby. Here's example from this starter example
#!/usr/bin/env ruby
file = ARGV[0]
if file.nil? || file.empty?
abort "aruba-test-cli [file]: Filename is missing"
elsif !File.exist? file
abort "aruba-test-cli [file]: File does not exist"
end
puts File.read(file).chomp
So in your case you'll need to add this to the first line of your seedly-calculator.rb file
#!/usr/bin/env ruby
Then run this from command line to make it executable.
chmod +x #!/usr/bin/env ruby
I made a simple example forked off the one I reffed above. See this commit
Rspec convention is that it should match the same file structure of your project. It is not a good idea to set PATH manually.
Rake tasks are normally put in a tasks folder so you should have in project root a tasks folder
my_project/tasks/something.rake
Then you should have a spec folder that matches
my_project/spec/tasks/something_spec.rb
Then you should be able to get rid of task :set_path do end block and just run the spec without that.
You should also have a Gemfile to load your gems, run bundle install then invoke your test with
bundle exec rspec spec/tasks/sometask_spec.rb
I'm trying set the server options I.E. port, host, etc but I can't find anything on how to do this from within the config.ru file.
I've tried putting the config options into a hash and then doing:
configure { set :server, config[:server][:handler].to_sym }
Rack::Handler.default.run(App, config[:server])
Also tried:
Rack::Handler::pick(['puma']).run App, config[:server]
and even:
configure { set :server, config[:server].delete(:handler).to_sym }
so that the handler won't be in the server config hash and still...
no dice.
config hash is:
{
:handler => "puma",
:host => "127.0.0.1",
:port => 3000,
:threads => "0:16",
:verbose => true
}
But the hash config just gets ignored, I set the port to 3000 but the app loads with 8080 as default.
It also errors about there not being a run command present (well obviously, I'm not using it).
So a fix for that would also be a nice.
I'm sure there's a proper way to do this but why is it so hard to find it documented? I've done as many search terms into google as I can think of and yet nothing completely correct comes back.
It's not documented well because most people don't do what you're trying to do. :-) Folks typically store their Puma configuration in config/puma.rb or pass it on the command line e.g. in Procfile.
I'm going to out on a limb here and assume your App is a Sinatra app or something similar. The main issue with trying to set these options in a Sinatra configure {} block is that by the time rackup is running the class and executing these statements it's already too late to set things like the port and thread pool size. As far as the missing run method goes, I think you just want run App in config.ru. Not sure what you're going for there.
You can tell rackup to use Puma by adding this at the top of the file:
#\ -s Puma
If you want to set the port or any other rackup options, you can do it like so:
#\ -s Puma -p 3000
or, for Puma-specific options:
#\ -s Puma -p 3000 -O Threads=0:16 -O Verbose=true
This is (mostly) documented in Puma's README here, and here.
Another option is to skip rackup and config.ru entirely and just build everything into your Sinatra app:
require 'sinatra/base'
require 'puma'
class App < Sinatra::Application
configure do
set :server, :puma
set :port, 3000
set :server_settings, :Threads => '0:16', :Verbose => true
end
run! if $0 == app_file
end
Then you can just run your app like any normal Ruby script, e.g. ruby app.rb.
At the end of the day, I would strongly recommend you explore creating a Puma configuration file and using that instead. It's just easier, cleaner, and more understandable. If you need to pull in the Puma settings from the environment or from the result of another method or process, you can do that in there. Best of luck.
You can specify options on a line staring with #\ in config.ru (it must be the first such line). You specify them as if you were specifying command line options to rackup:
#\ -s puma -o 127.0.0.1 -p 3000 -O Threads=0:16 -O Verbose
# require everything and set up middleware etc.
run MyApp
The docs for this are hidden away on the wiki.
Check rackup -s puma -h for the options you can use. -O passes the option through to the server you are using (Puma seems to accept Threads and Verbose).
I'm having a bit of a challenge on a Chef recipe. I'm new to Chef, so please bear with me.
Step 1: My chef recipe installs Ruby Passenger, then compiles the Passenger nginx module along with Nginx.
# Install passenger and nginx module
bash "Install Passenger" do
code <<-EOF
source /usr/local/rvm/scripts/rvm
gem install passenger
EOF
user "root"
not_if { `gem list`.lines.grep(/^passenger \(.*\)/).count > 0 }
end
# Install passenger
# Note that we have to explicitly include the RVM script otherwise it won't setup the environment correctly
bash "Install passenger nginx module and nginx from source" do
code <<-EOF
source /usr/local/rvm/scripts/rvm
passenger-install-nginx-module --auto --prefix=/opt/nginx --auto-download
EOF
user "root"
not_if { File.directory? "/opt/nginx" }
end
Step 2: After that, I create the nginx config file using a template. This configuration requires the location of Passenger, which is dependent on step 1 completing.
template "/opt/nginx/conf/nginx.conf" do
source "nginx.conf.erb"
action :create
variables(
deploy_user: deploy_user,
passenger_root: `bash -c "source /usr/local/rvm/scripts/rvm; passenger-config --root"`.chomp,
passenger_ruby: `bash -c "source /usr/local/rvm/scripts/rvm; which ruby"`.chomp,
passenger: node[:passenger]
)
end
Problem: Chef appears to compile templates at th ebeginning of the run. So what ends up happening is that Step 2 is actually compiled before Step 1 is run. This means that the passenger_root variable is blank. It needs Step 1 to complete before being able to get the passenger_root, then run the template.
I tried wrapping the step 2 code in a ruby_block but that doesn't work: undefined methodtemplate' for Chef::Resource::RubyBlock`.
Not sure what to do here, or what is the best practice for Chef for something like this?
Thanks in advance,
Leonard
A cleaner and recommended way is to use Lazy Attribute Evaluation.
template "/opt/nginx/conf/nginx.conf" do
source "nginx.conf.erb"
action :create
variables lazy {
{
deploy_user: deploy_user,
passenger_root: `bash -c "source /usr/local/rvm/scripts/rvm; passenger-config --root"`.strip,
passenger_ruby: `bash -c "source /usr/local/rvm/scripts/rvm; which ruby"`.strip,
passenger: node[:passenger]
}
}
end
Also, I'd suggest using strip instead of chomp [thanks Draco].
As soon as you wrap your code in ruby_block you cannot use ordinary recipe resource declaration anymore. You have to write pure ruby code:
ruby_block "create /opt/nginx/conf/nginx.conf from template" do
block do
res = Chef::Resource::Template.new "/opt/nginx/conf/nginx.conf", run_context
res.source "nginx.conf.erb"
res.variables(
deploy_user: deploy_user,
passenger_root: `bash -c "source /usr/local/rvm/scripts/rvm; passenger-config --root"`.chomp,
passenger_ruby: `bash -c "source /usr/local/rvm/scripts/rvm; which ruby"`.chomp,
passenger: node[:passenger]
)
res.run_action :create
end
end
PS. And I guess you want to use strip instead of chomp to remove whitespace.
Yeah, Chef is a beast. I think part of the problem is there are a million ways to do the same things but there really is no documentation detailing the best way. What you probably want is to use Notifications, so that the block 1 runs first than notifies the block 2 to run. This means block 2 needs action :none so that it does not trigger until it gets notified.
I added the notify to your example in block 1 and added the action :none to block 2.
bash "Install Passenger" do
code <<-EOF
source /usr/local/rvm/scripts/rvm
gem install passenger
EOF
user "root"
not_if { `gem list`.lines.grep(/^passenger \(.*\)/).count > 0 }
notifies :run, 'bash[Install passenger nginx module and nginx from source]', :immediately
end
bash "Install passenger nginx module and nginx from source" do
code <<-EOF
source /usr/local/rvm/scripts/rvm
passenger-install-nginx-module --auto --prefix=/opt/nginx --auto-download
EOF
user "root"
action :none
end
Ruby noob - I need to have guard running in my rake tasks but I can't find a way to have it run in the background. It needs to run 2nd last, therefore having the guard > shell waiting for commands is preventing the final task from running, so calling sh bundle exec guard in the rake file is not an option. According to the documentation this should work:
##
desc "Watch files"
##
task :watcher do
Guard.setup
Guard::Dsl.evaluate_guardfile(:guardfile => 'Guardfile', :group => ['Frontend'])
Guard.guards('copy').run_all
end
#end watch files
https://github.com/guard/guard/wiki/Use-Guard-programmatically-cookbook
Here is my Guardfile, in full, (in same dir as Rakefile)
# Any files created or modified in the 'source' directory
# will be copied to the 'target' directory. Update the
# guard as appropriate for your needs.
guard :copy, :from => 'src', :to => 'dist',
:mkpath => true, :verbose => true
But rake watcher returns an error:
07:02:31 - INFO - Using Guardfile at Guardfile.
07:02:31 - ERROR - Guard::Copy - cannot copy, no valid :to directories
rake aborted!
uncaught throw :task_has_failed
I have tried different hacks, too many to mention here, but all have returned the above Guard::copy - cannot copy, no valid :to directories. The dist directory definitely exists. Also if I call guard from the shell, inside rake or on cmd line, then it runs perfect, but leaves me with the guard > shell. Think my issue maybe a syntax error in the rake file? any help appreciated ;)
Guard Copy does some initialization in the #start method, so you need to start the Guard before you can run it:
task :watcher do
Guard.setup
copy = Guard.guards('copy')
copy.start
copy.run_all
end
In addition there's no need to call Guard::Dsl.evaluate_guardfile anymore, that info on the wiki is outdated.
Edit 1: Keep watching
When you want to watch the dir, then you need to start Guard:
task :watcher do
Guard.start
copy = Guard.guards('copy')
copy.start
copy.run_all
end
Note: If you setup Guard and start it afterwards, then Guard fails with Hook with name 'load_guard_rc'
Edit 2: Really keep watching
Guard starts Listen in non blocking mode, so in order to make the call blocking, you need to wait for it:
task :watcher do
Guard.start
copy = Guard.guards('copy')
copy.start
copy.run_all
while ::Guard.running do
sleep 0.5
end
end
If you also want to disable interactions, you can pass the no_interactions option:
Guard.start({ no_interactions: true })
The API is absolutely not optimal and I'll improve it for Guard 2 when we remove Ruby 1.8.7 support and some deprecated stuff.
How do I set global configuration for RSpec in Ubuntu.
Specifically so, --color and --format specdoc stay turned on, across all my projects (ie every time I run rspec anywhere).
As you can see in the docs here, the intended use is creating ~/.rspec and in it putting your options, such as --color.
To quickly create an ~/.rspec file with the --color option, just run:
echo '--color' >> ~/.rspec
One can also use a spec_helper.rb file in all projects. The file should include the following:
RSpec.configure do |config|
# Use color in STDOUT
config.color = true
# Use color not only in STDOUT but also in pagers and files
config.tty = true
# Use the specified formatter
config.formatter = :documentation # :progress, :html,
# :json, CustomFormatterClass
end
Any example file must require the helper to be able to use that options.
In your spec_helper.rb file, include the following option:
RSpec.configure do |config|
config.color_enabled = true
end
You then must require in each *_spec.rb file that should use that option.
If you use rake to run rspec tests then you can edit spec/spec.opts
http://rspec.info/rails/runners.html
Or simply add alias spec=spec --color --format specdoc to your ~/.bashrc file like me.
One thing to be aware of is the impact of the different ways of running RSpec.
I was trying to turn on the option with the following code in spec/spec_helper.rb -
Rspec.configure do |config|
config.tty = $stdout.tty?
end
calling the 'rspec' binary directly - or as 'bundle exec rspec' and checking $stdout.tty? will return true.
invoking the 'rake spec' task - or as 'bundle exec rake spec' - Rake will invoke rspec in a separate process, and $stdout.tty? will return false.
In the end I used the ~/.rspec option, with just --tty as its contents. Works well for me and keeps our CI server output clean.