fancybox2 0.0.3 → 0.0.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: b5f1b63c49186562a7754d3a1674d201202f8cfa2fa83b009d148b8036514a41
4
- data.tar.gz: d91b6945428ab9b366ecd29dcf4f56eced667f129592cf48b39888bd114b360f
3
+ metadata.gz: 7d6fec49dcd588ebb60bc82f555240121134b3fcfc40747b63513795f50adf96
4
+ data.tar.gz: 5d683438d83ad32f12db66ab811da55864e3c0f801f6fb0eb7620d5bb35432a8
5
5
  SHA512:
6
- metadata.gz: 6f03ac7a8f28b7dc5ce77ce4e77e71f623a94a5ccfa30bb472d4408e8c0cb5d07334166864474c514f4f72f74f9010b8aa7a1b0f785a87e93845af986f6113d5
7
- data.tar.gz: e5df22e0bc8a716cc16cbfe0d0c696560ead8923b9bb9b41b431284e11574fe389ce9980e54943cc8dc314266c9761c010a626945206f7c5368214a88241a0e0
6
+ metadata.gz: 22dcbb5bc7e731cc338ade1afe6023b706230526abeafef56655fe863f0e3314a622c8bab91eb3a2b788219026acb8151651be6deed4f4a616dfde5780338def
7
+ data.tar.gz: 49e4e79a91a552d4cb130c7c8cce4e037d6cf7c3314d38abaffc75167d3c1fac83fb0384ae850860df11c87de46e5caf278a0442df1e9f2e939b4cbfc1e71f19
@@ -13,10 +13,13 @@ module Fancybox2
13
13
  unless @client.respond_to?(:publish)
14
14
  raise ArgumentError, "provided client does not respond to 'publish'"
15
15
  end
16
+ unless @client.respond_to?(:connected?)
17
+ raise ArgumentError, "provided client does not respond to 'connected?'"
18
+ end
16
19
  end
17
20
 
18
21
  def write(message)
19
- if @client.connected?
22
+ if @client && @client.connected?
20
23
  @client.publish @topic, message
21
24
  end
22
25
  end
@@ -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+)/.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 name must start with an integer number e.g: 02_do_something.rb'
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')].map do |file_path|
44
+ migration_name = File.basename(file_path)
45
+ klass = Class.new(Base)
46
+ klass.class_eval(File.read(file_path), file_path)
47
+ klass.freeze
48
+ klass.new migration_name
49
+ end.sort_by { |migration| migration.version }
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
@@ -30,6 +30,7 @@ module Fancybox2
30
30
  @logger = options.fetch :logger, create_default_logger
31
31
  @status = :stopped
32
32
  @alive_task = nil
33
+ @configs = {}
33
34
  end
34
35
 
35
36
  def alive_message_data(&block)
@@ -86,20 +87,12 @@ module Fancybox2
86
87
  @on_configs = block
87
88
  return
88
89
  end
89
- @configs = begin
90
- # Try to parse
91
- JSON.parse packet.payload
92
- rescue JSON::ParserError
93
- logger.debug 'on_configs: failed parsing packet as JSON, retrying with YAML'
94
- begin
95
- # Try to parse YAML
96
- YAML.load packet.payload
97
- rescue StandardError
98
- logger.debug 'on_configs: failed parsing packet as YAML. Falling back to raw payload'
99
- # Fallback to original content
100
- packet.payload
101
- end
102
- end
90
+
91
+ cfg = packet.payload
92
+ if cfg && cfg.is_a?(Hash) && cfg['configs']
93
+ self.configs.merge! cfg['configs']
94
+ end
95
+
103
96
  @on_configs.call(packet) if @on_configs
104
97
  end
105
98
 
@@ -142,6 +135,8 @@ module Fancybox2
142
135
  return
143
136
  end
144
137
 
138
+ @status = :on_shutdown
139
+
145
140
  shutdown_ok = true
146
141
  logger.debug "Received 'shutdown' command"
147
142
  # Stop sending alive messages
@@ -162,17 +157,20 @@ module Fancybox2
162
157
  message_to :core, :shutdown, { status: shutdown_message }
163
158
  sleep 0.05 # Wait some time in order to be sure that the message has been published (message is not mandatory)
164
159
 
165
- if mqtt_client && mqtt_client.connected?
166
- # Gracefully disconnect from broker and exit
167
- logger.debug 'Disconnecting from broker'
168
- mqtt_client.disconnect
169
- 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
170
167
 
171
- if do_exit
172
- # Exit from process
173
- status_code = shutdown_ok ? 0 : 1
174
- logger.debug "Exiting with status code #{status_code}"
175
- 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
176
174
  end
177
175
  end
178
176
 
@@ -188,8 +186,8 @@ module Fancybox2
188
186
  # Call user code
189
187
  @on_start.call(packet) if @on_start
190
188
 
191
- configs = packet ? packet.payload : {}
192
- interval = configs['aliveTimeout'] || 1000
189
+ cfg = packet ? packet.payload : {}
190
+ interval = cfg['aliveTimeout'] || 1000
193
191
  # Start code execution from scratch
194
192
  logger.debug "Received 'start'"
195
193
  @status = :running
@@ -358,12 +356,12 @@ module Fancybox2
358
356
 
359
357
  def create_default_logger
360
358
  stdout_logger = ::Logger.new STDOUT
361
- broker_logger = ::Logger.new(Logger::MQTTLogDevice.new(topic_for(dest: :core, action: :logs),
362
- client: mqtt_client),
363
- formatter: Logger::JSONFormatter.new)
364
- logger = Logger::Multi.new stdout_logger, broker_logger,
365
- level: @log_level,
366
- 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
367
365
  logger
368
366
  end
369
367
 
@@ -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,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fancybox2
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.8'
5
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.3
4
+ version: 0.0.8
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-19 00:00:00.000000000 Z
11
+ date: 2021-02-05 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