asyncapi-server 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +9 -0
- data/README.md +83 -0
- data/Rakefile +33 -0
- data/app/controllers/asyncapi/server/v1/jobs_controller.rb +34 -0
- data/app/models/asyncapi/server/job.rb +14 -0
- data/app/serializers/asyncapi/server/job_serializer.rb +10 -0
- data/app/workers/asyncapi/server/job_worker.rb +41 -0
- data/config/database.yml +25 -0
- data/config/routes.rb +7 -0
- data/db/migrate/20141112034324_create_asyncapi_server_jobs.rb +10 -0
- data/db/migrate/20141212064931_add_secret_to_asyncapi_server_job.rb +5 -0
- data/db/migrate/20150130062520_add_expired_at_to_asyncapi_server_job.rb +11 -0
- data/db/migrate/20150201231018_drop_expired_at_from_asyncapi_server_jobs.rb +9 -0
- data/lib/asyncapi-server.rb +1 -0
- data/lib/asyncapi/server.rb +16 -0
- data/lib/asyncapi/server/engine.rb +23 -0
- data/lib/asyncapi/server/rails_ext/controller.rb +32 -0
- data/lib/asyncapi/server/rspec.rb +19 -0
- data/lib/asyncapi/server/version.rb +5 -0
- data/lib/asyncapi_server/engine.rb +1 -0
- data/lib/generators/asyncapi/server/config_generator.rb +17 -0
- data/lib/tasks/asyncapi_server_tasks.rake +4 -0
- data/spec/controllers/asyncapi/server/v1/jobs_controller_spec.rb +51 -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 +5 -0
- data/spec/dummy/app/controllers/tests_controller.rb +5 -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/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +29 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +78 -0
- data/spec/dummy/config/environments/test.rb +39 -0
- data/spec/dummy/config/initializers/assets.rb +8 -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/default_url_options.rb +2 -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 +59 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/db/migrate/20141212065005_create_asyncapi_server_jobs.asyncapi_server.rb +11 -0
- data/spec/dummy/db/migrate/20141212065006_add_secret_to_asyncapi_server_job.asyncapi_server.rb +6 -0
- data/spec/dummy/db/migrate/20150130062901_add_expired_at_to_asyncapi_server_job.asyncapi_server.rb +12 -0
- data/spec/dummy/db/migrate/20150201231329_drop_expired_at_from_asyncapi_server_jobs.asyncapi_server.rb +10 -0
- data/spec/dummy/db/schema.rb +24 -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/job.rb +7 -0
- data/spec/fixtures/runner.rb +6 -0
- data/spec/models/job_spec.rb +44 -0
- data/spec/requests/enqueueing_jobs_spec.rb +33 -0
- data/spec/serializers/job_serializer_spec.rb +18 -0
- data/spec/server_spec.rb +22 -0
- data/spec/spec_helper.rb +40 -0
- data/spec/support/factory_girl.rb +3 -0
- data/spec/support/indifferent_hash.rb +8 -0
- data/spec/workers/job_worker_spec.rb +80 -0
- metadata +354 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dc1e5729a68ac1d07a41e52ce742a35dc36ff5be
|
4
|
+
data.tar.gz: 4bc45e530b4b9f0ff4bbc6ba515a6bd753aafd00
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9631bfcc20b0d4e3327caab6872caa4288f16c7909f3eb9b3075315c2b1a0d649d024dbf362acb3a6fe894a6166dce8235101f9c02b8acdc4bbf5cb0a2d29f25
|
7
|
+
data.tar.gz: e6a08f0a83bbd0ab17d81efc472259634dbf485cbb2b4f65435b3311d5b65c5a6e23d6f89683dc61763571f2ea23e82c8647bf04274ad8f676f59c7f3c88b510
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
Copyright 2015 G5
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# Asyncapi::Server
|
2
|
+
|
3
|
+
`Asyncapi::Server` is a Rails engine that allows asynchronous responses for API calls made by [Asyncapi::Client](https://github.com/G5/asyncapi-client).
|
4
|
+
|
5
|
+
# Installation
|
6
|
+
|
7
|
+
Add `asyncapi-server` into your Gemfile, `bundle install`, then run the following commands:
|
8
|
+
|
9
|
+
```bash
|
10
|
+
rails g asyncapi:server:config
|
11
|
+
rake asyncapi_server:install:migrations
|
12
|
+
rake db:migrate
|
13
|
+
```
|
14
|
+
|
15
|
+
Make sure you have `:host` in the `default_url_options` set up for your server. Example `config/initializers/default_url_options.rb`:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
Rails.application.routes.default_url_options ||= {}
|
19
|
+
Rails.application.routes.default_url_options[:host] = ENV["HOST"]
|
20
|
+
```
|
21
|
+
|
22
|
+
# Usage
|
23
|
+
|
24
|
+
In your controller where you want a `create` action:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
# some_controller.rb
|
28
|
+
class SomeController < ApplicationController
|
29
|
+
async :create, CreateSomething
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Create the `CreateSomething` runner class that will be called in the background:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
# create_something.rb
|
37
|
+
class CreateSomething
|
38
|
+
def self.call(params)
|
39
|
+
something = Something.new(params)
|
40
|
+
|
41
|
+
# the last line in this method is sent back to Asyncapi::Client as the `message`
|
42
|
+
if something.save
|
43
|
+
something.to_json
|
44
|
+
else
|
45
|
+
something.errors.messages
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
If you use `protected_attributes`, in an initializer:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
Asyncapi::Server::Job.attr_accessible :status, :callback_url, :class_name, :params, :secret
|
55
|
+
```
|
56
|
+
|
57
|
+
### RSpec
|
58
|
+
|
59
|
+
If you want to create an integration spec for you Asyncapi server endpoint, make sure you require the helper:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
require "asyncapi/server/rspec"
|
63
|
+
```
|
64
|
+
|
65
|
+
When you make a request, instead of `post`, use `asyncapi_post`. Ex:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
asyncapi_post("/api/v1/long_running_job", name: "Compute")
|
69
|
+
```
|
70
|
+
|
71
|
+
This helper calls `post` underneath but builds the request in a way that Asyncapi server understands.
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
Copyright (c) 2015 G5
|
76
|
+
|
77
|
+
MIT License
|
78
|
+
|
79
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
80
|
+
|
81
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
82
|
+
|
83
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
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 = 'AsyncapiServer'
|
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
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
|
25
|
+
Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
|
26
|
+
|
27
|
+
require "rspec/core"
|
28
|
+
require "rspec/core/rake_task"
|
29
|
+
|
30
|
+
desc "Run all specs in spec directory (excluding plugin specs)"
|
31
|
+
RSpec::Core::RakeTask.new(:spec => "app:db:test:prepare")
|
32
|
+
|
33
|
+
task :default => :spec
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Asyncapi
|
2
|
+
module Server
|
3
|
+
module V1
|
4
|
+
class JobsController < ApplicationController # TODO: Asyncapi::Server.parent_controller
|
5
|
+
|
6
|
+
protect_from_forgery with: :null_session
|
7
|
+
respond_to :json
|
8
|
+
|
9
|
+
def index
|
10
|
+
jobs = paginate Job.all
|
11
|
+
serializer = ActiveModel::ArraySerializer.new(jobs)
|
12
|
+
render json: serializer
|
13
|
+
end
|
14
|
+
|
15
|
+
def show
|
16
|
+
job = Job.find(params[:id])
|
17
|
+
job = JobSerializer.new(job)
|
18
|
+
render json: job
|
19
|
+
end
|
20
|
+
|
21
|
+
def destroy
|
22
|
+
job = Job.find_by(id: params[:id], secret: params[:secret])
|
23
|
+
if job
|
24
|
+
job.destroy
|
25
|
+
respond_with job
|
26
|
+
else
|
27
|
+
render nothing: true, status: 404
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Asyncapi::Server
|
2
|
+
class JobWorker
|
3
|
+
|
4
|
+
include Sidekiq::Worker
|
5
|
+
sidekiq_options retry: false
|
6
|
+
|
7
|
+
def perform(job_id)
|
8
|
+
job = Job.find(job_id)
|
9
|
+
runner_class = job.class_name.constantize
|
10
|
+
|
11
|
+
job_status = :success
|
12
|
+
job_message = runner_class.call(job.params)
|
13
|
+
rescue => e
|
14
|
+
job_status = :error
|
15
|
+
job_message = [e.message, e.backtrace].flatten.join("\n")
|
16
|
+
ensure
|
17
|
+
job.update_attributes(status: job_status)
|
18
|
+
report_job_status(job, job_message)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def report_job_status(job, job_message)
|
24
|
+
Typhoeus.put(
|
25
|
+
job.callback_url,
|
26
|
+
body: {
|
27
|
+
job: {
|
28
|
+
status: job.status,
|
29
|
+
message: job_message,
|
30
|
+
secret: job.secret,
|
31
|
+
}
|
32
|
+
}.to_json,
|
33
|
+
headers: {
|
34
|
+
"Content-Type" => "application/json",
|
35
|
+
Accept: "application/json"
|
36
|
+
}
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
data/config/database.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# SQLite version 3.x
|
2
|
+
# gem install sqlite3
|
3
|
+
#
|
4
|
+
# Ensure the SQLite 3 gem is defined in your Gemfile
|
5
|
+
# gem 'sqlite3'
|
6
|
+
#
|
7
|
+
default: &default
|
8
|
+
adapter: sqlite3
|
9
|
+
pool: 5
|
10
|
+
timeout: 5000
|
11
|
+
|
12
|
+
development:
|
13
|
+
<<: *default
|
14
|
+
database: db/development.sqlite3
|
15
|
+
|
16
|
+
# Warning: The database defined as "test" will be erased and
|
17
|
+
# re-generated from your development database when you run "rake".
|
18
|
+
# Do not set this db to the same as development or production.
|
19
|
+
test:
|
20
|
+
<<: *default
|
21
|
+
database: db/test.sqlite3
|
22
|
+
|
23
|
+
production:
|
24
|
+
<<: *default
|
25
|
+
database: db/production.sqlite3
|
data/config/routes.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
class AddExpiredAtToAsyncapiServerJob < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
add_column :asyncapi_server_jobs, :expired_at, :datetime
|
4
|
+
|
5
|
+
Asyncapi::Server::Job.reset_column_information
|
6
|
+
|
7
|
+
Asyncapi::Server::Job.
|
8
|
+
where(Asyncapi::Server::Job.arel_table[:expired_at].eq(nil)).
|
9
|
+
update_all(expired_at: Asyncapi::Server.expiry_threshold.from_now)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "asyncapi/server"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "api-pagination"
|
2
|
+
require "kaminari"
|
3
|
+
require "typhoeus"
|
4
|
+
require "sidekiq"
|
5
|
+
require "asyncapi/server/rails_ext/controller"
|
6
|
+
require "active_model/serializer"
|
7
|
+
|
8
|
+
module Asyncapi
|
9
|
+
module Server
|
10
|
+
class Engine < ::Rails::Engine
|
11
|
+
isolate_namespace Asyncapi::Server
|
12
|
+
engine_name "asyncapi_server"
|
13
|
+
|
14
|
+
config.to_prepare do
|
15
|
+
::ApplicationController.send :include, RailsExt::Controller
|
16
|
+
|
17
|
+
Engine.routes.default_url_options =
|
18
|
+
Rails.application.routes.default_url_options
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Asyncapi
|
2
|
+
module Server
|
3
|
+
module RailsExt
|
4
|
+
module Controller
|
5
|
+
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def async(method_name, klass)
|
10
|
+
define_method(method_name) do
|
11
|
+
job = Job.create(job_params_with(klass.name))
|
12
|
+
serializer = JobSerializer.new(job)
|
13
|
+
JobWorker.perform_async(job.id)
|
14
|
+
render json: serializer
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def job_params_with(class_name)
|
20
|
+
params[:job].merge!(class_name: class_name)
|
21
|
+
params.require(:job).permit(
|
22
|
+
:callback_url,
|
23
|
+
:class_name,
|
24
|
+
:params,
|
25
|
+
:secret,
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Asyncapi
|
2
|
+
module Server
|
3
|
+
module RSpec
|
4
|
+
|
5
|
+
def asyncapi_post(url, params)
|
6
|
+
post(url, {
|
7
|
+
job: {
|
8
|
+
callback_url: "callback_url",
|
9
|
+
params: params,
|
10
|
+
secret: "sekret",
|
11
|
+
}
|
12
|
+
})
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
RSpec.configuration.include Asyncapi::Server::RSpec, type: :request
|
@@ -0,0 +1 @@
|
|
1
|
+
require "asyncapi/server/engine"
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "asyncapi/server/engine"
|
2
|
+
|
3
|
+
module Asyncapi::Server
|
4
|
+
class ConfigGenerator < Rails::Generators::Base
|
5
|
+
|
6
|
+
desc "Inserts routes code for Asyncapi::Server"
|
7
|
+
|
8
|
+
def mount_on_routes
|
9
|
+
inject_into_file(
|
10
|
+
"config/routes.rb",
|
11
|
+
%Q( mount Asyncapi::Server::Engine, at: "/asyncapi/server"\n),
|
12
|
+
before: /^end/
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|