Use whenever with sinatra - ruby

I'm trying to get whenever to work with sinatra. When I run the whenever command, I get the generated cron tab. But the problem is, that in my sinatra app, I don't have a script/runner file, which is present in Rails.
How do I get this runner, or is there a whenever command to generate one?
thx!

You can use a rake task in place of script/runner. The Whenever gem supports defining the job via a rake task (and more in fact)
Sample:
# config/schedule.rb
every 3.hours do
rake "destroy_all"
end
and in your Rakefile: (for lack of good examples)
task :destroy_all do
puts "Do not do this"
# sh "rm -rf ."
end

Related

resque rake task giving error

I have been using resque for background processing, No my problem with code is :
- when I start rake task as "rake resque:work QUEUE=''" as per ryan bates episode no. 271. in remote server the code inside worker class for file maipulation works properly without any filepath issues and I/O errors.
- when i start rake task as "rake resque:work QUEUE='' BACKGROUND=yes" now, the code inside worker class gives "failed:Errno::EIO: Input/output error # io_write - >" error.
Now my question is I want to start the resque queue above rake command only one time and why second point giving error is this issue with filepaths if so then why it runs smoothly as mention in point first.
You can use god to manage your background process. Or nohup can be your solution too as below:
$ nohup bundle exec rake resque:work QUEUE=queue_name PIDFILE=tmp/pids/resque_worker_QUEUE.pid & >> log/resque_worker_QUEUE.log 2>&1
and even this command worked for me:
PIDFILE=./resque.pid BACKGROUND=yes QUEUE="*" rake resque:work >> worker1.log &
Hope that will help you too.

Rails.vim - How to test all features and/or specs?

I am using rails.vim plugin which is pretty awesome. However, I fail to see how could I test all the specs in one command. Right now I need to open a particular spec and do :Rake and that just tests the current opened spec. However, how could I test all the specs? Which command?
Thanks
Have you tried pairing a rake task with a ViM leader mapping?
In your Rakefile, you could set up something like this:
desc 'Continuous integration task'
task :ci do
['rspec',
'cucumber -f progress',
'rake konacha:run'].each do |cmd|
system("bundle exec #{cmd}")
raise "#{cmd} failed!" unless $?.exitstatus == 0
end
end
Then you can setup a leader command in ViM to execute your ci rake task:
nnoremap <leader>T :w\|:!bundle exec rake ci<CR>
Then when you execute <leader> T in normal mode, ViM will shell out and run bundle exec rake ci.
I use tmux, so I prefer the following leader mapping which runs the rake task in a bottom pane:
nnoremap <leader>T :w\|:silent !tmux send-keys -t bottom C-u 'bundle exec rake ci' C-m <CR>\|:redraw!<CR>

Make a rake task fail if system call return error

I have a Rakefile that I use to automate some tasks in my project.
Inside some tasks, I call system, but, even if the process return an error,
task continues without any issue.
How can I avoid that? I want to make rake exit when some subprocess return an error.
Thanks in advance
You can evaluate the return value of system
system('inexistent command') or exit!(1)
puts "This line is not reached"
sh is the Rake way to call a command. It will fail with a neat message. Compared with system, sh prints out the command as well.

How can I create a daemon with Thor (ruby)?

I would like to use the popular Thor gem to create a daemonized task. My Thor class looks like this:
require 'rubygems'
require 'daemons'
require 'thor'
class CLI < Thor
desc "start", "Startup the App"
method_option :daemonize, :aliases => "-d", :default => false, :type => :boolean, :banner => "Run as daemon"
def start
run_app(options[:daemonize])
end
desc "stop", "Stop the daemon"
def stop
stop_app
end
no_tasks {
def run_app(run_as_daemon)
# Run the application code
Daemons.daemonize if run_as_daemon
# loop until stopped or interrupted
# ...
end
def stop_app
#stop the app
end
}
end
So here I've setup a basic thor class with two tasks, start and stop. I'm also, currently using the Daemons gem, but that isn't required. The part that I'm struggling with is that when this app runs as "run_thor_app.rb start" everything runs just fine. Obviously the stop task isn't needed in this instance. But when I run "run_thor_app.rb start -d" the app runs until Daemons.daemonize runs and then it quits. Checking the running processes shows that nothing is running in the background.
Even if something were running, I wouldn't know how to approach the stop task. For example, how do you detect that the app is running as a daemon and stop it. I've looked at Daemons::Monitor, but the documentation isn't clear on how that works and when I tried it, it didn't work.
It seems to me that this would be a good use case for something that is built into Thor, but searching through the code on github hasn't revealed anything to me. Maybe I just missed it somewhere. In any case, I think it would be good to document a best practice or a pattern for handling daemons with Thor for others to reference.
The way you usually manage daemon processes is by having them write their PID in a file. This makes it possible for another process to discover the daemon's PID, and kill it (or send some other signal).
Your code should work. I tried a bare bones script that used the deamons gem, and it took me a few tries until I found the deamonized process. I figured it would get the same name as the parent process, or something similar, but instead it's name was "self". Remember that the daemonized process will no longer write to STDOUT.
Anyway, try this:
# set up everything
# then daemonize
Daemons.daemonize
# and write a pid file
File.open('/tmp/mydaemon.pid', 'w') { |f| f.puts(Process.pid) }
loop do
# do something
# this loop is important, if the script ends the daemon dies
end
and check the /tmp/mydaemon.pid file for the PID. Then run ps ax | grep x where x is the PID. Run cat /tmp/mydaemon.pid | xargs kill` to kill the daemon.
I think the daemons' gem has some helpers for managing PidFiles, check out PidFile in http://rubydoc.info/gems/daemons/1.1.0/frames

How to ensure a rake task only running a process at a time

I use crontab to invoke rake task at some time for example: every 3 hour
I want to ensure that when crontab ready to execute the rake task
it can check the rake task is running. if it is so don't execute.
how to do this. thanks.
I'll leave this here because I think it's useful:
task :my_task do
pid_file = '/tmp/my_task.pid'
raise 'pid file exists!' if File.exists? pid_file
File.open(pid_file, 'w'){|f| f.puts Process.pid}
begin
# execute code here
ensure
File.delete pid_file
end
end
You could use a lock file for this. When the task runs, try to grab the lock and run the rake task if you get the lock. If you don't get the lock, then don't run rake; you might want to log an error or warning somewhere too or you can end up with your rake task not doing anything for weeks or months before you know about it. When rake exits, unlock the lock file.
Something like RAA might help but I haven't used it so maybe not.
You could also use a PID file. You'd have a file somewhere that holds the rake processes process ID. Before starting rake, you read the PID from that file and see if the process is running; if it isn't then start up rake and write its PID to the PID file. When rake exists, delete the PID file. You'd want to combine this with locking on the PID file if you want to be really strict but this depends on your particular situation.
All you need is a gem named pidfile.
Add this to your Gemfile:
gem 'pidfile', '>= 0.3.0'
And the task could be:
desc "my task"
task :my_task do |t|
PidFile.new(piddir: "/var/lock", pidfile: "#{t.name}.pid")
# do something
end

Resources