async_request 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Rakefile +37 -0
- data/app/assets/javascripts/async_request/application.js +13 -0
- data/app/assets/stylesheets/async_request/application.css +15 -0
- data/app/controllers/async_request/application_controller.rb +4 -0
- data/app/controllers/async_request/jobs_controller.rb +19 -0
- data/app/helpers/async_request/application_helper.rb +15 -0
- data/app/models/async_request/job.rb +6 -0
- data/app/views/layouts/async_request/application.html.erb +14 -0
- data/app/workers/async_request/job_processor.rb +16 -0
- data/config/routes.rb +3 -0
- data/db/migrate/20160411190611_create_async_request_jobs.rb +16 -0
- data/lib/async_request.rb +4 -0
- data/lib/async_request/engine.rb +23 -0
- data/lib/async_request/version.rb +3 -0
- data/lib/tasks/async_request_tasks.rake +4 -0
- data/spec/controllers/async_request/jobs_controller_spec.rb +47 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +10 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/workers/test.rb +7 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +26 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +19 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +41 -0
- data/spec/dummy/config/environments/production.rb +79 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +4 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/db/schema.rb +33 -0
- data/spec/dummy/log/development.log +827 -0
- data/spec/dummy/log/test.log +5076 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/factories/async_request_job.rb +23 -0
- data/spec/helpers/async_request/application_helper_spec.rb +39 -0
- data/spec/spec_helper.rb +68 -0
- data/spec/support/parsed_response_helper.rb +7 -0
- data/spec/workers/async_request/job_processor_spec.rb +32 -0
- metadata +293 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cdb61fdcbf0c00605261d249aa526c7b62cc5937
|
4
|
+
data.tar.gz: aba340ec4b640116ff853c007ac7ab2c92e2b7c5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: befccc1cecbbd1d08e04d1899f5e494febe13675e6b91266e2ee8bca2ddf9aff520277914d88235c7a68e75f56b62310c0cb13bfd6f69022aa09dee191d4a6d4
|
7
|
+
data.tar.gz: a8d8ae4c2d99e4d9ae078868390bca2320d4e9b18f89fb1d09f9285dfca070d2710271dfee303546b67c2fdafbe9b54735d761a73c9853e6ff3f9d03404628c8
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'AsyncRequest'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
Bundler::GemHelper.install_tasks
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
|
29
|
+
Rake::TestTask.new(:test) do |t|
|
30
|
+
t.libs << 'lib'
|
31
|
+
t.libs << 'spec'
|
32
|
+
t.pattern = 'spec/**/*_spec.rb'
|
33
|
+
t.verbose = false
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
task default: :test
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module AsyncRequest
|
2
|
+
class JobsController < ActionController::Base
|
3
|
+
def show
|
4
|
+
job = Job.find_by(uid: params[:id])
|
5
|
+
return head :not_found unless job.present?
|
6
|
+
job.processed? ? render_finished_job(job) : render_pending(job)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def render_pending(job)
|
12
|
+
render json: { status: job.status }, status: :accepted
|
13
|
+
end
|
14
|
+
|
15
|
+
def render_finished_job(job)
|
16
|
+
render json: JSON.parse(job.response), status: job.status_code
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module AsyncRequest
|
2
|
+
module ApplicationHelper
|
3
|
+
def execute_async(worker_class, *params)
|
4
|
+
raise ArgumentError if worker_class.nil?
|
5
|
+
job = Job.create(
|
6
|
+
worker: worker_class,
|
7
|
+
params: params,
|
8
|
+
status: Job.statuses[:waiting],
|
9
|
+
uid: SecureRandom.uuid
|
10
|
+
)
|
11
|
+
JobProcessor.perform_async(job.id)
|
12
|
+
job.uid
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>AsyncRequest</title>
|
5
|
+
<%= stylesheet_link_tag "async_request/application", media: "all" %>
|
6
|
+
<%= javascript_include_tag "async_request/application" %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module AsyncRequest
|
2
|
+
class JobProcessor
|
3
|
+
include Sidekiq::Worker
|
4
|
+
|
5
|
+
def perform(id)
|
6
|
+
job = Job.find(id)
|
7
|
+
job.processing!
|
8
|
+
status, response = job.worker.constantize.new.execute(*job.params)
|
9
|
+
job.update_attributes!(
|
10
|
+
status: Job.statuses[:processed],
|
11
|
+
status_code: status,
|
12
|
+
response: response.to_json
|
13
|
+
)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/config/routes.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
class CreateAsyncRequestJobs < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :async_request_jobs do |t|
|
4
|
+
t.string :worker
|
5
|
+
t.integer :status
|
6
|
+
t.integer :status_code
|
7
|
+
t.text :response
|
8
|
+
t.string :uid
|
9
|
+
t.text :params
|
10
|
+
|
11
|
+
t.timestamps null: false
|
12
|
+
end
|
13
|
+
add_index :async_request_jobs, :status
|
14
|
+
add_index :async_request_jobs, :uid, unique: true
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'sidekiq'
|
2
|
+
module AsyncRequest
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
isolate_namespace AsyncRequest
|
5
|
+
|
6
|
+
initializer "async_request", before: :load_config_initializers do |app|
|
7
|
+
Rails.application.routes.append do
|
8
|
+
mount AsyncRequest::Engine, at: "/async_request"
|
9
|
+
end
|
10
|
+
|
11
|
+
unless app.root.to_s.match root.to_s
|
12
|
+
config.paths["db/migrate"].expanded.each do |expanded_path|
|
13
|
+
Rails.application.config.paths["db/migrate"] << expanded_path
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
config.generators do |g|
|
19
|
+
g.test_framework :rspec
|
20
|
+
g.fixture_replacement :factory_girl, dir: 'spec/factories'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AsyncRequest
|
4
|
+
describe JobsController do
|
5
|
+
routes { AsyncRequest::Engine.routes }
|
6
|
+
|
7
|
+
describe '#show' do
|
8
|
+
context 'when there is no job with the given id' do
|
9
|
+
let!(:job) { FactoryGirl.create(:async_request_job, :waiting) }
|
10
|
+
it 'returns 404' do
|
11
|
+
get :show, id: job.uid + 'ABC'
|
12
|
+
expect(response.status).to eq(404)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when the job exists but it is in a waiting status' do
|
17
|
+
let!(:job) { FactoryGirl.create(:async_request_job, :waiting) }
|
18
|
+
it 'returns 202' do
|
19
|
+
get :show, id: job.uid
|
20
|
+
expect(response.status).to eq(202)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when the job exists but it is in a processing status' do
|
25
|
+
let!(:job) { FactoryGirl.create(:async_request_job, :processing) }
|
26
|
+
it 'returns 202' do
|
27
|
+
get :show, id: job.uid
|
28
|
+
expect(response.status).to eq(202)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when the job exists and has finished' do
|
33
|
+
let!(:job) { FactoryGirl.create(:async_request_job, :processed) }
|
34
|
+
|
35
|
+
it 'returns the saved status code' do
|
36
|
+
get :show, id: job.uid
|
37
|
+
expect(response.status).to eq(job.status_code)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'returns the saved status code' do
|
41
|
+
get :show, id: job.uid
|
42
|
+
expect(response_body).to eq(JSON.parse(job.response))
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/spec/dummy/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class ApplicationController < ActionController::Base
|
2
|
+
# Prevent CSRF attacks by raising an exception.
|
3
|
+
# For APIs, you may want to use :null_session instead.
|
4
|
+
protect_from_forgery with: :exception
|
5
|
+
|
6
|
+
def async
|
7
|
+
id = execute_async(Test, 'a')
|
8
|
+
render json: { id: id, url: async_request.job_url(id) }, status: 202
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
6
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/spec/dummy/bin/rake
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
# path to your application root.
|
5
|
+
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
|
6
|
+
|
7
|
+
Dir.chdir APP_ROOT do
|
8
|
+
# This script is a starting point to setup your application.
|
9
|
+
# Add necessary setup steps to this file:
|
10
|
+
|
11
|
+
puts "== Installing dependencies =="
|
12
|
+
system "gem install bundler --conservative"
|
13
|
+
system "bundle check || bundle install"
|
14
|
+
|
15
|
+
# puts "\n== Copying sample files =="
|
16
|
+
# unless File.exist?("config/database.yml")
|
17
|
+
# system "cp config/database.yml.sample config/database.yml"
|
18
|
+
# end
|
19
|
+
|
20
|
+
puts "\n== Preparing database =="
|
21
|
+
system "bin/rake db:setup"
|
22
|
+
|
23
|
+
puts "\n== Removing old logs and tempfiles =="
|
24
|
+
system "rm -f log/*"
|
25
|
+
system "rm -rf tmp/cache"
|
26
|
+
|
27
|
+
puts "\n== Restarting application server =="
|
28
|
+
system "touch tmp/restart.txt"
|
29
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
require 'rails/all'
|
4
|
+
|
5
|
+
Bundler.require(*Rails.groups)
|
6
|
+
require "async_request"
|
7
|
+
|
8
|
+
module Dummy
|
9
|
+
class Application < Rails::Application
|
10
|
+
# Settings in config/environments/* take precedence over those specified here.
|
11
|
+
# Application configuration should go into files in config/initializers
|
12
|
+
# -- all .rb files in that directory are automatically loaded.
|
13
|
+
|
14
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
15
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
16
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
17
|
+
|
18
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
19
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
20
|
+
# config.i18n.default_locale = :de
|
21
|
+
|
22
|
+
# Do not swallow errors in after_commit/after_rollback callbacks.
|
23
|
+
config.active_record.raise_in_transactional_callbacks = true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|