質問

I am using the state_machine gem and the rufus-scheduler gem. State machine is working perfectly. Rufus-scheduler, however, is not. I am building this program just to get used to rufus gem. 10 seconds after :accepted is transitioned to :both, it should then transition to :only_employer. However, rufus-scheduler for some reason is not working. The transition to :both works, but then it does not transition to :only_employer. How do I fix this? Thanks.

class Schedule < ActiveRecord::Base
    require 'rufus-scheduler'
    scheduler = Rufus::Scheduler.new

    def set_schedule
        scheduler.in '10' do #documentation says this should be 10 seconds
          self.change_to_done
        end
    end

    state_machine :accepted, :initial => :none do
         after_transition :any => :both, :do => :set_schedule
         event :change_to_done do
             transition :both => :only_employer 
         end
    end

end
役に立ちましたか?

解決

Using rufus-scheduler for this is overkill.

def set_schedule
  Thread.new do
    sleep 10
    self.change_to_done
  end
end

should be sufficient.

But then is the "server" you run Rails on top of thread-friendly? If not, you'd better pass the task to something like resque or sidekick.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top