You are currently browsing the tag archive for the ‘Rails’ tag.
Deletes taking to long, just take a break ![]()
(so replication can catch up/things get unblocked)
Only use save sql, there is no escapeing.
class ActiveRecord::Base
def self.slow_delete_all(condition)
count = 0
loop do
result = ActiveRecord::Base.connection.send(:delete_sql, "delete from #{table_name} where #{condition} limit 10000")
count += result
break if result == 0
sleep 1
end
count
end
end
require ‘resque/tasks’
require ‘resque_scheduler/tasks’
Scheduler needs very little cpu, just start it with a worker.
desc "schedule and work, so we only need 1 dyno"
task :schedule_and_work do
if Process.fork
sh "rake environment resque:work"
else
sh "rake resque:scheduler"
Process.wait
end
end
When testing resque jobs, the simples solution is to set Resque.inline = true, which just executes the jobs imediatly, but if you want to make sure that they e.g. have been scheduled for the right time or simulate non-parallel execution, you might find these useful.
ResqueScheduler
module ResqueScheduler
def all_scheduled_jobs_count
total_jobs = Hash.new(0)
Array(redis.zrange(:delayed_queue_schedule, 0, -1)).each do |timestamp|
total_jobs[timestamp.to_i] += redis.llen("delayed:#{timestamp}").to_i
end
total_jobs
end
end
Resque
module Resque
def self.perform_enqueue_and_scheduled(queue)
while timestamp = Resque.next_delayed_timestamp
Resque::Scheduler.enqueue_delayed_items_for_timestamp(timestamp)
end
Resque.perform_all(queue)
end
def self.perform_all(queue_name)
until size(queue_name) == 0
reserve(queue_name).perform
end
end
end
Make your rspec output look more interesting!
(yes we had too much spare time at our hands…
)
# spec/spec_helper.rb
# encoding: UTF-8
if defined? RSpec::Core::Formatters::ProgressFormatter
RSpec::Core::Formatters::ProgressFormatter.class_eval do
DOTS = ['☘','⚘']
def example_passed(example)
output.print green(DOTS[rand(DOTS.size)])
end
end
end
If your environments/*.rb look like a repetitive mess, its time to get a config.yml!
- overview of your configuration
- dry code
- (optional) not check in all the passwords/keys of your app (only check in config.example.yml), great for open-source apps
- Can be loaded without loading the environment (e.g. small rake tasks)
# lib/cfg.rb
CFG = YAML.load(ERB.new(
File.read("config/config.yml")
).result)[Rails.env].with_indifferent_access
# config/application.rb
require "cfg"
# config/config.yml
common: &common
api:
airbrake_key: xxx
google_analytics_key: yyy
admin_email: admin@example.com
test:
<<: *common
host: 'localhost'
development:
<<: *common
host: 'localhost'
production:
<<: *common
host: 'fuu.bar'
# config/application.rb
config.action_mailer.default_url_options = {:host => CFG[:host]}
