startback-jobs 0.14.0 → 0.14.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -2
- data/lib/startback/ext/support/operation_runner.rb +17 -0
- data/lib/startback/ext/web/api.rb +12 -0
- data/lib/startback/ext.rb +2 -2
- data/lib/startback/jobs/agent.rb +15 -0
- data/lib/startback/jobs/api.rb +17 -0
- data/lib/startback/jobs/event/job_created.rb +8 -0
- data/lib/startback/jobs/event/job_ran.rb +8 -0
- data/lib/startback/jobs/event.rb +8 -0
- data/lib/startback/jobs/model/job.rb +35 -0
- data/lib/startback/jobs/model.rb +12 -0
- data/lib/startback/jobs/operation/create_job.rb +37 -0
- data/lib/startback/jobs/operation/run_job.rb +36 -0
- data/lib/startback/jobs/operation.rb +8 -0
- data/lib/startback/jobs/services.rb +25 -0
- data/lib/startback/jobs/support/job_result/embedded.rb +19 -0
- data/lib/startback/jobs/support/job_result/not_ready.rb +15 -0
- data/lib/startback/jobs/support/job_result/redirect.rb +32 -0
- data/lib/startback/jobs/support/job_result.rb +31 -0
- data/lib/startback/jobs/support.rb +1 -0
- data/lib/startback/jobs.fio +44 -0
- data/lib/startback/jobs.rb +23 -0
- data/spec/spec_helper.rb +26 -33
- data/spec/unit/api/test_job_result.rb +121 -0
- data/spec/unit/model/test_job.rb +23 -0
- data/spec/unit/operation/test_create_job.rb +42 -0
- data/spec/unit/operation/test_run_job.rb +37 -0
- data/spec/unit/test_finitio_schema.rb +20 -0
- data/tasks/test.rake +0 -1
- metadata +29 -92
- data/README.md +0 -13
- data/lib/startback/audit/prometheus.rb +0 -87
- data/lib/startback/audit/shared.rb +0 -17
- data/lib/startback/audit/trailer.rb +0 -129
- data/lib/startback/audit.rb +0 -3
- data/lib/startback/caching/entity_cache.rb +0 -157
- data/lib/startback/caching/no_store.rb +0 -28
- data/lib/startback/caching/store.rb +0 -34
- data/lib/startback/context/h_factory.rb +0 -43
- data/lib/startback/context/middleware.rb +0 -53
- data/lib/startback/context.rb +0 -122
- data/lib/startback/errors.rb +0 -197
- data/lib/startback/event/agent.rb +0 -84
- data/lib/startback/event/bus/bunny/async.rb +0 -162
- data/lib/startback/event/bus/bunny.rb +0 -1
- data/lib/startback/event/bus/memory/async.rb +0 -45
- data/lib/startback/event/bus/memory/sync.rb +0 -35
- data/lib/startback/event/bus/memory.rb +0 -2
- data/lib/startback/event/bus.rb +0 -100
- data/lib/startback/event/engine.rb +0 -94
- data/lib/startback/event/ext/context.rb +0 -5
- data/lib/startback/event/ext/operation.rb +0 -13
- data/lib/startback/event.rb +0 -47
- data/lib/startback/ext/date_time.rb +0 -9
- data/lib/startback/ext/time.rb +0 -9
- data/lib/startback/model.rb +0 -6
- data/lib/startback/operation/error_operation.rb +0 -19
- data/lib/startback/operation/multi_operation.rb +0 -28
- data/lib/startback/operation.rb +0 -78
- data/lib/startback/services.rb +0 -11
- data/lib/startback/support/data_object.rb +0 -71
- data/lib/startback/support/env.rb +0 -41
- data/lib/startback/support/fake_logger.rb +0 -18
- data/lib/startback/support/hooks.rb +0 -48
- data/lib/startback/support/log_formatter.rb +0 -34
- data/lib/startback/support/logger.rb +0 -34
- data/lib/startback/support/operation_runner.rb +0 -150
- data/lib/startback/support/robustness.rb +0 -157
- data/lib/startback/support/transaction_manager.rb +0 -25
- data/lib/startback/support/transaction_policy.rb +0 -33
- data/lib/startback/support/world.rb +0 -54
- data/lib/startback/support.rb +0 -26
- data/lib/startback/version.rb +0 -8
- data/lib/startback/web/api.rb +0 -99
- data/lib/startback/web/auto_caching.rb +0 -85
- data/lib/startback/web/catch_all.rb +0 -52
- data/lib/startback/web/cors_headers.rb +0 -80
- data/lib/startback/web/health_check.rb +0 -49
- data/lib/startback/web/magic_assets/ng_html_transformer.rb +0 -80
- data/lib/startback/web/magic_assets/rake_tasks.rb +0 -64
- data/lib/startback/web/magic_assets.rb +0 -98
- data/lib/startback/web/middleware.rb +0 -13
- data/lib/startback/web/prometheus.rb +0 -16
- data/lib/startback/web/shield.rb +0 -58
- data/lib/startback.rb +0 -43
- data/spec/unit/audit/test_prometheus.rb +0 -72
- data/spec/unit/audit/test_trailer.rb +0 -105
- data/spec/unit/caching/test_entity_cache.rb +0 -136
- data/spec/unit/context/test_abstraction_factory.rb +0 -64
- data/spec/unit/context/test_dup.rb +0 -42
- data/spec/unit/context/test_fork.rb +0 -37
- data/spec/unit/context/test_h_factory.rb +0 -31
- data/spec/unit/context/test_middleware.rb +0 -45
- data/spec/unit/context/test_with_world.rb +0 -20
- data/spec/unit/context/test_world.rb +0 -17
- data/spec/unit/event/bus/memory/test_async.rb +0 -43
- data/spec/unit/event/bus/memory/test_sync.rb +0 -43
- data/spec/unit/support/hooks/test_after_hook.rb +0 -54
- data/spec/unit/support/hooks/test_before_hook.rb +0 -54
- data/spec/unit/support/operation_runner/test_around_run.rb +0 -156
- data/spec/unit/support/operation_runner/test_before_after_call.rb +0 -48
- data/spec/unit/support/test_data_object.rb +0 -156
- data/spec/unit/support/test_env.rb +0 -75
- data/spec/unit/support/test_robusteness.rb +0 -229
- data/spec/unit/support/test_transaction_manager.rb +0 -64
- data/spec/unit/support/test_world.rb +0 -72
- data/spec/unit/test_event.rb +0 -62
- data/spec/unit/test_operation.rb +0 -55
- data/spec/unit/test_support.rb +0 -40
- data/spec/unit/web/fixtures/assets/app/hello.es6 +0 -4
- data/spec/unit/web/fixtures/assets/app/hello.html +0 -1
- data/spec/unit/web/fixtures/assets/index.es6 +0 -1
- data/spec/unit/web/test_api.rb +0 -82
- data/spec/unit/web/test_auto_caching.rb +0 -81
- data/spec/unit/web/test_catch_all.rb +0 -77
- data/spec/unit/web/test_cors_headers.rb +0 -88
- data/spec/unit/web/test_healthcheck.rb +0 -59
- data/spec/unit/web/test_magic_assets.rb +0 -82
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2984bf7dac7e3cdaeeb25f154f70c1424e134beabd34223d1615719ad74c4f9b
|
4
|
+
data.tar.gz: 33dbc99dad7d665d00c3a34c4d0c343d515315b69a36a61e4859a1b599771dba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5ad084bbde5e706ec7af51df04282036ce186fef8d6492ac9ce8d657a73dfaf2f09744317516e41c97a7d4fe09b8876b92892a0c4bb3bb98094bd6d8b3cd0aa
|
7
|
+
data.tar.gz: fc9b8b8d46f0bf9433996d67e52a707f76617ceebe84da6777e00592183dbc1e1f6ca13c861b2241561b8d4b5c33b229efc271197c85c58c7ae753b1caa5ef6f
|
data/Gemfile
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Startback
|
2
|
+
module Support
|
3
|
+
module OperationRunner
|
4
|
+
|
5
|
+
def run_as_job(op)
|
6
|
+
run Startback::Jobs::CreateJob.new({
|
7
|
+
isReady: false,
|
8
|
+
opClass: op.class.name,
|
9
|
+
opInput: op.input,
|
10
|
+
opContext: context.to_h,
|
11
|
+
createdBy: '',
|
12
|
+
})
|
13
|
+
end
|
14
|
+
|
15
|
+
end # module OperationRunner
|
16
|
+
end # module Support
|
17
|
+
end # module Startback
|
data/lib/startback/ext.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
require_relative 'ext/
|
2
|
-
require_relative 'ext/
|
1
|
+
require_relative 'ext/web/api'
|
2
|
+
require_relative 'ext/support/operation_runner'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Startback
|
2
|
+
module Jobs
|
3
|
+
class Agent < Startback::Event::Agent
|
4
|
+
|
5
|
+
def install_listeners
|
6
|
+
async Event::JobCreated, 'job-runner'
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(event)
|
10
|
+
run RunJob.new(event.data.to_h)
|
11
|
+
end
|
12
|
+
|
13
|
+
end # class Agent
|
14
|
+
end # module Jobs
|
15
|
+
end # module Startback
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Startback
|
2
|
+
module Jobs
|
3
|
+
class Api < Startback::Web::Api
|
4
|
+
|
5
|
+
get %r{/([^\/]+)/?} do |id|
|
6
|
+
job = context.factor(Services).get_job!(id: id)
|
7
|
+
serve 'Job', job
|
8
|
+
end
|
9
|
+
|
10
|
+
get %r{/([^\/]+)/result/?} do |id|
|
11
|
+
job = context.factor(Services).get_job!(id: id)
|
12
|
+
Support::JobResult.for(job).api_serve(self)
|
13
|
+
end
|
14
|
+
|
15
|
+
end # class Api
|
16
|
+
end # module Jobs
|
17
|
+
end # module Startback
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Startback
|
2
|
+
module Jobs
|
3
|
+
class Model
|
4
|
+
class Job < Model
|
5
|
+
def self.ref(data)
|
6
|
+
dress(data, 'Job.Ref')
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.full(data)
|
10
|
+
dress(data, 'Job.Full')
|
11
|
+
end
|
12
|
+
|
13
|
+
def ready?
|
14
|
+
self.isReady
|
15
|
+
end
|
16
|
+
|
17
|
+
def not_ready?
|
18
|
+
!ready?
|
19
|
+
end
|
20
|
+
|
21
|
+
def expired?
|
22
|
+
self.expiredAt && self.expiredAt < Time.now
|
23
|
+
end
|
24
|
+
|
25
|
+
def fully_consumed?
|
26
|
+
self.consumedMax && (self.consumedCount || 0 >= self.consumedMax)
|
27
|
+
end
|
28
|
+
|
29
|
+
def result
|
30
|
+
Support::JobResult.for(self)
|
31
|
+
end
|
32
|
+
end # class Job
|
33
|
+
end # class Model
|
34
|
+
end # module Jobs
|
35
|
+
end # module Startback
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Startback
|
2
|
+
module Jobs
|
3
|
+
class CreateJob < Operation
|
4
|
+
|
5
|
+
def initialize(input)
|
6
|
+
super(System['Job.CreationRequest'].dress(input))
|
7
|
+
end
|
8
|
+
|
9
|
+
def call
|
10
|
+
@job = Model::Job.full({
|
11
|
+
id: SecureRandom.urlsafe_base64(16),
|
12
|
+
opInput: {},
|
13
|
+
opContext: {},
|
14
|
+
opResult: nil,
|
15
|
+
strategy: 'NotReady',
|
16
|
+
strategyOptions: {},
|
17
|
+
expiresAt: nil,
|
18
|
+
refreshFreq: nil,
|
19
|
+
refreshedAt: nil,
|
20
|
+
consumeMax: nil,
|
21
|
+
consumeCount: 0,
|
22
|
+
createdAt: Time.now,
|
23
|
+
createdBy: nil,
|
24
|
+
}.merge(input))
|
25
|
+
|
26
|
+
context.world.startback_jobs.insert(@job.to_data)
|
27
|
+
|
28
|
+
@job
|
29
|
+
end
|
30
|
+
|
31
|
+
emits(Event::JobCreated) do
|
32
|
+
{ id: @job.id }
|
33
|
+
end
|
34
|
+
|
35
|
+
end # class CreateJob
|
36
|
+
end # module Jobs
|
37
|
+
end # module Startback
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Startback
|
2
|
+
module Jobs
|
3
|
+
class RunJob < Operation
|
4
|
+
|
5
|
+
def initialize(input)
|
6
|
+
super(System['Job.Ref'].dress(input))
|
7
|
+
end
|
8
|
+
|
9
|
+
def call
|
10
|
+
services = context.factor(Services)
|
11
|
+
@job = services.get_job!(input)
|
12
|
+
|
13
|
+
job_context = context.fork(@job.op_context)
|
14
|
+
job_class = ::Kernel.const_get(@job.op_class)
|
15
|
+
job_input = @job.op_input
|
16
|
+
|
17
|
+
op_result = with_context(job_context) do
|
18
|
+
run job_class.new(job_input)
|
19
|
+
end
|
20
|
+
|
21
|
+
services.update_job!(input, {
|
22
|
+
opResult: op_result,
|
23
|
+
isReady: true,
|
24
|
+
strategy: 'Embedded',
|
25
|
+
})
|
26
|
+
|
27
|
+
op_result
|
28
|
+
end
|
29
|
+
|
30
|
+
emits(Event::JobRan) do
|
31
|
+
{ id: @job.id }
|
32
|
+
end
|
33
|
+
|
34
|
+
end # class RunJob
|
35
|
+
end # module Jobs
|
36
|
+
end # module Startback
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Startback
|
2
|
+
module Jobs
|
3
|
+
class Services < Startback::Services
|
4
|
+
|
5
|
+
def get_job!(ref)
|
6
|
+
job_relvar = startback_jobs.restrict(ref)
|
7
|
+
Model::Job.full(job_relvar.one)
|
8
|
+
rescue Bmg::OneError
|
9
|
+
not_found_error!("Job #{ref[:id]}")
|
10
|
+
end
|
11
|
+
|
12
|
+
def update_job!(ref, update)
|
13
|
+
job_relvar = startback_jobs.restrict(ref)
|
14
|
+
job_relvar.update(update)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def startback_jobs
|
20
|
+
context.world.startback_jobs
|
21
|
+
end
|
22
|
+
|
23
|
+
end # class Services
|
24
|
+
end # module Jobs
|
25
|
+
end # module Startback
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Startback
|
2
|
+
module Jobs
|
3
|
+
module Support
|
4
|
+
class JobResult
|
5
|
+
class Embedded < JobResult
|
6
|
+
|
7
|
+
def api_serve(api)
|
8
|
+
[
|
9
|
+
200,
|
10
|
+
{"Content-Type" => "application/json"},
|
11
|
+
[job.opResult.to_json]
|
12
|
+
]
|
13
|
+
end
|
14
|
+
|
15
|
+
end # class Embedded
|
16
|
+
end # class JobResult
|
17
|
+
end # module Support
|
18
|
+
end # module Jobs
|
19
|
+
end # module Startback
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Startback
|
2
|
+
module Jobs
|
3
|
+
module Support
|
4
|
+
class JobResult
|
5
|
+
class NotReady < JobResult
|
6
|
+
|
7
|
+
def api_serve(api)
|
8
|
+
[202, {}, []]
|
9
|
+
end
|
10
|
+
|
11
|
+
end # class NotReady
|
12
|
+
end # class JobResult
|
13
|
+
end # module Support
|
14
|
+
end # module Jobs
|
15
|
+
end # module Startback
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Startback
|
2
|
+
module Jobs
|
3
|
+
module Support
|
4
|
+
class JobResult
|
5
|
+
class Redirect < JobResult
|
6
|
+
|
7
|
+
DEFAULT_REDIRECT_OPTIONS = {
|
8
|
+
status: 301,
|
9
|
+
headers: {}
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
def api_serve(api)
|
13
|
+
options = redirect_options
|
14
|
+
[
|
15
|
+
options.status || 301,
|
16
|
+
options.headers.merge("Location" => job.opResult),
|
17
|
+
[]
|
18
|
+
]
|
19
|
+
end
|
20
|
+
|
21
|
+
def redirect_options
|
22
|
+
opts = DEFAULT_REDIRECT_OPTIONS.merge(
|
23
|
+
job.strategy_options
|
24
|
+
)
|
25
|
+
Startback::Model.new(opts)
|
26
|
+
end
|
27
|
+
|
28
|
+
end # class Embedded
|
29
|
+
end # class JobResult
|
30
|
+
end # module Support
|
31
|
+
end # module Jobs
|
32
|
+
end # module Startback
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Startback
|
2
|
+
module Jobs
|
3
|
+
module Support
|
4
|
+
class JobResult
|
5
|
+
|
6
|
+
def initialize(job)
|
7
|
+
@job = job
|
8
|
+
end
|
9
|
+
private :initialize
|
10
|
+
|
11
|
+
attr_reader :job
|
12
|
+
|
13
|
+
def self.for(job)
|
14
|
+
unless job.is_ready?
|
15
|
+
JobResult::NotReady.new(job)
|
16
|
+
else
|
17
|
+
JobResult.const_get(job.strategy).new(job)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def api_serve(api)
|
22
|
+
raise NotImplementedError
|
23
|
+
end
|
24
|
+
|
25
|
+
end # class JobResult
|
26
|
+
end # module Support
|
27
|
+
end # module Jobs
|
28
|
+
end # module Startback
|
29
|
+
require_relative 'job_result/not_ready'
|
30
|
+
require_relative 'job_result/embedded'
|
31
|
+
require_relative 'job_result/redirect'
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'support/job_result'
|
@@ -0,0 +1,44 @@
|
|
1
|
+
@import finitio/data
|
2
|
+
|
3
|
+
ClassName = String
|
4
|
+
|
5
|
+
DumpableHash = { ...: .Object }
|
6
|
+
|
7
|
+
Job.Ref = {
|
8
|
+
id : String
|
9
|
+
}
|
10
|
+
|
11
|
+
Job.Strategy = String(s | %w{Embedded NotReady Redirect}.include? s )
|
12
|
+
|
13
|
+
Job.Full = {
|
14
|
+
id : String
|
15
|
+
opClass : ClassName
|
16
|
+
opInput : DumpableHash
|
17
|
+
opContext : DumpableHash
|
18
|
+
opResult : .
|
19
|
+
isReady : Boolean
|
20
|
+
strategy : Job.Strategy
|
21
|
+
strategyOptions : DumpableHash
|
22
|
+
expiresAt : DateTime|Time|Nil
|
23
|
+
refreshFreq : String|Nil
|
24
|
+
refreshedAt : DateTime|Time|Nil
|
25
|
+
consumeMax : Integer|Nil
|
26
|
+
consumeCount : Integer|Nil
|
27
|
+
createdAt : DateTime|Time|Nil
|
28
|
+
createdBy : String|Nil
|
29
|
+
}
|
30
|
+
|
31
|
+
Job.CreationRequest = {
|
32
|
+
opClass : ClassName
|
33
|
+
opInput : DumpableHash
|
34
|
+
opContext : DumpableHash
|
35
|
+
createdBy : String
|
36
|
+
|
37
|
+
isReady : Boolean
|
38
|
+
strategy :? Job.Strategy
|
39
|
+
strategyOptions :? DumpableHash
|
40
|
+
|
41
|
+
expiresAt :? DateTime|Time
|
42
|
+
refreshFreq :? String
|
43
|
+
consumeMax :? Integer
|
44
|
+
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'path'
|
2
|
+
require 'finitio'
|
3
|
+
require 'startback'
|
4
|
+
require 'startback/event'
|
5
|
+
require 'startback/web/api'
|
6
|
+
|
7
|
+
module Startback
|
8
|
+
module Jobs
|
9
|
+
require_relative 'jobs/support'
|
10
|
+
require_relative 'jobs/model'
|
11
|
+
require_relative 'jobs/event'
|
12
|
+
require_relative 'jobs/operation'
|
13
|
+
require_relative 'jobs/services'
|
14
|
+
require_relative 'jobs/api'
|
15
|
+
require_relative 'jobs/agent'
|
16
|
+
|
17
|
+
require_relative './ext'
|
18
|
+
|
19
|
+
Finitio.stdlib_path(Path.dir.parent)
|
20
|
+
|
21
|
+
System = Finitio.system(Path.dir/'jobs.fio')
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,49 +1,42 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
1
2
|
require 'startback'
|
2
|
-
require 'startback/
|
3
|
-
require 'startback/support/fake_logger'
|
3
|
+
require 'startback/jobs'
|
4
4
|
require 'rack/test'
|
5
|
-
require '
|
5
|
+
require 'bmg'
|
6
6
|
|
7
7
|
module SpecHelpers
|
8
8
|
end
|
9
9
|
|
10
10
|
RSpec.configure do |c|
|
11
11
|
c.include SpecHelpers
|
12
|
-
end
|
13
|
-
|
14
|
-
class SubContext < Startback::Context
|
15
|
-
|
16
|
-
attr_accessor :foo
|
17
|
-
|
18
|
-
h_factory do |c,h|
|
19
|
-
c.foo = h["foo"]
|
20
|
-
end
|
21
|
-
|
22
|
-
h_dump do |h|
|
23
|
-
h.merge!("foo" => foo)
|
24
|
-
end
|
25
12
|
|
26
|
-
|
27
|
-
|
13
|
+
def a_job_data(override = {})
|
14
|
+
{
|
15
|
+
id: 'abcdef',
|
16
|
+
isReady: false,
|
17
|
+
opClass: 'CowSay',
|
18
|
+
opInput: { 'message' => 'Hello !!' },
|
19
|
+
opContext: {},
|
20
|
+
opResult: nil,
|
21
|
+
strategy: 'NotReady',
|
22
|
+
strategyOptions: {},
|
23
|
+
expiresAt: nil,
|
24
|
+
refreshFreq: nil,
|
25
|
+
refreshedAt: nil,
|
26
|
+
consumeMax: nil,
|
27
|
+
consumeCount: 0,
|
28
|
+
createdAt: DateTime.now,
|
29
|
+
createdBy: 'blambeau',
|
30
|
+
}.merge(override)
|
28
31
|
end
|
29
|
-
|
30
32
|
end
|
31
33
|
|
32
|
-
class
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
h_factory do |c,h|
|
37
|
-
c.bar = h["bar"]
|
34
|
+
class CowSay < Startback::Operation
|
35
|
+
def initialize(input)
|
36
|
+
@input = Startback::Model.new(input)
|
38
37
|
end
|
39
38
|
|
40
|
-
|
41
|
-
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
class User
|
47
|
-
class Changed < Startback::Event
|
39
|
+
def call
|
40
|
+
input.message
|
48
41
|
end
|
49
42
|
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Startback
|
4
|
+
module Jobs
|
5
|
+
describe Api, "GET /{id}/result/" do
|
6
|
+
include Rack::Test::Methods
|
7
|
+
|
8
|
+
let(:job_data) do
|
9
|
+
a_job_data(override)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:jobs_relvar) do
|
13
|
+
Bmg.mutable([job_data])
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:context) do
|
17
|
+
Context.new.with_world(startback_jobs: jobs_relvar)
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:app) do
|
21
|
+
context = self.context
|
22
|
+
Rack::Builder.new do
|
23
|
+
use Context::Middleware, context
|
24
|
+
run Jobs::Api
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:job_id) do
|
29
|
+
'abcdef'
|
30
|
+
end
|
31
|
+
|
32
|
+
subject do
|
33
|
+
get "/#{job_id}/result/"
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when the job does not exist' do
|
37
|
+
let(:job_id) do
|
38
|
+
"no-such-one"
|
39
|
+
end
|
40
|
+
|
41
|
+
let(:override) do
|
42
|
+
{}
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'raises' do
|
46
|
+
expect{
|
47
|
+
subject
|
48
|
+
}.to raise_error(Startback::Errors::NotFoundError)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when the job is not ready yet' do
|
53
|
+
let(:override) do
|
54
|
+
{
|
55
|
+
isReady: false,
|
56
|
+
strategy: 'NotReady',
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'works fine' do
|
61
|
+
res = subject
|
62
|
+
expect(res.status).to eql(202)
|
63
|
+
expect(res.body).to be_empty
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'when the job is ready' do
|
68
|
+
let(:override) do
|
69
|
+
{
|
70
|
+
isReady: true,
|
71
|
+
opResult: { "foo" => 'Hello!!' },
|
72
|
+
strategy: 'Embedded'
|
73
|
+
}
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'works fine' do
|
77
|
+
res = subject
|
78
|
+
expect(res.status).to eql(200)
|
79
|
+
expect(res.body).to eql({ "foo" => 'Hello!!' }.to_json)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'when the job is ready and has to redirect' do
|
84
|
+
let(:override) do
|
85
|
+
{
|
86
|
+
isReady: true,
|
87
|
+
strategy: 'Redirect',
|
88
|
+
opResult: 'http://google.com',
|
89
|
+
}
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'works fine' do
|
93
|
+
res = subject
|
94
|
+
expect(res.status).to eql(301)
|
95
|
+
expect(res['Location']).to eql("http://google.com")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context 'when the job is ready and has to redirect with a 302' do
|
100
|
+
let(:override) do
|
101
|
+
{
|
102
|
+
isReady: true,
|
103
|
+
strategy: 'Redirect',
|
104
|
+
strategyOptions: {
|
105
|
+
'status' => 302,
|
106
|
+
'headers' => { 'X-Mine' => 'foo' }
|
107
|
+
},
|
108
|
+
opResult: 'http://google.com',
|
109
|
+
}
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'works fine' do
|
113
|
+
res = subject
|
114
|
+
expect(res.status).to eql(302)
|
115
|
+
expect(res['X-Mine']).to eql('foo')
|
116
|
+
expect(res['Location']).to eql("http://google.com")
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Startback
|
4
|
+
module Jobs
|
5
|
+
class Model
|
6
|
+
describe Job do
|
7
|
+
it 'makes it easy to create an instance' do
|
8
|
+
job = Job.new(a_job_data)
|
9
|
+
expect(job.id).to eql('abcdef')
|
10
|
+
expect(job[:id]).to eql('abcdef')
|
11
|
+
expect(job.ready?).to eql(false)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'makes it easy to dress an instance' do
|
15
|
+
job = Job.full(a_job_data)
|
16
|
+
expect(job.id).to eql('abcdef')
|
17
|
+
expect(job[:id]).to eql('abcdef')
|
18
|
+
expect(job.ready?).to eql(false)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|