Thursday, March 22, 2012

Update DelayedJob run_at to change daily emails send time

Below is just a sample code.
1) Create a table to track delayed jobs

class CreateTrackDelayedJobs < ActiveRecord::Migration
  def self.up
    create_table :track_delayed_jobs do |t|
      t.integer :delayed_job_id
      t.integer :user_id
      t.integer :item_id

      t.timestamps
    end
  end

  def self.down
    drop_table :track_delayed_jobs
  end
end




2)Sample code to send daily news letters

a) Add a rake task which can be run everyday

task :send_news_letters => :environment do
        items = Item.all
        items.each do |current_item|
          
            send_time = Time.zone.local(Date.today.year, Date.today.month, Date.today.day, hours, minutes)
            NewsLetter.send_at(send_time, :send_digest, curent_item.id)
        end

b)

class NewsLetter

 def self.send_in(time, method, *args)
        send_at time.from_now, method, *args
    end

    def self.send_at(time, method, *args)
        dj = Delayed::Job.enqueue Delayed::PerformableMethod.new(self, method.to_sym, args), 0, time
        track_dj = TrackDelayedJob.where("item_id = ?",args[0]).first
        if track_dj.nil?
            TrackDelayedJob.create(:delayed_job_id => dj.id, :item_id => args[0])
        else
            old_dj = Delayed::Job.find_by_id(track_dj.delayed_job_id)
            old_dj.destroy unless old_dj.nil?
            track_dj.update_attribute(:delayed_job_id, dj.id)
        end
    end

def self.send_digest(item_id)
#write your code to send digest.
end

end

c) When you want to update already set delayed job then

NewsLetter.update_delayed_job_time(item_id, time)

add this in NewsLetter class
def self.set_delayed_job_time(item_id, time)
   
        track_dj = TrackDelayedJob.where("item_id = ?", item_id).first
        dj = Delayed::Job.find_by_id(track_dj.delayed_job_id) unless track_dj.nil?
        unless dj.nil?
            #below code is required in case you want to convert hours and minutes
            #   time = "#{Date.today} #{hours}:#{minutes}".to_time           
            dj.run_at = time
            dj.save
        end
    end

end

Saturday, March 17, 2012

Rails - Install and starting memcached on windows

1) Download Memcached for windows and extract it.

2) Open the command prompt as administrator and then run the command (assuming
your memcached is in c:\memcached)

c:\memcached\memcached.exe -d install

(Incase you get the below error:
C:\Windows\System32>c:\memcached\memcached.exe -d install
failed to install service or service already installed
Please make sure your are running the command prompt as administrator
start>command prompt> right click it and run as administrator.)

3) Start the server by:
C:\Windows\system32>net start "memcached Server"
The memcached Server service is starting.
The memcached Server service was started successfully.

Saturday, March 10, 2012

Rails Resque + RuntimeError at /resque/overview ERR unknown command 'server' issue

Issue: RuntimeError at /resque/overview ERR unknown command 'server' on windows at localhost:3000/overview


Fix: Just do a 'gem cleanup resque' to remove older versions of resque and then 'bundle update'.

Friday, March 2, 2012

Rails 3 Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.

Issue:
Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.
/home/user/.rvm/gems/ruby-1.9.2-head/gems/execjs-1.2.6/lib/execjs/runtimes.rb:46:in `autodetect'
/home/user/.rvm/gems/ruby-1.9.2-head/gems/execjs-1.2.6/lib/execjs.rb:5:in `'
/home/user/.rvm/gems/ruby-1.9.2-head/gems/execjs-1.2.6/lib/execjs.rb:4:in `'
/home/user/.rvm/gems/ruby-1.9.2-head/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `require'


Fix:

Add gem ‘therubyracer’ into Gemfile and then do a 'bundle install'

Thursday, March 1, 2012

Running a specific migration file


1) Incase you want to run migration from a particular file then 
rails console>> require "db/migrate/20110210070832_create_companies.rb"
>> CreateCompanies.up
 
other ways:
 
rake db:migrate:up VERSION=my_version
rake db:migrate:down VERSION=my_version