dwf 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb0f6f6f30b7ed1ba19120a4a1f77e450940e45433153de51f47df9f108d0fae
4
- data.tar.gz: e0e86fdd169b7cbcdc811cf62f87b3043fbbd708bdf0f41fb006b392e4ab784b
3
+ metadata.gz: 395f410168239441a56db548cccc50d729433bedcc9fd0d4c3ae15fea09c1019
4
+ data.tar.gz: 5a944cc497760afe6b92f38772d594d56f15837ab51495609dba24f7578af309
5
5
  SHA512:
6
- metadata.gz: 4a3770d4d7f63b7ede02d59e1ce43cb2634b39cbd9e037c920e4f816acb7b393de0f9030fda735e8ea0873f3679da120ff405f725e87c60c1b9eb6a7fbf355e3
7
- data.tar.gz: 469e25cdf11441c59c80ff47197dcabcf40e0b086ab06babb6b39a0597483517c479ee9ea65767819b69afebf83db6dcdb5926a9839a2e40d02c03cf39a6eb57
6
+ metadata.gz: 0123f65c0242c905c93e0998365a43b4193432c6e5b74af4a33422e7b489424294a70c68cf4e42feb627ecc038429e0dba6dc54af80acd1dc7628c2b56ff532c
7
+ data.tar.gz: 674a7144dc944d878cfddb549254985dc0a136fd84069df184a44c8a375f6ccc47e9be3d6b445f6bfcc83af8f52ed6b23fa9687f23714e49b30e24a6bbdea1de
data/CHANGELOG.md CHANGED
@@ -1,5 +1,47 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
+ ## 0.1.8
4
+ ### Added
5
+ - add pinlining feature
6
+
7
+ ```ruby
8
+ class SendOutput < Dwf::Item
9
+ def perform
10
+ output('it works')
11
+ end
12
+ end
13
+
14
+ ```
15
+
16
+ `output` method used to output data from the job to add outgoing jobs
17
+
18
+ ```ruby
19
+ class ReceiveOutput < Dwf::Item
20
+ def perform
21
+ message = payloads.first[:output] # it works
22
+ end
23
+ end
24
+ ```
25
+
26
+ `payloads` is an array that containing outputs from incoming jobs
27
+
28
+ ```
29
+ [
30
+ {
31
+ id: "SendOutput|1849a3f9-5fce-401e-a73a-91fc1048356",
32
+ class: "SendOutput",
33
+ output: 'it works'
34
+ }
35
+ ]
36
+ ```
37
+
38
+ ```ruby
39
+ Dwf.config do |config|
40
+ config.opts = { url 'redis://127.0.0.1:6379' }
41
+ config.namespace = 'dwf'
42
+ end
43
+ ```
44
+
3
45
  ## 0.1.7
4
46
  ### Added
5
47
  - Allow to config redis and queue
data/README.md CHANGED
@@ -83,6 +83,39 @@ Dwf.config do |config|
83
83
  end
84
84
  ```
85
85
 
86
+ # Pinelining
87
+ You can pass jobs result to next nodes
88
+
89
+ ```ruby
90
+ class SendOutput < Dwf::Item
91
+ def perform
92
+ output('it works')
93
+ end
94
+ end
95
+
96
+ ```
97
+
98
+ `output` method used to output data from the job to add outgoing jobs
99
+
100
+ ```ruby
101
+ class ReceiveOutput < Dwf::Item
102
+ def perform
103
+ message = payloads.first[:output] # it works
104
+ end
105
+ end
106
+ ```
107
+
108
+ `payloads` is an array that containing outputs from incoming jobs
109
+
110
+ ```ruby
111
+ [
112
+ {
113
+ id: "SendOutput|1849a3f9-5fce-401e-a73a-91fc1048356",
114
+ class: "SendOutput",
115
+ output: 'it works'
116
+ }
117
+ ]
118
+ ```
86
119
 
87
120
  # Todo
88
121
  - [x] Make it work
data/dwf.gemspec CHANGED
@@ -3,11 +3,10 @@
3
3
 
4
4
  lib = File.expand_path('../lib', __FILE__)
5
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
- require_relative 'lib/dwf/version'
7
6
 
8
7
  Gem::Specification.new do |spec|
9
8
  spec.name = "dwf"
10
- spec.version = Dwf::VERSION
9
+ spec.version = '0.1.8'
11
10
  spec.authors = ["dthtien"]
12
11
  spec.email = ["tiendt2311@gmail.com"]
13
12
 
data/lib/dwf/item.rb CHANGED
@@ -5,22 +5,11 @@ require_relative 'client'
5
5
  module Dwf
6
6
  class Item
7
7
  attr_reader :workflow_id, :id, :params, :queue, :klass, :started_at,
8
- :enqueued_at, :finished_at, :failed_at, :callback_type
8
+ :enqueued_at, :finished_at, :failed_at, :callback_type, :output_payload
9
9
  attr_accessor :incoming, :outgoing
10
10
 
11
11
  def initialize(options = {})
12
- @workflow_id = options[:workflow_id]
13
- @id = options[:id]
14
- @params = options[:params]
15
- @queue = options[:queue]
16
- @incoming = options[:incoming] || []
17
- @outgoing = options[:outgoing] || []
18
- @klass = options[:klass] || self.class
19
- @failed_at = options[:failed_at]
20
- @finished_at = options[:finished_at]
21
- @enqueued_at = options[:enqueued_at]
22
- @started_at = options[:started_at]
23
- @callback_type = options[:callback_type]
12
+ assign_attributes(options)
24
13
  end
25
14
 
26
15
  def self.from_hash(hash)
@@ -39,6 +28,11 @@ module Dwf
39
28
  callback_type == Dwf::Workflow::BUILD_IN
40
29
  end
41
30
 
31
+ def reload
32
+ item = client.find_job(workflow_id, name)
33
+ assign_attributes(item.to_hash)
34
+ end
35
+
42
36
  def perform_async
43
37
  Dwf::Worker.set(queue: queue || client.config.namespace)
44
38
  .perform_async(workflow_id, name)
@@ -48,6 +42,10 @@ module Dwf
48
42
  @name ||= "#{klass}|#{id}"
49
43
  end
50
44
 
45
+ def output(data)
46
+ @output_payload = data
47
+ end
48
+
51
49
  def no_dependencies?
52
50
  incoming.empty?
53
51
  end
@@ -58,6 +56,17 @@ module Dwf
58
56
  end
59
57
  end
60
58
 
59
+ def payloads
60
+ incoming.map do |job_name|
61
+ job = client.find_job(workflow_id, job_name)
62
+ {
63
+ id: job.name,
64
+ class: job.klass.to_s,
65
+ output: job.output_payload
66
+ }
67
+ end
68
+ end
69
+
61
70
  def enqueue!
62
71
  @enqueued_at = current_timestamp
63
72
  @started_at = nil
@@ -142,7 +151,8 @@ module Dwf
142
151
  failed_at: failed_at,
143
152
  params: params,
144
153
  workflow_id: workflow_id,
145
- callback_type: callback_type
154
+ callback_type: callback_type,
155
+ output_payload: output_payload
146
156
  }
147
157
  end
148
158
 
@@ -159,5 +169,21 @@ module Dwf
159
169
  def client
160
170
  @client ||= Dwf::Client.new
161
171
  end
172
+
173
+ def assign_attributes(options)
174
+ @workflow_id = options[:workflow_id]
175
+ @id = options[:id]
176
+ @params = options[:params]
177
+ @queue = options[:queue]
178
+ @incoming = options[:incoming] || []
179
+ @outgoing = options[:outgoing] || []
180
+ @klass = options[:klass] || self.class
181
+ @failed_at = options[:failed_at]
182
+ @finished_at = options[:finished_at]
183
+ @enqueued_at = options[:enqueued_at]
184
+ @started_at = options[:started_at]
185
+ @callback_type = options[:callback_type]
186
+ @output_payload = options[:output_payload]
187
+ end
162
188
  end
163
189
  end
data/lib/dwf/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dwf
4
- VERSION = '0.1.7'
4
+ VERSION = '0.1.8'
5
5
  end
data/lib/dwf/worker.rb CHANGED
@@ -9,7 +9,7 @@ module Dwf
9
9
 
10
10
  def perform(workflow_id, job_name)
11
11
  job = client.find_job(workflow_id, job_name)
12
- return job.enqueue_outgoing_jobs if job. succeeded?
12
+ return job.enqueue_outgoing_jobs if job.succeeded?
13
13
 
14
14
  job.mark_as_started
15
15
  job.perform
@@ -181,4 +181,40 @@ describe Dwf::Item, item: true do
181
181
  it { expect(a_item).not_to have_received(:persist_and_perform_async!) }
182
182
  end
183
183
  end
184
+
185
+ describe '#output' do
186
+ before { item.output(1) }
187
+
188
+ it { expect(item.output_payload).to eq 1 }
189
+ end
190
+
191
+ describe '#payloads' do
192
+ let(:incoming) { ["A|#{SecureRandom.uuid}"] }
193
+ let(:client_double) { double(find_job: nil) }
194
+ let!(:a_item) do
195
+ described_class.new(
196
+ workflow_id: SecureRandom.uuid,
197
+ id: SecureRandom.uuid,
198
+ finished_at: finished_at,
199
+ output_payload: 1
200
+ )
201
+ end
202
+
203
+ before do
204
+ allow(Dwf::Client).to receive(:new).and_return client_double
205
+ allow(client_double)
206
+ .to receive(:find_job).and_return a_item
207
+ end
208
+
209
+ it do
210
+ expected_payload = [
211
+ {
212
+ class: a_item.class.name,
213
+ id: a_item.name,
214
+ output: 1
215
+ }
216
+ ]
217
+ expect(item.payloads).to eq expected_payload
218
+ end
219
+ end
184
220
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'mock_redis'
5
+ require 'sidekiq/testing'
6
+
7
+ describe Dwf::Worker, client: true do
8
+ let(:workflow_id) { SecureRandom.uuid }
9
+ let(:id) { SecureRandom.uuid }
10
+ let(:redis) { Redis.new }
11
+ let(:worker) { described_class.perform_async(workflow_id, job.name) }
12
+ before do
13
+ redis_instance = MockRedis.new
14
+ allow(Redis).to receive(:new).and_return redis_instance
15
+ end
16
+
17
+ describe '#find_job' do
18
+ let!(:job) do
19
+ j = Dwf::Item.new(workflow_id: workflow_id, id: id)
20
+ j.persist!
21
+ j
22
+ end
23
+
24
+ before do
25
+ worker
26
+ Sidekiq::Worker.drain_all
27
+ job.reload
28
+ end
29
+
30
+ it { expect(job.finished?).to be_truthy }
31
+ end
32
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dwf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - dthtien
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-08 00:00:00.000000000 Z
11
+ date: 2021-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -110,6 +110,7 @@ files:
110
110
  - spec/dwf/configuration_spec.rb
111
111
  - spec/dwf/item_spec.rb
112
112
  - spec/dwf/utils_spec.rb
113
+ - spec/dwf/worker_spec.rb
113
114
  - spec/spec_helper.rb
114
115
  homepage: https://github.com/dthtien/wf
115
116
  licenses:
@@ -140,4 +141,5 @@ test_files:
140
141
  - spec/dwf/configuration_spec.rb
141
142
  - spec/dwf/item_spec.rb
142
143
  - spec/dwf/utils_spec.rb
144
+ - spec/dwf/worker_spec.rb
143
145
  - spec/spec_helper.rb