async_job 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jimmy Thrasher
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = async_job
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to async_job
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Jimmy Thrasher. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,3 @@
1
+ require 'async_job/results'
2
+ require 'async_job/job'
3
+ require 'async_job/controller_methods'
@@ -0,0 +1,31 @@
1
+ module AsyncJob
2
+ module ControllerMethods
3
+ module Client
4
+ def render_job_queued(results, options = {})
5
+ response['Location'] = make_results_url(results)
6
+ response['Retry-Interval'] = options[:retry_interval].to_s || '1.0'
7
+ head 202
8
+ end
9
+ end
10
+
11
+ def show
12
+ results = AsyncJob::Results.find(params[:id])
13
+
14
+ if results.results
15
+ logger.debug "Returning async job results: #{results.inspect}"
16
+ if results.success?
17
+ if stale?(:etag => results.results)
18
+ render :json => results.results, :status => :ok
19
+ end
20
+ else
21
+ render :json => results.results, :status => :unprocessable_entity
22
+ end
23
+ results.destroy
24
+ else
25
+ render_job_queued(results)
26
+ end
27
+ end
28
+
29
+ include Client
30
+ end
31
+ end
@@ -0,0 +1,30 @@
1
+ module AsyncJob
2
+ class Job < Delayed::PerformableMethod
3
+ def self.perform_async(user, object, method, *args)
4
+ AsyncJob::Results.create.tap do |results|
5
+ Delayed::Job.enqueue(self.new(object, method, args, results.id))
6
+ end
7
+ end
8
+
9
+ def initialize(object, method, args, results_id)
10
+ super(object, method, args)
11
+ @results_id = results_id
12
+ end
13
+
14
+ def perform
15
+ job_results = AsyncJob::Results.find(@results_id)
16
+
17
+ begin
18
+ results = super
19
+ job_results.success = true
20
+ job_results.results = results
21
+ rescue => error
22
+ job_results.success = false
23
+ job_results.results = [error.message, error.backtrace]
24
+ Rails.logger.error "Error processing async job: #{error.message}\n#{error.backtrace.join("\n")}"
25
+ end
26
+
27
+ job_results.save
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_record'
2
+
3
+ module AsyncJob
4
+ class Results < ActiveRecord::Base
5
+ set_table_name "async_job_results"
6
+
7
+ serialize :results
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class AsyncJobMigrationGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ def self.source_root
8
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
9
+ end
10
+
11
+ # Implement the required interface for Rails::Generators::Migration.
12
+ #
13
+ def self.next_migration_number(dirname) #:nodoc:
14
+ next_migration_number = current_migration_number(dirname) + 1
15
+ if ActiveRecord::Base.timestamped_migrations
16
+ [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
17
+ else
18
+ "%.3d" % next_migration_number
19
+ end
20
+ end
21
+
22
+ def create_migration_file
23
+ if defined?(ActiveRecord)
24
+ migration_template 'migration.rb', 'db/migrate/create_async_job_results.rb'
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ class CreateAsyncJobResults < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :async_job_results, :force => true do |t|
4
+ t.boolean :success
5
+ t.text :results
6
+ t.timestamps
7
+ end
8
+
9
+ add_index :async_job_results, [:id], :name => 'async_job_results_id'
10
+ end
11
+
12
+ def self.down
13
+ drop_table :async_job_results
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'async_job'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestAsyncJob < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: async_job
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Jimmy Thrasher
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-14 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activerecord
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: shoulda
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: bundler
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.0
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: jeweler
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: 1.5.2
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: rcov
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: activerecord
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: *id006
82
+ description: ""
83
+ email: jimmy@jimmythrasher.com
84
+ executables: []
85
+
86
+ extensions: []
87
+
88
+ extra_rdoc_files:
89
+ - LICENSE.txt
90
+ - README.rdoc
91
+ files:
92
+ - lib/async_job.rb
93
+ - lib/async_job/controller_methods.rb
94
+ - lib/async_job/job.rb
95
+ - lib/async_job/results.rb
96
+ - lib/generators/async_job_migration/async_job_migration_generator.rb
97
+ - lib/generators/async_job_migration/templates/migration.rb
98
+ - LICENSE.txt
99
+ - README.rdoc
100
+ - test/helper.rb
101
+ - test/test_async_job.rb
102
+ has_rdoc: true
103
+ homepage: http://github.com/jjthrash/async_job
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: -4254545143270447403
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: "0"
126
+ requirements: []
127
+
128
+ rubyforge_project:
129
+ rubygems_version: 1.6.1
130
+ signing_key:
131
+ specification_version: 3
132
+ summary: Simple asynchronous jobs for Rails
133
+ test_files:
134
+ - test/helper.rb
135
+ - test/test_async_job.rb