employer-mongoid 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .apt-cache
4
+ .bundle
5
+ .config
6
+ .vagrant
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/.pryrc ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler"
2
+ Bundler.require
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation -I. -rspec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in employer-mongoid.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mark Kremer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Employer::Mongoid
2
+
3
+ Mongoid backend for Employer
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'employer-mongoid'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install employer-mongoid
18
+
19
+ ## Usage
20
+
21
+ Ensure that your Employer configuration starts with something like the below:
22
+
23
+ ```ruby
24
+ require "./config/environment.rb"
25
+ require "employer-mongoid"
26
+
27
+ pipeline_backend Employer::Mongoid::Pipeline.new
28
+ ```
29
+
30
+ If you're not using Rails then require whatever sets up your application's
31
+ environment instead of config/environment.rb before requiring employer-mongoid.
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/Vagrantfile ADDED
@@ -0,0 +1,16 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ Vagrant::Config.run do |config|
5
+ config.vm.host_name = "employer"
6
+ config.vm.box = "precise64"
7
+ config.vm.box_url = "http://files.vagrantup.com/precise64.box"
8
+ config.vm.network :hostonly, "192.168.33.33"
9
+ config.vm.customize ["modifyvm", :id, "--memory", 512]
10
+ config.vm.share_folder "v-root", "/vagrant", ".", :nfs => !(ENV["OS"] =~ /windows/i)
11
+ if File.directory?(File.expand_path("./.apt-cache/partial/"))
12
+ config.vm.share_folder "apt-cache", "/var/cache/apt/archives", "./.apt-cache", :owner => "root", :group => "root"
13
+ end
14
+ config.vm.provision :shell, :path => "script/vagrant-keep-agent-forwarding"
15
+ config.vm.provision :shell, :path => "script/vagrant-provision"
16
+ end
@@ -0,0 +1,16 @@
1
+ development:
2
+ sessions:
3
+ default:
4
+ database: employer
5
+ hosts:
6
+ - localhost:27017
7
+ options:
8
+ allow_dynamic_fields: false
9
+ test:
10
+ sessions:
11
+ default:
12
+ database: employer
13
+ hosts:
14
+ - localhost:27017
15
+ options:
16
+ allow_dynamic_fields: false
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'employer-mongoid/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "employer-mongoid"
8
+ gem.version = Employer::Mongoid::VERSION
9
+ gem.authors = ["Mark Kremer"]
10
+ gem.email = ["mark@without-brains.net"]
11
+ gem.summary = %q{Mongoid backend for Employer}
12
+ gem.homepage = "https://github.com/mkremer/employer-mongoid"
13
+ gem.license = "MIT"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency "mongoid", "~> 3.0"
21
+ gem.add_runtime_dependency "employer", "~> 0.1"
22
+
23
+ gem.add_development_dependency "rspec"
24
+ gem.add_development_dependency "pry"
25
+ end
@@ -0,0 +1,14 @@
1
+ module Employer
2
+ module Mongoid
3
+ module Employee
4
+ def perform_job
5
+ super
6
+ ensure
7
+ ::Mongoid::IdentityMap.clear
8
+ ::Mongoid.sessions.keys.each do |session_name|
9
+ ::Mongoid.session(session_name).disconnect
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ require "mongoid"
2
+
3
+ module Employer
4
+ module Mongoid
5
+ class Job
6
+ include ::Mongoid::Document
7
+ include ::Mongoid::Timestamps
8
+ store_in collection: "employer_jobs"
9
+
10
+ field :state, type: Symbol, default: :free
11
+ field :type, type: String
12
+ field :properties, type: Hash, default: {}
13
+
14
+ validates :state, presence: true
15
+ validates :type, presence: true
16
+
17
+ scope :free, where(state: :free)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ module Employer
2
+ module Mongoid
3
+ class Pipeline
4
+ def enqueue(job_hash)
5
+ job_attributes = {
6
+ type: job_hash[:class],
7
+ properties: job_hash[:attributes]
8
+ }
9
+
10
+ Employer::Mongoid::Job.create!(job_attributes).id
11
+ end
12
+
13
+ def dequeue
14
+ if job = Employer::Mongoid::Job.free.asc(:created_at).find_and_modify({"$set" => {state: :locked}}, new: true)
15
+ {id: job.id, class: job.type, attributes: job.properties}
16
+ end
17
+ end
18
+
19
+ def complete(job)
20
+ Employer::Mongoid::Job.find(job.id).destroy
21
+ end
22
+
23
+ def reset(job)
24
+ Employer::Mongoid::Job.find(job.id).update_attributes(state: :free)
25
+ end
26
+
27
+ def fail(job)
28
+ Employer::Mongoid::Job.find(job.id).update_attributes(state: :failed)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module Employer
2
+ module Mongoid
3
+ VERSION = "0.1"
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require "employer-mongoid/version"
2
+ require "employer-mongoid/pipeline"
3
+ require "employer-mongoid/job"
4
+ require "employer-mongoid/employee"
5
+ require "employer/employees/abstract_employee"
6
+
7
+ Employer::Employees::AbstractEmployee.send(:include, Employer::Mongoid::Employee)
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # Ensure that SSH_AUTH_SOCK is kept
4
+ echo "Defaults env_keep+=\"SSH_AUTH_SOCK\"" > "/etc/sudoers.d/keep_ssh_auth_sock"
5
+ chmod 0440 /etc/sudoers.d/keep_ssh_auth_sock
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env bash
2
+ export DEBIAN_FRONTEND=noninteractive
3
+
4
+ # Add MongoDB APT repository
5
+ if [ -f "/etc/apt/sources.list.d/10gen.list" ]; then
6
+ echo "MongoDB already added to APT sources"
7
+ else
8
+ apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
9
+ echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" > "/etc/apt/sources.list.d/10gen.list"
10
+ fi
11
+
12
+ apt-get update > /dev/null
13
+ apt-get -y install autoconf automake bison build-essential curl g++ git-core libc6-dev libreadline6 libreadline6-dev libsqlite3-dev libssl-dev libtool libxml2-dev libxslt-dev libyaml-dev mongodb-10gen ncurses-dev openjdk-7-jdk openjdk-7-jre-headless openssl pkg-config sqlite3 subversion unzip zlib1g zlib1g-dev
14
+
15
+ # Use chruby to manage Rubies (and ruby-build to build them)
16
+ if [ -f "/usr/local/share/chruby/chruby.sh" ]; then
17
+ echo 'chruby already present'
18
+ else
19
+ # Download and install chruby
20
+ echo 'chruby not yet installed, installing now...'
21
+ wget -O chruby-0.3.2.tar.gz https://github.com/postmodern/chruby/archive/v0.3.2.tar.gz
22
+ tar -xzvf chruby-0.3.2.tar.gz
23
+ cd chruby-0.3.2/
24
+ make install
25
+ # Add to global profile (enabling chruby for all bash users)
26
+ echo '[ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ] || return' >> /etc/profile.d/chruby.sh
27
+ echo "source /usr/local/share/chruby/chruby.sh" >> /etc/profile.d/chruby.sh
28
+ # Add to global zprofile (enabling chruby for all zsh users)
29
+ echo "source /usr/local/share/chruby/chruby.sh" >> /etc/zsh/zprofile
30
+ # Install ruby-build
31
+ cd
32
+ git clone git://github.com/sstephenson/ruby-build.git
33
+ cd ruby-build
34
+ ./install.sh
35
+ # Use ruby-build to install MRI and JRuby
36
+ ruby-build 1.9.3-p374 /opt/rubies/ruby-1.9.3-p374
37
+ ruby-build jruby-1.7.2 /opt/rubies/jruby-1.7.2
38
+ # Set ruby-1.9 as the global default for bash users
39
+ echo "chruby ruby-1.9" >> /etc/profile.d/chruby.sh
40
+ # Set ruby-1.9 as the global default for zsh users
41
+ echo "chruby ruby-1.9" >> /etc/zsh/zprofile
42
+ fi
43
+
44
+ # Setup the bundle
45
+ source /usr/local/share/chruby/chruby.sh
46
+ chruby jruby && gem install bundler && cd /vagrant && bundle
47
+ chruby ruby-1.9 && gem install bundler && cd /vagrant && bundle
@@ -0,0 +1,42 @@
1
+ require "employer-mongoid/job"
2
+
3
+ describe Employer::Mongoid::Job do
4
+ before(:all) do
5
+ if File.exists?("config/mongoid.yml")
6
+ Mongoid.load!("config/mongoid.yml", :test)
7
+ else
8
+ Mongoid.load!("config/mongoid.yml.default", :test)
9
+ end
10
+ end
11
+
12
+ let(:job) { Employer::Mongoid::Job.new(state: :free, type: "TestJob") }
13
+
14
+ it "requires state" do
15
+ job.state = nil
16
+ job.should be_invalid
17
+ job.errors.should include(:state)
18
+ job.state = :free
19
+ job.should be_valid
20
+ end
21
+
22
+ it "requires type" do
23
+ job.type = nil
24
+ job.should be_invalid
25
+ job.errors.should include(:type)
26
+ job.type = "TestJob"
27
+ job.should be_valid
28
+ end
29
+
30
+ describe ".free" do
31
+ it "returns jobs with state free" do
32
+ job1 = Employer::Mongoid::Job.create(state: :free, type: "TestJob")
33
+ job2 = Employer::Mongoid::Job.create(state: :free, type: "TestJob")
34
+ job3 = Employer::Mongoid::Job.create(state: :locked, type: "TestJob")
35
+
36
+ free_jobs = Employer::Mongoid::Job.free.to_a
37
+ free_jobs.should include(job1)
38
+ free_jobs.should include(job2)
39
+ free_jobs.should_not include(job3)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,62 @@
1
+ require "employer-mongoid/pipeline"
2
+
3
+ describe Employer::Mongoid::Pipeline do
4
+ let(:pipeline) { Employer::Mongoid::Pipeline.new }
5
+
6
+ before(:each) do
7
+ stub_const("Employer::Mongoid::Job", Class.new)
8
+ end
9
+
10
+ describe "#enqueue" do
11
+ it "saves the job hash to the collection" do
12
+ job_hash = {class: "TestJob", attributes: {shape: "Square", color: "Blue"}}
13
+ job_attributes_hash = {type: "TestJob", properties: {shape: "Square", color: "Blue"}}
14
+ job_document = double("Job", id: "123")
15
+ Employer::Mongoid::Job.should_receive(:create!).with(job_attributes_hash).and_return(job_document)
16
+ pipeline.enqueue(job_hash).should eq("123")
17
+ end
18
+ end
19
+
20
+ describe "#dequeue" do
21
+ it "locks a job from the collection and returns it as a job hash" do
22
+ job_document = double("Job", type: "TestJob", id: "234", properties: {shape: "Triangle", color: "Green"})
23
+ free_jobs_scope = double("Free jobs")
24
+ sorted_scope = double("Sorted scope")
25
+ Employer::Mongoid::Job.should_receive(:free).and_return(free_jobs_scope)
26
+ free_jobs_scope.should_receive(:asc).with(:created_at).and_return(sorted_scope)
27
+ sorted_scope.should_receive(:find_and_modify).with({"$set" => {state: :locked}}, new: true).and_return(job_document)
28
+ job_hash = {id: "234", class: "TestJob", attributes: {shape: "Triangle", color: "Green"}}
29
+ pipeline.dequeue.should eq(job_hash)
30
+ end
31
+ end
32
+
33
+ describe "#complete" do
34
+ it "deletes the job from the collection" do
35
+ job_document = double("Job document")
36
+ job = double("Job", id: "345")
37
+ Employer::Mongoid::Job.should_receive(:find).with("345").and_return(job_document)
38
+ job_document.should_receive(:destroy)
39
+ pipeline.complete(job)
40
+ end
41
+ end
42
+
43
+ describe "#reset" do
44
+ it "unlocks the job in the collection" do
45
+ job_document = double("Job document")
46
+ job = double("Job", id: "456")
47
+ Employer::Mongoid::Job.should_receive(:find).with("456").and_return(job_document)
48
+ job_document.should_receive(:update_attributes).with({state: :free})
49
+ pipeline.reset(job)
50
+ end
51
+ end
52
+
53
+ describe "#fail" do
54
+ it "marks the job as failed in the collection" do
55
+ job_document = double("Job document")
56
+ job = double("Job", id: "567")
57
+ Employer::Mongoid::Job.should_receive(:find).with("567").and_return(job_document)
58
+ job_document.should_receive(:update_attributes).with({state: :failed})
59
+ pipeline.fail(job)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1 @@
1
+ require "pry"
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: employer-mongoid
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mark Kremer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: employer
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.1'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.1'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description:
79
+ email:
80
+ - mark@without-brains.net
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .pryrc
87
+ - .rspec
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - Vagrantfile
93
+ - config/mongoid.yml.default
94
+ - employer-mongoid.gemspec
95
+ - lib/employer-mongoid.rb
96
+ - lib/employer-mongoid/employee.rb
97
+ - lib/employer-mongoid/job.rb
98
+ - lib/employer-mongoid/pipeline.rb
99
+ - lib/employer-mongoid/version.rb
100
+ - script/vagrant-keep-agent-forwarding
101
+ - script/vagrant-provision
102
+ - spec/employer-mongoid/job_spec.rb
103
+ - spec/employer-mongoid/pipeline_spec.rb
104
+ - spec/spec_helper.rb
105
+ homepage: https://github.com/mkremer/employer-mongoid
106
+ licenses:
107
+ - MIT
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 1.8.23
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: Mongoid backend for Employer
130
+ test_files:
131
+ - spec/employer-mongoid/job_spec.rb
132
+ - spec/employer-mongoid/pipeline_spec.rb
133
+ - spec/spec_helper.rb