fancybox2 0.0.2 → 0.0.7

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: 45c38b5bf5cf22c421a61cd6ebcf5e09802d92047fb02f74a53ea97a5cd2a0d1
4
- data.tar.gz: fda98ad8a2a41d27e6d84a278e74d5d29aab5e97934d1e4794aaddc8ca24947b
3
+ metadata.gz: 771569623e40be10df4f7903ee6bde6b9523421e559f1ed05418932ba34da21e
4
+ data.tar.gz: 15bfb48338dd6785ecabdd63dd0b714eefee51d7a18d2add44b0a43cb7d27acb
5
5
  SHA512:
6
- metadata.gz: 75b363b07e701e3d143d0055cab28acbfc39e00121d41a26d56f3d2fd84fbb5be895f973b53987a5830ed2e242148f8a5cfdd8eca8f1217768af5fde108c9b5c
7
- data.tar.gz: cfa1d5b4a6501bbddf1923937e1d6c1a9e804bbc6244052ebc045099f2538c72fd2e0d631413516e76420b05b0a7845f7fab830c157b0f14f51c74aaa5e4d373
6
+ metadata.gz: 44ecd19a2239f7687496040045110b2274898c57f33721b63321877e1fffc7e2f2419abc56be3072126bd89e7b353068a5c15a0d10dddf5a85289746adcdb0c0
7
+ data.tar.gz: eb42ba62b805b3e43fe51784d900021b81d779569365d5a617d07639635211013f23fd3c3538c81ac04a1eb7d2719d924fe06d29287728569c1332159287a709
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'zeitwerk'
2
4
  require 'logger'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Array
2
4
  # Extracts options from a set of arguments. Removes and returns the last
3
5
  # element in the array if it's a hash, otherwise returns a blank hash.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # :nocov:
2
4
  class Hash
3
5
  # Returns a new hash with all keys converted using the block operation.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'logger'
2
4
  require 'json'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fancybox2
2
4
  module Logger
3
5
  class MQTTLogDevice
@@ -11,10 +13,13 @@ module Fancybox2
11
13
  unless @client.respond_to?(:publish)
12
14
  raise ArgumentError, "provided client does not respond to 'publish'"
13
15
  end
16
+ unless @client.respond_to?(:connected?)
17
+ raise ArgumentError, "provided client does not respond to 'connected?'"
18
+ end
14
19
  end
15
20
 
16
21
  def write(message)
17
- if @client.connected?
22
+ if @client && @client.connected?
18
23
  @client.publish @topic, message
19
24
  end
20
25
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fancybox2
2
4
  module Logger
3
5
 
@@ -0,0 +1,24 @@
1
+ module Fancybox2
2
+ module Migrations
3
+ class Base
4
+
5
+ class << self
6
+ def self.descendants
7
+ ObjectSpace.each_object(Class).select { |klass| klass < self }
8
+ end
9
+ end
10
+
11
+ attr_reader :name, :version
12
+
13
+ def initialize(name)
14
+ @name = name
15
+
16
+ @version = Runner.extract_and_validate_version_from name
17
+ end
18
+
19
+ def call(up_or_down = :up)
20
+ send up_or_down
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ module Fancybox2
2
+ module Migrations
3
+ module Exceptions
4
+
5
+ class FileNameError < StandardError
6
+ def initialize(message = nil)
7
+ message = message || 'One of the provided migrations file has an invalid name'
8
+ super(message)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,78 @@
1
+ module Fancybox2
2
+ module Migrations
3
+ class Runner
4
+ include Exceptions
5
+
6
+ VERSION_REGEXP = /^(\d{14})/.freeze
7
+
8
+ class << self
9
+
10
+ def extract_and_validate_version_from(migration_name)
11
+ version = migration_name.to_s.scan(VERSION_REGEXP).flatten.first
12
+ unless version
13
+ raise ArgumentError, 'migration version must be a 14 digits integer'
14
+ end
15
+
16
+ version.to_i
17
+ end
18
+ end
19
+
20
+ attr_reader :files_path, :current_version, :migrations
21
+
22
+ def initialize(files_path)
23
+ @files_path = files_path
24
+
25
+ load_migrations
26
+ end
27
+
28
+ # @param from a valid migration name or version
29
+ # @param to a valid migration name or version
30
+ def run(from: nil, to: nil)
31
+ # Extract and validate versions
32
+ from = self.class.extract_and_validate_version_from from
33
+ to = self.class.extract_and_validate_version_from to
34
+ # Select migrations to run
35
+ to_run, direction = migrations_to_run from, to
36
+ to_run.each do |migration|
37
+ migration.send direction
38
+ end
39
+ end
40
+
41
+ def load_migrations
42
+ # Load files from files_path and create classes
43
+ @migrations = Dir[File.join(File.expand_path(files_path), '**', '*.rb')].sort.map do |f_path|
44
+ migration_name = File.basename(f_path)
45
+ klass = Class.new(Base)
46
+ klass.class_eval(File.read(f_path), f_path)
47
+ klass.freeze
48
+ klass.new migration_name
49
+ end
50
+ end
51
+
52
+ # Select migrations to run depending given a starting and an ending one
53
+ def migrations_to_run(from, to)
54
+ selected = []
55
+ direction = from <= to ? :up : :down
56
+ @migrations.each do |m|
57
+ break if (from == to)
58
+ # Break if we already arrived to "from" migration
59
+ break if (from > to) && (m.version > from)
60
+ # Break if we already arrived to "to" migration
61
+ break if (from < to) && (m.version > to)
62
+ # Skip until we arrive to "to" migration
63
+ next if (from > to) && (m.version < to)
64
+ # Skip until we arrive to "from" migration
65
+ next if (from < to) && (m.version < from)
66
+
67
+ if m.version <= from
68
+ selected.prepend m
69
+ else
70
+ selected.append m
71
+ end
72
+ end
73
+
74
+ [selected, direction]
75
+ end
76
+ end
77
+ end
78
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json'
2
4
  require 'yaml'
3
5
  require 'logger'
@@ -28,6 +30,7 @@ module Fancybox2
28
30
  @logger = options.fetch :logger, create_default_logger
29
31
  @status = :stopped
30
32
  @alive_task = nil
33
+ @configs = {}
31
34
  end
32
35
 
33
36
  def alive_message_data(&block)
@@ -84,20 +87,12 @@ module Fancybox2
84
87
  @on_configs = block
85
88
  return
86
89
  end
87
- @configs = begin
88
- # Try to parse
89
- JSON.parse packet.payload
90
- rescue JSON::ParserError
91
- logger.debug 'on_configs: failed parsing packet as JSON, retrying with YAML'
92
- begin
93
- # Try to parse YAML
94
- YAML.load packet.payload
95
- rescue StandardError
96
- logger.debug 'on_configs: failed parsing packet as YAML. Falling back to raw payload'
97
- # Fallback to original content
98
- packet.payload
99
- end
100
- end
90
+
91
+ cfg = packet.payload
92
+ if cfg && cfg.is_a?(Hash) && cfg['configs']
93
+ self.configs.merge! cfg['configs']
94
+ end
95
+
101
96
  @on_configs.call(packet) if @on_configs
102
97
  end
103
98
 
@@ -140,6 +135,8 @@ module Fancybox2
140
135
  return
141
136
  end
142
137
 
138
+ @status = :on_shutdown
139
+
143
140
  shutdown_ok = true
144
141
  logger.debug "Received 'shutdown' command"
145
142
  # Stop sending alive messages
@@ -160,17 +157,20 @@ module Fancybox2
160
157
  message_to :core, :shutdown, { status: shutdown_message }
161
158
  sleep 0.05 # Wait some time in order to be sure that the message has been published (message is not mandatory)
162
159
 
163
- if mqtt_client && mqtt_client.connected?
164
- # Gracefully disconnect from broker and exit
165
- logger.debug 'Disconnecting from broker'
166
- mqtt_client.disconnect
167
- end
160
+ Thread.new do
161
+ if mqtt_client && mqtt_client.connected?
162
+ # Gracefully disconnect from broker and exit
163
+ logger.debug 'Disconnecting from broker, bye'
164
+ mqtt_client.disconnect
165
+ @mqtt_client = nil
166
+ end
168
167
 
169
- if do_exit
170
- # Exit from process
171
- status_code = shutdown_ok ? 0 : 1
172
- logger.debug "Exiting with status code #{status_code}"
173
- exit status_code
168
+ if do_exit
169
+ # Exit from process
170
+ status_code = shutdown_ok ? 0 : 1
171
+ logger.debug "Exiting with status code #{status_code}"
172
+ exit status_code
173
+ end
174
174
  end
175
175
  end
176
176
 
@@ -186,8 +186,8 @@ module Fancybox2
186
186
  # Call user code
187
187
  @on_start.call(packet) if @on_start
188
188
 
189
- configs = packet ? packet.payload : {}
190
- interval = configs['aliveTimeout'] || 1000
189
+ cfg = packet ? packet.payload : {}
190
+ interval = cfg['aliveTimeout'] || 1000
191
191
  # Start code execution from scratch
192
192
  logger.debug "Received 'start'"
193
193
  @status = :running
@@ -228,14 +228,17 @@ module Fancybox2
228
228
  def start_sending_alive(interval: 5000)
229
229
  # TODO: replace the alive interval task with Eventmachine?
230
230
  # Interval is expected to be msec, so convert it to secs
231
- interval /= 1000
231
+ interval /= 1000.0
232
232
  @alive_task.shutdown if @alive_task
233
233
  @alive_task = Concurrent::TimerTask.new(execution_interval: interval, timeout_interval: 2, run_now: true) do
234
- packet = { status: @status, lastSeen: Time.now.utc }
235
- if @alive_message_data
236
- packet[:data] = @alive_message_data.call
234
+ packet = { status: @status, lastSeen: Time.now.utc, data: nil }
235
+ begin
236
+ packet[:data] = alive_message_data
237
+ message_to :core, :alive, packet
238
+ rescue StandardError => e
239
+ logger.error "Error in alive_message_data callback: #{e.message}"
240
+ logger.error e.backtrace.join "\n"
237
241
  end
238
- message_to :core, :alive, packet
239
242
  end
240
243
  @alive_task.execute
241
244
  end
@@ -353,12 +356,12 @@ module Fancybox2
353
356
 
354
357
  def create_default_logger
355
358
  stdout_logger = ::Logger.new STDOUT
356
- broker_logger = ::Logger.new(Logger::MQTTLogDevice.new(topic_for(dest: :core, action: :logs),
357
- client: mqtt_client),
358
- formatter: Logger::JSONFormatter.new)
359
- logger = Logger::Multi.new stdout_logger, broker_logger,
360
- level: @log_level,
361
- progname: @log_progname
359
+ # broker_logger = ::Logger.new(Logger::MQTTLogDevice.new(topic_for(dest: :core, action: :logs),
360
+ # client: mqtt_client),
361
+ # formatter: Logger::JSONFormatter.new)
362
+ logger = Logger::Multi.new stdout_logger,# broker_logger,
363
+ level: @log_level,
364
+ progname: @log_progname
362
365
  logger
363
366
  end
364
367
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fancybox2
2
4
  module Module
3
5
  class Config
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fancybox2
2
4
  module Module
3
5
  module Exceptions
@@ -4,34 +4,34 @@ module Fancybox2
4
4
  extend self
5
5
 
6
6
  def identifier
7
- return @indentifier if @indentifier
7
+ return @identifier if @identifier
8
8
 
9
9
  host_os = RbConfig::CONFIG['host_os']
10
- case host_os
11
- when /aix(.+)$/
12
- 'aix'
13
- when /darwin(.+)$/
14
- 'darwin'
15
- when /linux/
16
- 'linux'
17
- when /freebsd(.+)$/
18
- 'freebsd'
19
- when /openbsd(.+)$/
20
- 'openbsd'
21
- when /netbsd(.*)$/
22
- 'netbsd'
23
- when /dragonfly(.*)$/
24
- 'dragonflybsd'
25
- when /solaris2/
26
- 'solaris2'
27
- when /mswin|mingw32|windows/
28
- # No Windows platform exists that was not based on the Windows_NT kernel,
29
- # so 'windows' refers to all platforms built upon the Windows_NT kernel and
30
- # have access to win32 or win64 subsystems.
31
- 'windows'
32
- else
33
- host_os
34
- end
10
+ @identifier = case host_os
11
+ when /aix(.+)$/
12
+ 'aix'
13
+ when /darwin(.+)$/
14
+ 'darwin'
15
+ when /linux/
16
+ 'linux'
17
+ when /freebsd(.+)$/
18
+ 'freebsd'
19
+ when /openbsd(.+)$/
20
+ 'openbsd'
21
+ when /netbsd(.*)$/
22
+ 'netbsd'
23
+ when /dragonfly(.*)$/
24
+ 'dragonflybsd'
25
+ when /solaris2/
26
+ 'solaris2'
27
+ when /mswin|mingw32|windows/
28
+ # No Windows platform exists that was not based on the Windows_NT kernel,
29
+ # so 'windows' refers to all platforms built upon the Windows_NT kernel and
30
+ # have access to win32 or win64 subsystems.
31
+ 'windows'
32
+ else
33
+ host_os
34
+ end
35
35
  end
36
36
  end
37
37
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fancybox2
2
- VERSION = '0.0.2'
4
+ VERSION = '0.0.7'
3
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fancybox2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessandro Verlato
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-15 00:00:00.000000000 Z
11
+ date: 2021-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk
@@ -66,6 +66,9 @@ files:
66
66
  - lib/fancybox2/logger/json_formatter.rb
67
67
  - lib/fancybox2/logger/mqtt_log_device.rb
68
68
  - lib/fancybox2/logger/multi.rb
69
+ - lib/fancybox2/migrations/base.rb
70
+ - lib/fancybox2/migrations/exceptions.rb
71
+ - lib/fancybox2/migrations/runner.rb
69
72
  - lib/fancybox2/module/base.rb
70
73
  - lib/fancybox2/module/config.rb
71
74
  - lib/fancybox2/module/exceptions.rb