Skip to content

Commit

Permalink
First version working.
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Reeder committed Jun 18, 2011
1 parent 4f41c21 commit 9c9b694
Show file tree
Hide file tree
Showing 13 changed files with 573 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
72 changes: 72 additions & 0 deletions basic_tests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require_relative 'simple_worker_unit_test_base'
require 'active_support/core_ext'

# bumpasdf
class BasicTests < SimpleWorkerUnitTestBase

attr_accessor :config
merge_worker 'one_line_worker', 'OneLineWorker'

def test_for_truth
assert true
end

def test_fail
assert false, "dang"
end

def test_exception
raise "dang exception"
end

def test_queue
worker = OneLineWorker.new
worker.queue
status = worker.wait_until_complete
log status
assert status['status'] == 'complete'
log worker.get_log

end

def test_schedule
tr = OneLineWorker.new
start_date = 15.seconds.from_now
response_hash = tr.schedule(:start_at => start_date, :end_at=>start_date + 1.minutes, :priority=>2)
puts 'response_hash=' + response_hash.inspect
assert response_hash, "Couldn't get response"
if response_hash
assert response_hash["schedule_id"], "Wrong response code"
status = wait_for_task(:schedule_id=>response_hash["schedule_id"])
assert status, "Scheduled task wasn't executed"
assert status["status"] == "complete", "wrong task status"
tasks = SimpleWorker.service.get_schedules.collect { |schedule| schedule["schedule_id"] }
assert tasks.include?(response_hash["schedule_id"]), "Response should contains schedule id"
end
end


def wait_for_task(params={})
tries = 0
status = nil
sleep 1
while tries < 60
status = status_for(params)
puts 'status=' + status.inspect + ' for ' + (params.inspect)
if status["status"] == "complete" || status["status"] == "error"
break
end
sleep 2
end
status
end

def status_for(ob)
if ob.is_a?(Hash)
ob[:schedule_id] ? SimpleWorker.service.schedule_status(ob[:schedule_id]) : SimpleWorker.service.status(ob[:task_id])
else
ob.status
end
end

end
49 changes: 49 additions & 0 deletions db_tests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require_relative 'simple_worker_unit_test_base'

# bump..
class DbTests < SimpleWorkerUnitTestBase

merge_worker 'mysql_worker', "MysqlWorker"
merge_worker 'mysql2_worker', "Mysql2Worker"
merge_worker 'mongo_worker', "MongoWorker"


def test_mysql

mysql_worker = MysqlWorker.new
mysql_worker.config = @config
mysql_worker.queue

status = mysql_worker.wait_until_complete
log status
assert status['status'] == 'complete'
log "log=" + mysql_worker.get_log.to_s

end

def test_mysql2

mysql_worker = Mysql2Worker.new
mysql_worker.config = @config
mysql_worker.queue

status = mysql_worker.wait_until_complete
log status
assert status['status'] == 'complete'
log "log=" + mysql_worker.get_log.to_s

end

def test_mongoid

mw = MongoWorker.new
mw.config = @config
mw.queue
status = mw.wait_until_complete
log status
assert status['status'] == 'complete'
log mw.get_log


end
end
24 changes: 24 additions & 0 deletions enqueue_tests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'simple_worker'
require 'yaml'
require_relative 'db_tests'
require_relative 'my_suite_worker'

@config = YAML.load_file('config.yml')
p @config

SimpleWorker.configure do |config|
config.access_key = @config["sw_access_key"]
config.secret_key = @config["sw_secret_key"]
end

#test = DbTest.new(@config)
#test.run_local

suite_worker = MySuiteWorker.new
suite_worker.config = @config
suite_worker.add('basic_tests', 'BasicTests', @config)
suite_worker.add('db_tests', 'DbTests', @config)
suite_worker.setup
suite_worker.queue
status = suite_worker.wait_until_complete
p status
52 changes: 52 additions & 0 deletions mongo_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Sample worker that connects to MongoDB and performs some operations.

require 'simple_worker'
require 'mongoid'

class MongoWorker < SimpleWorker::Base

attr_accessor :config

merge 'person'

def run
init_mongodb

log "saving person..."
person = Person.new(:first_name => "Ludwig", :last_name => "Beethoven the #{rand(100)}")
person.save!
log person.inspect

sleep 2

log "querying persons..."

persons = Person.find(:all, :conditions=>{:first_name=>"Ludwig"})
persons.each do |p|
log "found #{p.first_name} #{p.last_name}"
end


end

# Configures settings for MongoDB. Values for mongo_host and mongo_port are passed in
# to make the example easy to understand. Could be placed directly inline to streamline.
def init_mongodb
mconfig = @config['mongo']
mongo_db_name = mconfig["mongo_db_name"]
mongo_host = mconfig["mongo_host"]
mongo_port = mconfig["mongo_port"]
mongo_username = mconfig['mongo_username']
mongo_password = mconfig['mongo_password']

Mongoid.configure do |config|
config.database = Mongo::Connection.new(mongo_host, mongo_port).db(mongo_db_name)
config.database.authenticate(mongo_username, mongo_password)
# config.slaves = [
# Mongo::Connection.new(host, 27018, :slave_ok => true).db(name)
# ]
config.persist_in_safe_mode = false
end
end

end
25 changes: 25 additions & 0 deletions my_suite_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require_relative 'suite_worker'

# bump asdf

class MySuiteWorker < SuiteWorker

attr_accessor :config

merge_gem 'hipchat-api'

def on_complete

log 'POSTING TO HIPCHAT!'

client = HipChat::API.new(config['hipchat']['api_key'])
# puts client.rooms_list
notify_users = false
msg = suite_results_output(:format=>'html')
if num_failed == 0
msg = "LBJ? - We're stylin'!<br/>" + msg
end
log "POSTED: " + client.rooms_message(config['hipchat']['room_name'], 'UnitTestWorker', msg, notify_users).body
end

end
26 changes: 26 additions & 0 deletions mysql2_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# this one uses the mysql2 gem as it has separate issues from mysql gem. One is that it doesn't work with rails 3.0.x!

require 'mysql2'
require 'active_record'

class Mysql2Worker < SimpleWorker::Base

attr_accessor :config

def connect(config)
ActiveRecord::Base.establish_connection(config['mysql'].merge!(:adapter=>'mysql2'))
end

merge "photo"

def run

connect(config)

photo = Photo.create!(:name=>"hi", :url=>"http://www.whatever.com")
photo = Photo.first
log "first photo=" + photo.inspect

end

end
24 changes: 24 additions & 0 deletions mysql_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'mysql'
require 'active_record'

class MysqlWorker < SimpleWorker::Base

attr_accessor :config

def connect(config)
ActiveRecord::Base.establish_connection(config['mysql'].merge!(:adapter=>'mysql'))
end

merge "photo"

def run

connect(config)

photo = Photo.create!(:name=>"hi", :url=>"http://www.whatever.com")
photo = Photo.first
log "first photo=" + photo.inspect

end

end
11 changes: 11 additions & 0 deletions one_line_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'simple_worker'

# bump adsf
class OneLineWorker < SimpleWorker::Base

def run
log Time.now
log "hello"
log Time.now
end
end
8 changes: 8 additions & 0 deletions person.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'mongoid'

class Person
include Mongoid::Document
field :first_name
field :middle_initial
field :last_name
end
3 changes: 3 additions & 0 deletions photo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Photo < ActiveRecord::Base

end
Loading

0 comments on commit 9c9b694

Please sign in to comment.