I am using Ruby on Rails 5.2.0 to develop a system and I need to run a job every 2 minute.
I'm using sidekiq for background processing and sidekiq-cron for scheduling jobs.
I execute the following line in rails console:
job=Sidekiq::Cron::Job.new( name: 'TestWorker_Job', cron: '*/2 * * * *', klass: 'PriceWorker')
And this is the answer
=> #<Sidekiq::Cron::Job:0x000055b6d54707e8
#active_job=false,
#active_job_queue_name_delimiter=nil,
#active_job_queue_name_prefix=nil,
#args=[],
#cron="*/2 * * * *",
#fetch_missing_args=true,
#klass="PriceWorker",
#last_enqueue_time=nil,
#message={"retry"=>false, "queue"=>"default", "class"=>"PriceWorker", "args"=>[]},
#name="TestWorker_Job",
#queue="default",
#queue_name_with_prefix="default",
#status="enabled">
But it throws me an error:
job.errors
=> ["'cron' -> */2 * * * *: uninitialized constant Rufus::Scheduler::CronLine"]
There is no valid job. I don't know what i am doing wrong. Sidekiq-cron github page do the same thing.
sidekiq-cron is not targeting the correct version of rufus-scheduler. To make it work again, add this to your Gemfile:
gem 'rufus-scheduler', '~> 3.4.0'
Then run $ bundle install rufus-scheduler.
Related
What is the proper way to prevent Sinatra from displaying the full backtrace, when it fails to properly run the server (for example, due to the port being already in use)?
This is a sample sinatra app:
# test.rb
require 'sinatra'
require 'bundler/inline'
gemfile do
gem 'sinatra'
gem 'puma'
end
set :bind, "0.0.0.0"
set :port, 3000
get '/' do
"hello"
end
Then, running it with ruby test.rb once, to occupy the port.
Then, running it again in another terminal window, and this full error backtrace is shown:
$ ruby test.rb
== Sinatra (v2.0.4) has taken the stage on 3000 for development with backup from Puma
Puma starting in single mode...
* Version 3.12.0 (ruby 2.5.0-p0), codename: Llamas in Pajamas
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:3000
== Someone is already performing on port 3000!
Traceback (most recent call last):
5: from /store/gems/ruby-2.5.0/gems/sinatra-2.0.4/lib/sinatra/main.rb:26:in `block in <module:Sinatra>'
4: from /store/gems/ruby-2.5.0/gems/sinatra-2.0.4/lib/sinatra/base.rb:1464:in `run!'
3: from /store/gems/ruby-2.5.0/gems/sinatra-2.0.4/lib/sinatra/base.rb:1464:in `ensure in run!'
2: from /store/gems/ruby-2.5.0/gems/sinatra-2.0.4/lib/sinatra/base.rb:1439:in `quit!'
1: from /store/gems/ruby-2.5.0/gems/puma-3.12.0/lib/puma/launcher.rb:147:in `stop'
/store/gems/ruby-2.5.0/gems/puma-3.12.0/lib/puma/single.rb:27:in `stop': undefined method `stop' for nil:NilClass (NoMethodError)
Traceback (most recent call last):
3: from /store/gems/ruby-2.5.0/gems/sinatra-2.0.4/lib/sinatra/base.rb:1545:in `block in setup_traps'
2: from /store/gems/ruby-2.5.0/gems/sinatra-2.0.4/lib/sinatra/base.rb:1439:in `quit!'
1: from /store/gems/ruby-2.5.0/gems/puma-3.12.0/lib/puma/launcher.rb:147:in `stop'
/store/gems/ruby-2.5.0/gems/puma-3.12.0/lib/puma/single.rb:27:in `stop': undefined method `stop' for nil:NilClass (NoMethodError)
Since I am using it as an embedded server, I would like the output to simply and with the friendly error that Sinatra is already showing:
== Someone is already performing on port 3000!
and avoid showing the backtrace.
Ruby by default outputs error messages to the STDOUT. But if you're on *nix system you can do this:
ruby test.rb > /dev/null 2>&1
For windows you can probably do
ruby test.rb > NULL
windows powershell
ruby test.rb > $null
but for windows also see Is there a /dev/null on Windows?
But if you want programmatically suppress output when server is running this should work on *nix but not sure on windows
# test.rb
require 'sinatra'
require 'bundler/inline'
gemfile do
gem 'sinatra'
gem 'puma'
end
set :bind, "0.0.0.0"
set :port, 3000
get '/' do
"hello"
end
unless `ps aux | grep sinatra`.match('tcp://0.0.0.0:3000')
STDOUT.reopen('/dev/null', 'w')
STDERR.reopen('/dev/null', 'w')
end
See suppresing output to console with ruby
You can test to see if the port is in use by attempting to listen on the port before allowing Sinatra and Puma to take over. This isn't 100% effective because there's a race condition where you may open and close the port, but before Sinatra/Puma finish initializing some other process comes along and listens on the same port, but it should work for your use-case (which appears to be a cosmetic hack only).
Insert this code anywhere in test.rb:
require 'socket'
include Socket::Constants
begin
# Open and close the port
socket = Socket.new(AF_INET, SOCK_STREAM, 0)
sockaddr = Socket.pack_sockaddr_in(3000, '0.0.0.0')
socket.bind(sockaddr)
socket.listen(1)
socket.close
rescue Errno::EADDRINUSE => error
# Traps the same error that is trapped by Sinatra and exits if raised
puts error.message
exit
end
Start the first one with ruby test.rb:
== Sinatra (v2.0.4) has taken the stage on 3000 for development with backup from Puma
Puma starting in single mode...
* Version 3.12.0 (ruby 2.6.0-p-1), codename: Llamas in Pajamas
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Start the second one with ruby test.rb:
Address already in use - bind(2) for 0.0.0.0:3000
You can flesh out what you want printed to the console inside the rescue block.
This appears to be caused by an issue with Puma, that is fixed by this PR.
I updated one of my apps to Rails 5 and upgraded the Ruby version to 2.3.1 as well. The app already used Puma prior to the Rails 5 upgrade as well and was deployed on a Digital Ocean droplet.
When I start rails server locally, I get the normal output in my Rails log, which I've copied below.
=> Booting Puma
=> Rails 5.0.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
[14669] Puma starting in cluster mode...
[14669] * Version 3.4.0 (ruby 2.3.1-p112), codename: Owl Bowl Brawl
[14669] * Min threads: 5, max threads: 5
[14669] * Environment: development
[14669] * Process workers: 2
[14669] * Preloading application
[14669] * Listening on tcp://localhost:3000
[14669] Use Ctrl-C to stop
[14669] - Worker 1 (pid: 14684) booted, phase: 0
[14669] - Worker 0 (pid: 14683) booted, phase: 0
Everything looks normal to me. When I visit localhost:3000, the browser has a pending request that is pending indefinitely. There is no further activity in the Rails log acknowledging that any request is being received.
Has anyone encountered this type of issue, or know of any potential causes for that?
Resolved this issue, and confirmed by #marvindanig who was experiencing the same issue, that the 'tmp' folder needed to be cleared. There is a rake task in rails to do so...
rake tmp:clear
I am trying to use the Rufus Scheduler (within Dashing) to schedule a cron job, but also have it run once upon the server spinning up. I am following the readme here where it is saying to do the following:
scheduler.cron '00 14 * * *', :first_in => '3d' do
# ... every day at 14h00, but start after 3 * 24 hours
end
When I try to do this, I get the following error in my job:
`cron': unknown option: :first_in (ArgumentError)
Has anyone come across this?
Dashing is using rufus-scheduler 2.0.24 ( https://github.com/Shopify/dashing/blob/55f90939eae4d6eb64822fd3590f694418396510/dashing.gemspec#L24 ) which doesn't support the first_in feature for cron.
First_in was introduced for cron in rufus-scheduler 3.0.
It seems you're reading the rufus-scheduler 3.x documentation instead of the 2.x one.
The documentation for rufus-scheduler is at https://github.com/jmettraux/rufus-scheduler#rufus-scheduler , on top of it, there is the link to the 2.x documentation ( https://github.com/jmettraux/rufus-scheduler/blob/two/README.rdoc ). You'll have better luck there.
A 2.x alternative would be:
scheduler.in '3d' do
scheduler.cron '00 14 * * *' do
# ... every day at 1400
end
end
How do I get Redmine to start fast on Linux (CentOS)?
I upgraded all last week: latest Redmine, Ruby, Passenger, etc...
I tried about all I could find in the Redmine forum and other posts of getting it to speed up faster, that is: when requesting the Redmine website after a few hours being idle, it starts slow, but then it's blazing fast.
I am using Apache web server with Passenger. Below my current apache config, please some advice, as I am out of ideas:
LoadModule passenger_module /usr/local/rvm/gems/ruby-1.9.3-p448/gems/passenger-4.0.19/buildout/apache2/mod_passenger.so
PassengerRoot /usr/local/rvm/gems/ruby-1.9.3-p448/gems/passenger-4.0.19
PassengerDefaultRuby /usr/local/rvm/wrappers/ruby-1.9.3-p448/ruby
# Refs:
# http://stackoverflow.com/questions/8235309/redmine-perfomance-inconsistency
# http://www.redmine.org/boards/2/topics/31783
# This option should be 0, but has an issue: https://code.google.com/p/phusion-passenger/issues/detail?id=904
PassengerPoolIdleTime 999999
PassengerMinInstances 2
PassengerHighPerformance on
PassengerPreStart https://myhost/redmine
PassengerMaxPoolSize 5
PassengerMaxInstancesPerApp 4
PassengerStatThrottleRate 10
RailsAppSpawnerIdleTime 0
PassengerMaxPreloaderIdleTime 0
RailsBaseURI /redmine
RailsEnv production
I solved this by setting up a cron job to request the redmine homepage every 15 minutes:
*/15 * * * * /usr/bin/curl http://redmine_server/ --stderr - > /dev/null
I am using PostgreSQL, Rails 3.1.3 and Ruby 1.9.3. I am struggling to use db:migrate as outlined here.
This is what I am seeing in the terminal:
funkdified#funkdified-laptop:~/railsprojects/hartl$ bundle exec rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
** Execute db:migrate
== AddEmailUniquenessIndex: migrating ========================================
-- add_index(:users, :email, {:unique=>true})
and then the code hangs at this point. Any ideas why?
From: development.log
[1m[36m (0.1ms)[0m [1mSHOW search_path[0m
[1m[35m (0.5ms)[0m SELECT "schema_migrations"."version" FROM "schema_migrations"
Migrating to CreateUsers (20120124022843)
Migrating to AddEmailUniquenessIndex (20120124093922)
[1m[36m (0.1ms)[0m [1mBEGIN[0m
[1m[35m (3.6ms)[0m SELECT distinct i.relname, d.indisunique, d.indkey, t.oid
FROM pg_class t
INNER JOIN pg_index d ON t.oid = d.indrelid
INNER JOIN pg_class i ON d.indexrelid = i.oid
WHERE i.relkind = 'i'
AND d.indisprimary = 'f'
AND t.relname = 'users'
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) )
ORDER BY i.relname
I just had a similar problem, where a very simple migration was stalling for no apparent reason. I believe the problem has to do with not being able to get a database connection. I exited a rails console session that I had open in another terminal and then the migration immediately finished with no problems.
I had same problem .. I found out that there was idle transaction which blocked further queries on this table ..
Run:
heroku pg:ps --app=...
To view database processes. You will have to kill idle process:
heroku pg:kill 913 --force --app=...
(913 is ID of idle process -> change it to your needs)
I just made two migrations. The first one created a new table, the second one removed fields from an existing table. The second migration was hanging, and the reason turned out to be a rails console session (rails console --sandbox) running in another terminal windows.
I had the same issue and after trying a bunch of solutions what worked was...shutting down all of my terminal sessions, restarting the computer, and trying again. Sometimes it's just power cycle magic.