Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Caches complete test runs on the project/test_runs index page #6

Closed
wants to merge 1 commit into from

2 participants

Thomas Shafer Jared Grippe
Thomas Shafer

I did this so the index page doesn't calculate completed runs every time,
as that checks redis multiple times.

To accomplish this, I built a really simple in-memory cache to store any data.
You could easily change it to redis, however I thought an in memory cache
was faster and it shouldn't take up too much space.

BTW, I have no clue if this actually works. The specs do pass. But I couldn't get the web server to run, and didn't see any specs for the web server to test it against. I'm not super familiar with sinatra's render signature, so I based it off rails standard options. That should be looked at. And I don't know if the capture block works either. I believe haml implements capture too, so it might be ok. But the general idea, I believe, is there.

Thanks dude.

Thomas Shafer growlypants Caches complete test runs on the project/test_runs index page
I did this so the index page doesn't calculate completed runs every time,
as that checks redis multiple times.

To accomplish this, I built a really simple in-memory cache to store any data.
You could easily change it to redis, however I thought an in memory cache
was faster and it shouldn't take up too much space.
86956f2
Jared Grippe
Owner

done. =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 31, 2012
Thomas Shafer growlypants Caches complete test runs on the project/test_runs index page
I did this so the index page doesn't calculate completed runs every time,
as that checks redis multiple times.

To accomplish this, I built a really simple in-memory cache to store any data.
You could easily change it to redis, however I thought an in memory cache
was faster and it shouldn't take up too much space.
86956f2
This page is out of date. Refresh to see the latest.
1  lib/hobson.rb
@@ -31,6 +31,7 @@ module Hobson
31 31 autoload :Server, 'hobson/server'
32 32 autoload :Worker, 'hobson/worker'
33 33 autoload :CI, 'hobson/ci'
  34 + autoload :Cache, 'hobson/cache'
34 35
35 36 def root
36 37 @root ||= Pathname.new ENV['HOBSON_ROOT'] ||= Dir.pwd
39 lib/hobson/cache.rb
... ... @@ -0,0 +1,39 @@
  1 +class Hobson::Cache
  2 +
  3 + @@simple_cache = {}
  4 +
  5 + class << self
  6 +
  7 + def read key
  8 + @@simple_cache[key]
  9 + end
  10 +
  11 + def write key, value
  12 + @@simple_cache[key] = value
  13 + end
  14 +
  15 + def delete key
  16 + @@simple_cache.delete key
  17 + end
  18 +
  19 + def has_key? key
  20 + @@simple_cache.has_key? key
  21 + end
  22 +
  23 + def fetch key, &block
  24 + has_key?(key) ? read(key) : write(key, block.call)
  25 + end
  26 +
  27 + def clear
  28 + @@simple_cache = {}
  29 + end
  30 +
  31 + def build_key model, modifier=nil
  32 + key = model.respond_to?(:id) ? "#{model.class.name}-#{model.id}" : model
  33 + key << "-#{modifier}" if modifier.present?
  34 + key
  35 + end
  36 +
  37 + end
  38 +
  39 +end
1  lib/hobson/server.rb
@@ -190,6 +190,7 @@ def self.start! options={}
190 190 # delete
191 191 delete "/projects/:project_name/test_runs/:test_run_id" do
192 192 test_run.delete!
  193 + Hobson::Cache.delete Hobson::Cache.build_key(test_run, :list)
193 194 redirect test_runs_path
194 195 end
195 196
26 lib/hobson/server/views/projects/test_runs/_list.haml
@@ -14,24 +14,8 @@
14 14 %th Pending
15 15 %tbody
16 16 - test_runs.sort_by{|t| t.created_at || Time.now }.reverse.each do |test_run|
17   - %tr{:class => test_run.status.gsub(' ','-')}
18   - %td
19   - %form{:id => "delete#{test_run.id}", :action => test_run_path(test_run), :method => 'post'}
20   - %input{:type=>:hidden, :name=>'_method', :value=>'delete'}
21   - %a{:href => "javascript: $('#delete#{test_run.id}').submit()"} X
22   - %td= test_run.status
23   - %td
24   - %a{:href => test_run_path(test_run)}= test_run.id
25   - %td
26   - %a{:href => sha_url(test_run.project.origin, test_run.sha)}= test_run.sha[0..7]
27   - %td= test_run.requestor || 'unknown'
28   - %td= distance_of_time_in_words(Time.now - test_run.created_at) + ' ago' if test_run.created_at.present?
29   - %td= test_run.tests.find_all(&:waiting?).count
30   - %td= test_run.tests.map{|t| t.tries > 0 ? t.tries - 1 : 0}.sum
31   - %td= test_run.tests.find_all(&:fail?).count
32   - %td= test_run.tests.find_all(&:pass?).count
33   - %td= test_run.tests.find_all(&:pending?).count
34   - %td
35   - - max = test_run.tests.size
36   - - value = test_run.aborted? ? max : test_run.tests.reject(&:waiting?).size
37   - %progress{:value => value, :max => max}
  17 + - cache_key = Hobson::Cache.build_key test_run, :list
  18 + - if Hobson::Cache.has_key?(cache_key)
  19 + = Hobson::Cache.read(cache_key)
  20 + - else
  21 + = render :partial => :'projects/test_runs/test_run', :object => test_run, :locals => {:cache_key => cache_key}
24 lib/hobson/server/views/projects/test_runs/_test_run.html.haml
... ... @@ -0,0 +1,24 @@
  1 += content = capture do
  2 + %tr{:class => test_run.status.gsub(' ','-')}
  3 + %td
  4 + %form{:id => "delete#{test_run.id}", :action => test_run_path(test_run), :method => 'post'}
  5 + %input{:type=>:hidden, :name=>'_method', :value=>'delete'}
  6 + %a{:href => "javascript: $('#delete#{test_run.id}').submit()"} X
  7 + %td= test_run.status
  8 + %td
  9 + %a{:href => test_run_path(test_run)}= test_run.id
  10 + %td
  11 + %a{:href => sha_url(test_run.project.origin, test_run.sha)}= test_run.sha[0..7]
  12 + %td= test_run.requestor || 'unknown'
  13 + %td= distance_of_time_in_words(Time.now - test_run.created_at) + ' ago' if test_run.created_at.present?
  14 + %td= test_run.tests.find_all(&:waiting?).count
  15 + %td= test_run.tests.map{|t| t.tries > 0 ? t.tries - 1 : 0}.sum
  16 + %td= test_run.tests.find_all(&:fail?).count
  17 + %td= test_run.tests.find_all(&:pass?).count
  18 + %td= test_run.tests.find_all(&:pending?).count
  19 + %td
  20 + - max = test_run.tests.size
  21 + - value = test_run.aborted? ? max : test_run.tests.reject(&:waiting?).size
  22 + %progress{:value => value, :max => max}
  23 +
  24 +- Hobson::Cache.write cache_key, content if test_run.complete?
43 spec/hobson/cache_spec.rb
... ... @@ -0,0 +1,43 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Hobson::Cache do
  4 +
  5 + it 'should respond to write and read' do
  6 + Hobson::Cache.write(:a, :b).should == :b
  7 + Hobson::Cache.read(:a).should == :b
  8 + end
  9 +
  10 + it 'should respond to delete' do
  11 + Hobson::Cache.write(:a, :b).should == :b
  12 + Hobson::Cache.delete(:a).should == :b
  13 + end
  14 +
  15 + it 'should respond to fetch' do
  16 + Hobson::Cache.fetch(:a) { :b }.should == :b
  17 + Hobson::Cache.read(:a).should == :b
  18 + Hobson::Cache.fetch(:a) { :c }.should == :b
  19 + Hobson::Cache.delete(:a).should == :b
  20 + Hobson::Cache.fetch(:a) { :c }.should == :c
  21 + end
  22 +
  23 + it 'should respond to has_key?' do
  24 + Hobson::Cache.write(:a, :b).should == :b
  25 + Hobson::Cache.has_key?(:a).should be_true
  26 + Hobson::Cache.has_key?(:NOT_FOUND).should be_false
  27 + end
  28 +
  29 + it 'should build the proper cache key for models and other objects' do
  30 + Hobson::Cache.build_key(Hobson::Project::TestRun.new(nil, 1)).should == "Hobson::Project::TestRun-1"
  31 + Hobson::Cache.build_key(Hobson::Project::TestRun.new(nil, 2), :specific).should == "Hobson::Project::TestRun-2-specific"
  32 + Hobson::Cache.build_key(:abc).should == :abc
  33 + Hobson::Cache.build_key("123", :ab).should == "123-ab"
  34 + end
  35 +
  36 + it 'should respond to clear' do
  37 + Hobson::Cache.write(:a, :b).should == :b
  38 + Hobson::Cache.has_key?(:a).should be_true
  39 + Hobson::Cache.clear
  40 + Hobson::Cache.has_key?(:a).should be_false
  41 + end
  42 +
  43 +end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.