ruby on rails - Resque and redis wait for variable to be set -
currently running on rails 3.1 rc4 , using redis , resque queueing creation of rackspace servers.
the rackspace gem using, cloudservers, tells when server done being setup status method.
what trying code below execute code in elsif after server active , ready used.
class servergenerator @queue = :servers_queue def self.perform(current_id) current_user = user.find(current_id) cs = cloudservers::connection.new(:username => "***blocked security***", :api_key => "***blocked security***") image = cs.get_image(49) # set linux distro flavor = cs.get_flavor(1) # use 256 mb of ram instance newserver = cs.create_server(:name => "#{current_user.name}", :imageid => image.id, :flavorid => flavor.id) if newserver.status == "build" newserver.refresh elsif newserver.status == "active" # stuff here, generated server different, static name # see if working cs = cloudservers::connection.new(:username => "***blocked security***", :api_key => "***blocked security***") image = cs.get_image(49) flavor = cs.get_flavor(1) newserver = cs.create_server(:name => "working", :imageid => image.id, :flavorid => flavor.id) end end end
when ran above, generated first server uses "current_user.name", it's name. loop around if statement? seems poor way of queueing tasks. should enqueue new task checks see if server ready or not?
thanks bunch!
based upon you've written, i'm assuming cs.create_server
non-blocking. in case, yes, need wrap check in ... loop or similar construct. otherwise you're checking value precisely once , exiting perform method.
if you're going loop in method, should add in sleep calls, otherwise you're going burn lot of cpu cycles doing nothing. whether loop or call separate job , whether workers idle. put way, if takes 5 min. server come up, , loop, worker not going able process other jobs 5 min. if that's acceptable, it's easiest thing do. if not acceptable, you'll want job accepts server id , makes api call see if it's available.
that process can tricky though. if server never comes online whatever reason, find creating jobs waiting status ad infinitum. so, want pass sort of execution count around too, or keep track in redis, stop trying after x number of tries. i'd check out resque-scheduler can exert control on when job gets executed in case.
Comments
Post a Comment