lib/qless/server.rb in qless-0.9.2 vs lib/qless/server.rb in qless-0.9.3

- old
+ new

@@ -14,16 +14,15 @@ set :reload_templates, true # I'm not sure what this option is -- I'll look it up later # set :static, true - def self.client - @client ||= Qless::Client.new - end + attr_reader :client - def self.client=(client) + def initialize(client) @client = client + super end helpers do include Rack::Utils @@ -85,27 +84,27 @@ {:name => 'About' , :path => '/about' } ] end def application_name - return Server.client.config['application'] + return client.config['application'] end def queues - return Server.client.queues.counts + return client.queues.counts end def tracked - return Server.client.jobs.tracked + return client.jobs.tracked end def workers - return Server.client.workers.counts + return client.workers.counts end def failed - return Server.client.jobs.failed + return client.jobs.failed end # Return the supplied object back as JSON def json(obj) content_type :json @@ -119,16 +118,16 @@ # What are the top tags? Since it might go on, say, every # page, then we should probably be caching it def top_tags @top_tags ||= { - :top => Server.client.tags, + :top => client.tags, :fetched => Time.now } if (Time.now - @top_tags[:fetched]) > 60 then @top_tags = { - :top => Server.client.tags, + :top => client.tags, :fetched => Time.now } end @top_tags[:top] end @@ -155,65 +154,65 @@ erb :overview, :layout => true, :locals => { :title => "Overview" } end # Returns a JSON blob with the job counts for various queues get '/queues.json' do - json(Server.client.queues.counts) + json(client.queues.counts) end get '/queues/?' do erb :queues, :layout => true, :locals => { :title => 'Queues' } end # Return the job counts for a specific queue get '/queues/:name.json' do - json(Server.client.queues[params[:name]].counts) + json(client.queues[params[:name]].counts) end filtered_tabs = %w[ running scheduled stalled depends recurring ].to_set get '/queues/:name/?:tab?' do - queue = Server.client.queues[params[:name]] + queue = client.queues[params[:name]] tab = params.fetch('tab', 'stats') jobs = if tab == 'waiting' queue.peek(20) elsif filtered_tabs.include?(tab) - paginated(queue.jobs, tab).map { |jid| Server.client.jobs[jid] } + paginated(queue.jobs, tab).map { |jid| client.jobs[jid] } else [] end erb :queue, :layout => true, :locals => { :title => "Queue #{params[:name]}", :tab => tab, :jobs => jobs, - :queue => Server.client.queues[params[:name]].counts, + :queue => client.queues[params[:name]].counts, :stats => queue.stats } end get '/failed.json' do - json(Server.client.jobs.failed) + json(client.jobs.failed) end get '/failed/?' do # qless-core doesn't provide functionality this way, so we'll # do it ourselves. I'm not sure if this is how the core library # should behave or not. erb :failed, :layout => true, :locals => { :title => 'Failed', - :failed => Server.client.jobs.failed.keys.map { |t| Server.client.jobs.failed(t).tap { |f| f['type'] = t } } + :failed => client.jobs.failed.keys.map { |t| client.jobs.failed(t).tap { |f| f['type'] = t } } } end get '/failed/:type/?' do erb :failed_type, :layout => true, :locals => { :title => 'Failed | ' + params[:type], :type => params[:type], - :failed => paginated(Server.client.jobs, :failed, params[:type]) + :failed => paginated(client.jobs, :failed, params[:type]) } end get '/track/?' do erb :track, :layout => true, :locals => { @@ -223,11 +222,11 @@ get '/jobs/:jid' do erb :job, :layout => true, :locals => { :title => "Job | #{params[:jid]}", :jid => params[:jid], - :job => Server.client.jobs[params[:jid]] + :job => client.jobs[params[:jid]] } end get '/workers/?' do erb :workers, :layout => true, :locals => { @@ -236,32 +235,32 @@ end get '/workers/:worker' do erb :worker, :layout => true, :locals => { :title => 'Worker | ' + params[:worker], - :worker => Server.client.workers[params[:worker]].tap { |w| - w['jobs'] = w['jobs'].map { |j| Server.client.jobs[j] } - w['stalled'] = w['stalled'].map { |j| Server.client.jobs[j] } + :worker => client.workers[params[:worker]].tap { |w| + w['jobs'] = w['jobs'].map { |j| client.jobs[j] } + w['stalled'] = w['stalled'].map { |j| client.jobs[j] } w['name'] = params[:worker] } } end get '/tag/?' do - jobs = paginated(Server.client.jobs, :tagged, params[:tag]) + jobs = paginated(client.jobs, :tagged, params[:tag]) erb :tag, :layout => true, :locals => { :title => "Tag | #{params[:tag]}", :tag => params[:tag], - :jobs => jobs['jobs'].map { |jid| Server.client.jobs[jid] }, + :jobs => jobs['jobs'].map { |jid| client.jobs[jid] }, :total => jobs['total'] } end get '/config/?' do erb :config, :layout => true, :locals => { :title => 'Config', - :options => Server.client.config.all + :options => client.config.all } end get '/about/?' do erb :about, :layout => true, :locals => { @@ -271,11 +270,11 @@ # These are the bits where we accept AJAX requests post "/track/?" do # Expects a JSON-encoded hash with a job id, and optionally some tags data = JSON.parse(request.body.read) - job = Server.client.jobs[data["id"]] + job = client.jobs[data["id"]] if not job.nil? data.fetch("tags", false) ? job.track(*data["tags"]) : job.track() if request.xhr? json({ :tracked => [job.jid] }) else @@ -290,11 +289,11 @@ end end post "/untrack/?" do # Expects a JSON-encoded array of job ids to stop tracking - jobs = JSON.parse(request.body.read).map { |jid| Server.client.jobs[jid] }.select { |j| not j.nil? } + jobs = JSON.parse(request.body.read).map { |jid| client.jobs[jid] }.select { |j| not j.nil? } # Go ahead and cancel all the jobs! jobs.each do |job| job.untrack() end return json({ :untracked => jobs.map { |job| job.jid } }) @@ -304,11 +303,11 @@ # Expects a JSON-encoded dictionary of jid => priority response = Hash.new r = JSON.parse(request.body.read) r.each_pair do |jid, priority| begin - Server.client.jobs[jid].priority = priority + client.jobs[jid].priority = priority response[jid] = priority rescue response[jid] = 'failed' end end @@ -318,11 +317,11 @@ post "/tag/?" do # Expects a JSON-encoded dictionary of jid => [tag, tag, tag] response = Hash.new JSON.parse(request.body.read).each_pair do |jid, tags| begin - Server.client.jobs[jid].tag(*tags) + client.jobs[jid].tag(*tags) response[jid] = tags rescue response[jid] = 'failed' end end @@ -332,11 +331,11 @@ post "/untag/?" do # Expects a JSON-encoded dictionary of jid => [tag, tag, tag] response = Hash.new JSON.parse(request.body.read).each_pair do |jid, tags| begin - Server.client.jobs[jid].untag(*tags) + client.jobs[jid].untag(*tags) response[jid] = tags rescue response[jid] = 'failed' end end @@ -347,11 +346,11 @@ # Expects a JSON-encoded hash of id: jid, and queue: queue_name data = JSON.parse(request.body.read) if data["id"].nil? or data["queue"].nil? halt 400, "Need id and queue arguments" else - job = Server.client.jobs[data["id"]] + job = client.jobs[data["id"]] if job.nil? halt 404, "Could not find job" else job.move(data["queue"]) return json({ :id => data["id"], :queue => data["queue"]}) @@ -363,11 +362,11 @@ # Expects a JSON-encoded hash of id: jid, and queue: queue_name data = JSON.parse(request.body.read) if data["id"].nil? halt 400, "Need id" else - job = Server.client.jobs[data["id"]] + job = client.jobs[data["id"]] if job.nil? halt 404, "Could not find job" else job.undepend(data['dependency']) return json({:id => data["id"]}) @@ -379,15 +378,15 @@ # Expects a JSON-encoded hash of id: jid, and queue: queue_name data = JSON.parse(request.body.read) if data["id"].nil? halt 400, "Need id" else - job = Server.client.jobs[data["id"]] + job = client.jobs[data["id"]] if job.nil? halt 404, "Could not find job" else - queue = job.history[-1]["q"] + queue = job.raw_queue_history[-1]["q"] job.move(queue) return json({ :id => data["id"], :queue => queue}) end end end @@ -397,21 +396,21 @@ # Expects a JSON-encoded hash of type: failure-type data = JSON.parse(request.body.read) if data["type"].nil? halt 400, "Neet type" else - return json(Server.client.jobs.failed(data["type"], 0, 500)['jobs'].map do |job| - queue = job.history[-1]["q"] + return json(client.jobs.failed(data["type"], 0, 500)['jobs'].map do |job| + queue = job.raw_queue_history[-1]["q"] job.move(queue) { :id => job.jid, :queue => queue} end) end end post "/cancel/?" do # Expects a JSON-encoded array of job ids to cancel - jobs = JSON.parse(request.body.read).map { |jid| Server.client.jobs[jid] }.select { |j| not j.nil? } + jobs = JSON.parse(request.body.read).map { |jid| client.jobs[jid] }.select { |j| not j.nil? } # Go ahead and cancel all the jobs! jobs.each do |job| job.cancel() end @@ -426,10 +425,10 @@ # Expects a JSON-encoded hash of type: failure-type data = JSON.parse(request.body.read) if data["type"].nil? halt 400, "Neet type" else - return json(Server.client.jobs.failed(data["type"])['jobs'].map do |job| + return json(client.jobs.failed(data["type"])['jobs'].map do |job| job.cancel() { :id => job.jid } end) end end