raes 0.1.2 → 0.2.3

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: e9a39e2b8f32e2d974242f22ceb8a1a2b290b8e627d397e734cfe880be1dfacf
4
- data.tar.gz: f1025148b187c81a1a7481bc4a808af4a763a52652293d7898b3275a7cb8764e
3
+ metadata.gz: 9392ed6cf1189aadf25bf2a88a7eae729622dd19a71b33a64d41783e1062fe00
4
+ data.tar.gz: 327a5cb0c338e933d91e41b0a34b01b5abd975e3d4448a5fa39da9bf5a332398
5
5
  SHA512:
6
- metadata.gz: 352dfd2b50b583160512007bdd95093e62be21f128ae032bb869bdd83cb0b52fca74cf7bf17cc864eb458bdeb7fbf1d8ccd5e4f4b7a66890f0040197914ecb76
7
- data.tar.gz: c7960ca2ff1df42c2a5ed6be974a0474de647062410597ad41d14afeaf65dd0da8c909c2f98540997b458224d6f56650391efa06bfec856bda0788c5cfceb348
6
+ metadata.gz: 95e167c1ff7f4b8a6685643f44ef4592165e1d73332785532866a2b371c9f22ed80cab3c77f101ca8f689626cdbaf28c851a68bc1e408de04a7e285a56fe98c6
7
+ data.tar.gz: ce567beb27fbcdba14a53450b6e3af91af5f5c344d4581602ed1f9aaf8f0d7860423a85e827cd9c5cbc16051e3a7c19856c8ef7ac0e9e317de6dfbd2285b9b4b
data/README.md CHANGED
@@ -86,6 +86,25 @@ If use storage, then execute:
86
86
  $ rails active_storage:install
87
87
  ```
88
88
 
89
+ ## Configuration
90
+
91
+ Add the following to your config/raes.yml:
92
+
93
+ ```yaml
94
+ default: &default
95
+ concern:
96
+ context_logger: Rails
97
+
98
+ development:
99
+ <<: *default
100
+
101
+ test:
102
+ <<: *default
103
+
104
+ production:
105
+ <<: *default
106
+ ```
107
+
89
108
  ## Test
90
109
 
91
110
  ```bash
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raes
4
+ class StatelessJob < ApplicationJob
5
+ def perform(name, payload)
6
+ klass = name.constantize
7
+ if klass.instance_method(:initialize).arity.zero?
8
+ klass.call
9
+ else
10
+ klass.call(payload.symbolize_keys)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Raes::Engine.routes.draw do
4
+ # engine routes
4
5
  end
@@ -2,11 +2,19 @@
2
2
 
3
3
  require 'raes/engine'
4
4
  require 'raes/use_caller'
5
+ require 'raes/use_context_logger'
6
+ require 'raes/use_memory_limit'
5
7
  require 'raes/use_storage'
6
8
  require 'raes/dispatch'
7
9
  require 'raes/reducer'
8
10
 
9
11
  module Raes
12
+ def self.config
13
+ @config ||= Rails.application.config_for(:raes)
14
+ rescue StandardError
15
+ @config = {}
16
+ end
17
+
10
18
  def self.dispatch(name, payload = {})
11
19
  Dispatch.call(name, payload)
12
20
  end
@@ -14,4 +22,8 @@ module Raes
14
22
  def self.search(name)
15
23
  Action.search(name)
16
24
  end
25
+
26
+ def self.stateless(name, payload = {})
27
+ StatelessJob.perform_later(name.to_s, payload)
28
+ end
17
29
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raes
4
+ class ContextLogger
5
+ def initialize(class_name)
6
+ @class_name = class_name
7
+ @logger_class = logger_class
8
+ end
9
+
10
+ def debug(message)
11
+ @logger_class.logger.debug context(message)
12
+ end
13
+
14
+ def info(message)
15
+ @logger_class.logger.info context(message)
16
+ end
17
+
18
+ def warn(message)
19
+ @logger_class.logger.warn context(message)
20
+ end
21
+
22
+ def error(message)
23
+ @logger_class.logger.error context(message)
24
+ end
25
+
26
+ def fatal(message)
27
+ @logger_class.logger.fatal context(message)
28
+ end
29
+
30
+ private
31
+
32
+ def context(message)
33
+ { class: @class_name, message: message }
34
+ end
35
+
36
+ def logger_class
37
+ (Raes.config.dig(:concern, :context_logger) || 'Rails').constantize
38
+ end
39
+ end
40
+ end
@@ -23,10 +23,11 @@ module Raes
23
23
  end
24
24
 
25
25
  def create_state
26
- if @action.name.constantize.instance_method(:initialize).arity.zero?
27
- @action.name.constantize.call
26
+ klass = @action.name.constantize
27
+ if klass.instance_method(:initialize).arity.zero?
28
+ klass.call
28
29
  else
29
- @action.name.constantize.call(JSON.parse(@action.payload).symbolize_keys)
30
+ klass.call(JSON.parse(@action.payload).symbolize_keys)
30
31
  end
31
32
  end
32
33
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'raes/context_logger'
4
+
5
+ module Raes
6
+ module UseContextLogger
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ def logger
11
+ ContextLogger.new(self.class.name)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Raes
4
+ module UseMemoryLimit
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ def memsize
9
+ GetProcessMem.new(Process.pid).mb.ceil
10
+ end
11
+
12
+ def memsize_with_gc
13
+ size = memsize
14
+ if !memory_limit.zero? && size > memory_limit
15
+ GC.start
16
+ size = memsize
17
+ end
18
+ size
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def memory_limit
25
+ @memory_limit ||= Raes.config.dig(:concern, :memory_limit).to_i
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Raes
4
- VERSION = '0.1.2'
4
+ VERSION = '0.2.3'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ogom
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-04 00:00:00.000000000 Z
11
+ date: 2020-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -30,6 +30,20 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 6.0.3.2
33
+ - !ruby/object:Gem::Dependency
34
+ name: get_process_mem
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: factory_bot_rails
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -87,6 +101,7 @@ files:
87
101
  - app/controllers/raes/application_controller.rb
88
102
  - app/helpers/raes/application_helper.rb
89
103
  - app/jobs/raes/application_job.rb
104
+ - app/jobs/raes/stateless_job.rb
90
105
  - app/jobs/raes/subscribe_job.rb
91
106
  - app/mailers/raes/application_mailer.rb
92
107
  - app/models/raes/action.rb
@@ -97,10 +112,13 @@ files:
97
112
  - db/migrate/20200628202001_create_raes_actions.rb
98
113
  - db/migrate/20200628202002_create_raes_storages.rb
99
114
  - lib/raes.rb
115
+ - lib/raes/context_logger.rb
100
116
  - lib/raes/dispatch.rb
101
117
  - lib/raes/engine.rb
102
118
  - lib/raes/reducer.rb
103
119
  - lib/raes/use_caller.rb
120
+ - lib/raes/use_context_logger.rb
121
+ - lib/raes/use_memory_limit.rb
104
122
  - lib/raes/use_storage.rb
105
123
  - lib/raes/version.rb
106
124
  - lib/tasks/raes_tasks.rake
@@ -109,7 +127,7 @@ licenses:
109
127
  - MIT
110
128
  metadata:
111
129
  allowed_push_host: https://rubygems.org/
112
- post_install_message:
130
+ post_install_message:
113
131
  rdoc_options: []
114
132
  require_paths:
115
133
  - lib
@@ -125,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
143
  version: '0'
126
144
  requirements: []
127
145
  rubygems_version: 3.0.3
128
- signing_key:
146
+ signing_key:
129
147
  specification_version: 4
130
148
  summary: Summary of Rails Async Event Store.
131
149
  test_files: []