operationable 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f604843ce9dfd14fa65505a764ca64ba7db621f8
4
- data.tar.gz: 11f4bd95302fc3b8580d333e17d56fdf2f1ad45c
3
+ metadata.gz: 242b22f9a25dc80a803e755e3ffbf896ad38f40f
4
+ data.tar.gz: d4b942448bd5aa78bd8e538ba600501ca94ffb22
5
5
  SHA512:
6
- metadata.gz: f7822dde3a5d044b8c5c7844a8dc22242a32e4487102f53a9856cb0a772293de771f46056eff144221f92f25bc03626be3a97f268f2e1c59e7dd6a366b8104ec
7
- data.tar.gz: 08118da4160440c14b731ec32a6683077cb271fbcf1f0838b248fa7e1dd1af82e4effda3c492c264bc33d1a13a23110410a582e2f6de48b30f9e56da994541aa
6
+ metadata.gz: 3ff60383e608ac86b63f6dc5a857ab3c06aa8397495f9abd531b712add4670ed4f6aa8d2788eb22e6d3a26b73e2bc280bde4ebab6b7f9649ad217e4bb1a71dbf
7
+ data.tar.gz: 1d4fae19b8ac1ee54049b5a60254de7550dff48aa8f47964e2f52e378077d5a6c433d06a717e51495dd8ce5a4bf7dd3834fec17cf6cbef50583c5da9bf32ac15
data/README.md CHANGED
@@ -113,28 +113,6 @@ Class that contain callback methods, that will called after model saved in runti
113
113
  #Validators
114
114
  TODO: describe validators
115
115
 
116
- #Persistence and guaranteed delivery
117
-
118
- First add table to store operations and track their execution in pair with resque-status
119
-
120
- ```
121
- class CreateOperationCallbacks < ActiveRecord::Migration[5.0]
122
- def change
123
- create_table :operation_callbacks do |t|
124
- t.string :status
125
- t.text :message
126
- t.string :uuid
127
- t.json :q_options
128
- t.json :props
129
-
130
- t.timestamps
131
- end
132
- end
133
- end
134
- ```
135
-
136
- After that all operations by default will populate database with status of execution.
137
-
138
116
  ## Development
139
117
 
140
118
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,22 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
  class OperationJob < ActiveJob::Base
3
- include Operationable::Persisters::Base
4
- include Operationable::Persisters::Memory
5
- include Operationable::Persisters::Database
6
-
7
3
  queue_as do
8
4
  arguments.first[:q_options][:queue]
9
5
  end
10
6
 
11
- after_enqueue do |job|
12
- create_status_hash
13
- notify_database
14
- end
15
-
16
- around_perform do |job, block|
17
- safe_perform(job, block)
18
- end
19
-
20
7
  after_perform :clear_runtime
21
8
 
22
9
  def perform(q_options:, props:)
@@ -13,10 +13,6 @@ require 'operationable/operation'
13
13
  require 'operationable/serializer'
14
14
  require 'operationable/specification'
15
15
 
16
- require 'operationable/persisters/base'
17
- require 'operationable/persisters/database'
18
- require 'operationable/persisters/memory'
19
-
20
16
  require 'operationable/runners/base'
21
17
  require 'operationable/runners/serial'
22
18
  require 'operationable/runners/separate'
@@ -1,3 +1,3 @@
1
1
  module Operationable
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: operationable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirill Suhodolov
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-03-12 00:00:00.000000000 Z
12
+ date: 2017-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activejob
@@ -118,9 +118,6 @@ files:
118
118
  - lib/operationable/builder.rb
119
119
  - lib/operationable/callback.rb
120
120
  - lib/operationable/operation.rb
121
- - lib/operationable/persisters/base.rb
122
- - lib/operationable/persisters/database.rb
123
- - lib/operationable/persisters/memory.rb
124
121
  - lib/operationable/runners/base.rb
125
122
  - lib/operationable/runners/separate.rb
126
123
  - lib/operationable/runners/serial.rb
@@ -1,107 +0,0 @@
1
- module Operationable
2
- module Persisters
3
- module Base
4
- STATUS_INIT = 'init'
5
- STATUS_QUEUED = 'queued'
6
- STATUS_WORKING = 'working'
7
- STATUS_COMPLETED = 'completed'
8
- STATUS_FAILED = 'failed'
9
- STATUS_KILLED = 'killed'
10
- STATUSES = [
11
- STATUS_INIT,
12
- STATUS_QUEUED,
13
- STATUS_WORKING,
14
- STATUS_COMPLETED,
15
- STATUS_FAILED,
16
- STATUS_KILLED
17
- ].freeze
18
-
19
- extend Forwardable
20
- extend ActiveSupport::Concern
21
-
22
- class Killed < RuntimeError; end
23
- class NotANumber < RuntimeError; end
24
-
25
- # attr_reader :uuid, :options
26
-
27
- def uuid
28
- self.job_id
29
- end
30
-
31
- def options
32
- arguments.first
33
- end
34
-
35
- # Run by the Resque::Worker when processing this job. It wraps the <tt>perform</tt>
36
- # method ensuring that the final status of the job is set regardless of error.
37
- # If an error occurs within the job's work, it will set the status as failed and
38
- # re-raise the error.
39
- def safe_perform(job, block)
40
- working
41
- block.call
42
- if status && status.failed?
43
- on_failure(status.message) if respond_to?(:on_failure)
44
- return
45
- elsif status && !status.completed?
46
- completed
47
- end
48
- on_success if respond_to?(:on_success)
49
- rescue Killed
50
- Resque::Plugins::Status::Hash.killed(uuid)
51
- on_killed if respond_to?(:on_killed)
52
- rescue => e
53
- failed("The task failed because of an error: #{e}")
54
- if respond_to?(:on_failure)
55
- on_failure(e)
56
- else
57
- raise e
58
- end
59
- end
60
-
61
- def name
62
- "#{self.class.name}(#{callback_class_name} #{callback_method_name})"
63
- end
64
-
65
- def callback_class_name
66
- arguments.first[:q_options][:callback_class_name]
67
- end
68
-
69
- def callback_method_name
70
- arguments.first[:q_options][:callback_method_name]
71
- end
72
-
73
- def working
74
- set_status({'status' => Operationable::Persisters::Base::STATUS_WORKING, attempts: 1})
75
- end
76
-
77
- # set the status to 'failed' passing along any additional messages
78
- def failed(*messages)
79
- set_status({'status' => Operationable::Persisters::Base::STATUS_FAILED}, *messages)
80
- end
81
-
82
- # set the status to 'completed' passing along any addional messages
83
- def completed(*messages)
84
- set_status({
85
- 'status' => Operationable::Persisters::Base::STATUS_COMPLETED,
86
- 'message' => "Completed at #{Time.now}"
87
- }, *messages)
88
- end
89
-
90
- # kill the current job, setting the status to 'killed' and raising <tt>Killed</tt>
91
- def kill!
92
- set_status({
93
- 'status' => Operationable::Persisters::Base::STATUS_KILLED,
94
- 'message' => "Killed at #{Time.now}"
95
- })
96
- raise Killed
97
- end
98
-
99
- private
100
-
101
- def set_status(*args)
102
- self.database_status = args
103
- self.status = [status, {'name' => name}, args].flatten
104
- end
105
- end
106
- end
107
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
- module Operationable
3
- module Persisters
4
- module Database
5
- def self.create(q_options, props)
6
- date = Time.zone.now.to_date
7
-
8
- ::OperationCallback.create(
9
- q_options: q_options,
10
- props: props,
11
- attempts: 0,
12
- week: props[:week] || date.cweek,
13
- year: props[:year] || date.year,
14
- status: Operationable::Persisters::Base::STATUS_INIT
15
- )
16
- end
17
-
18
- def notify_database
19
- self.database_status = [{status: Operationable::Persisters::Base::STATUS_QUEUED}]
20
- end
21
-
22
- def op_cb_id
23
- arguments.first[:q_options][:op_cb_id]
24
- end
25
-
26
- def database_status
27
- ::OperationCallback.find(op_cb_id)
28
- end
29
-
30
- def database_status=(new_status)
31
- ::OperationCallback.find(op_cb_id).update(
32
- new_status.reduce({uuid: uuid}){ |acc, o| acc.merge(o) }
33
- )
34
- end
35
- end
36
- end
37
- end
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
- # codebase has been taken from abandoned resque-status gem, that not compatible with ActiveJob and Rails now
3
- module Operationable
4
- module Persisters
5
- module Memory
6
- autoload :Hash, 'resque/plugins/status/hash'
7
-
8
- def create_status_hash
9
- Resque::Plugins::Status::Hash.create(uuid, options)
10
- end
11
-
12
- # Set the jobs status. Can take an array of strings or hashes that are merged
13
- # (in order) into a final status hash.
14
- def status=(new_status)
15
- Resque::Plugins::Status::Hash.set(uuid, *new_status)
16
- end
17
-
18
- # get the Resque::Plugins::Status::Hash object for the current uuid
19
- def status
20
- Resque::Plugins::Status::Hash.get(uuid)
21
- end
22
-
23
- # Checks against the kill list if this specific job instance should be killed
24
- # on the next iteration
25
- def should_kill?
26
- Resque::Plugins::Status::Hash.should_kill?(uuid)
27
- end
28
-
29
- # set the status of the job for the current itteration. <tt>num</tt> and
30
- # <tt>total</tt> are passed to the status as well as any messages.
31
- # This will kill the job if it has been added to the kill list with
32
- # <tt>Resque::Plugins::Status::Hash.kill()</tt>
33
- def at(num, total, *messages)
34
- if total.to_f <= 0.0
35
- raise(NotANumber, "Called at() with total=#{total} which is not a number")
36
- end
37
- tick({
38
- 'num' => num,
39
- 'total' => total
40
- }, *messages)
41
- end
42
-
43
- # sets the status of the job for the current itteration. You should use
44
- # the <tt>at</tt> method if you have actual numbers to track the iteration count.
45
- # This will kill the job if it has been added to the kill list with
46
- # <tt>Resque::Plugins::Status::Hash.kill()</tt>
47
- def tick(*messages)
48
- kill! if should_kill?
49
- set_status({'status' => Operationable::Persisters::Base::STATUS_WORKING}, *messages)
50
- end
51
-
52
- end
53
- end
54
- end