elected 0.1.0 → 0.2.0

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: 09c82b9bd8412effb3d88ad09894e6c5d7739868
4
- data.tar.gz: 44a5123b80fad1ca635b655855ee040855075565
3
+ metadata.gz: 8b5c89ac46abb1a6ea65b8920deceef50191c861
4
+ data.tar.gz: 671056c5313758131924eb017b72c7c94755072f
5
5
  SHA512:
6
- metadata.gz: 30fa980526ecc9f8d6280c7c22cd4143a317cb271622f613946a79ff1a71ad00897d013a90712742ec0b67bf7beba1699693ea8e12500333ff1de681bd3588a6
7
- data.tar.gz: 3e72ef5c02f94b9269bd32f96f320689c565dc81cda4bd51b29b60fd1967f6c01b6429295eab57223e6653aad2560557f93d5bd1b897c45c27bb7cce820be9e1
6
+ metadata.gz: 3ec2c9051972ec2bbbfccc9d5fb8dca3e310dd610edaa3bdda0cc9668b039b0f73a8c522a5f92c8e01f4eabec1031c77a511dd10b90b6977af28ffc17ee06ccb
7
+ data.tar.gz: 844371981fbede73461956dcd1c87e46c6b09a06fb45037e2d3a19dfab9feb14f0536b5736eeb072e245d1b373875d7966cc49e578f7108aa405638b0fce4979
data/.travis.yml CHANGED
@@ -1,4 +1,27 @@
1
+ before_install: gem install bundler -v 1.10.6
2
+
1
3
  language: ruby
4
+ jdk:
5
+ - oraclejdk8
2
6
  rvm:
3
- - 2.0.0
4
- before_install: gem install bundler -v 1.10.6
7
+ - ruby-2.0.0-p598
8
+ - ruby-2.2.0
9
+ - jruby-9.0.0.0
10
+ - jruby-head
11
+ services:
12
+ - redis-server
13
+ env:
14
+ global:
15
+ - REDIS_URL="redis://localhost:6379/0"
16
+ - JRUBY_OPTS="--server -J-Dfile.encoding=utf8 --2.0"
17
+ install:
18
+ - bundle install --jobs=3 --retry=3
19
+ script:
20
+ - bundle exec rspec
21
+ notifications:
22
+ email:
23
+ recipients:
24
+ - aemadrid@gmail.com
25
+ on_success: change
26
+ on_failure: change
27
+ sudo: false
data/README.md CHANGED
@@ -1,3 +1,8 @@
1
+ [![Build Status](https://travis-ci.org/simple-finance/elected.svg?branch=master)](https://travis-ci.org/simple-finance/elected)
2
+ [![Code Climate](https://codeclimate.com/github/simple-finance/elected/badges/gpa.svg)](https://codeclimate.com/github/simple-finance/elected)
3
+ [![security](https://hakiri.io/github/simple-finance/elected/master.svg)](https://hakiri.io/github/simple-finance/elected/master)
4
+ [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/simple-finance/elected?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
5
+
1
6
  # Elected - A ruby distributed leader election through Redis locks.
2
7
 
3
8
  > Elect a leader out of many processes and run code only in one of your servers at a time.
@@ -61,17 +66,23 @@ Make sure you have at least 1 redis instances up.
61
66
 
62
67
  ## Disclaimer
63
68
 
64
- This code, like Redlock in which is based, implements an algorithm which is currently a proposal, it was not formally analyzed. Make sure to understand how it works before using it in your production environments. You can see discussion about this approach at [reddit](http://www.reddit.com/r/programming/comments/2nt0nq/distributed_lock_using_redis_implemented_in_ruby/).
69
+ The hard work of securing a distributed lock is all done through the great [redlock](https://github.com/leandromoreira/redlock-rb) gem. Thanks to [Leandro Moreira](https://github.com/leandromoreira) for his hard work.
70
+ This code, thanks to Redlock, implements an algorithm which is currently a proposal, it was not formally analyzed.
71
+ Make sure to understand how it works before using it in your production environments.
72
+ You can see discussion about this approach at [reddit](http://www.reddit.com/r/programming/comments/2nt0nq/distributed_lock_using_redis_implemented_in_ruby/).
65
73
 
66
74
  ## Development
67
75
 
68
- 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.
76
+ After checking out the repo, run `bin/setup` to install dependencies.
77
+ Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
78
 
70
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
79
+ To install this gem onto your local machine, run `bundle exec rake install`.
80
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
71
81
 
72
82
  ## Contributing
73
83
 
74
- Bug reports and pull requests are welcome on GitHub at https://github.com/simple-finance/elected. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/simple-finance/elected.
85
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
75
86
 
76
87
 
77
88
  ## License
data/lib/elected/lider.rb CHANGED
@@ -3,6 +3,8 @@ require 'yaml'
3
3
  module Elected
4
4
  class Lider
5
5
 
6
+ include Logging
7
+
6
8
  attr_reader :info, :validity, :resource, :value, :created, :deadline, :diff
7
9
 
8
10
  def initialize(info, timeout)
@@ -0,0 +1,92 @@
1
+ require 'logger'
2
+
3
+ module Elected
4
+ module Logging
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ def debug_var(var_name, var, meth = :inspect, level = :debug)
13
+ msg = var.nil? ? 'NIL' : var.send(meth)
14
+ msg = msg[0..-2] if msg[-1, 1] == "\n"
15
+ klass = var.is_a?(Class) ? var.name : var.class.name
16
+ Elected.logger.send level, "#{log_prefix} #{var_name} : (#{klass}:#{var.object_id}) #{msg}"
17
+ end
18
+
19
+ def log(string, type = :debug)
20
+ msg = "#{log_prefix} #{string}"
21
+ Elected.logger.send type, msg
22
+ end
23
+
24
+ def debug(string)
25
+ log string, :debug
26
+ end
27
+
28
+ def info(string)
29
+ log string, :info
30
+ end
31
+
32
+ def warn(string)
33
+ log string, :warn
34
+ end
35
+
36
+ def error(string)
37
+ log string, :error
38
+ end
39
+
40
+ def log_prefix(length = 60)
41
+ prefixes = [name]
42
+ label = log_prefix_labels.last
43
+ prefixes << label.rjust(length, ' ')[-length, length]
44
+ prefixes << ' | '
45
+ prefixes.join('')
46
+ end
47
+
48
+ def run_thread_count
49
+ Thread.list.select { |thread| thread.status == 'run' }.count
50
+ end
51
+
52
+ def thread_count
53
+ Thread.list.count
54
+ end
55
+
56
+ def log_prefix_labels
57
+ caller.
58
+ reject { |x| x.index(__FILE__) }.
59
+ map { |x| x =~ /(.*):(.*):in `(.*)'/ ? [$1, $2, $3] : nil }.
60
+ first
61
+ end
62
+
63
+ end
64
+
65
+ def debug(string)
66
+ self.class.debug string
67
+ end
68
+
69
+ def debug_var(name, var, meth = :inspect, level = :debug)
70
+ self.class.debug_var name, var, meth, level
71
+ end
72
+
73
+ def info(string)
74
+ self.class.info string
75
+ end
76
+
77
+ def warn(string)
78
+ self.class.warn string
79
+ end
80
+
81
+ def error(string)
82
+ self.class.error string
83
+ end
84
+ end
85
+
86
+ extend self
87
+
88
+ attr_accessor :logger
89
+
90
+ self.logger = Logger.new(STDOUT).tap { |x| x.level = Logger::INFO } if logger.nil?
91
+
92
+ end
@@ -3,6 +3,8 @@ require 'thread'
3
3
  module Elected
4
4
  class Senado
5
5
 
6
+ include Logging
7
+
6
8
  attr_accessor :key, :timeout
7
9
 
8
10
  def initialize(key = nil, timeout = nil)
@@ -1,3 +1,3 @@
1
1
  module Elected
2
- VERSION = "0.1.0"
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/elected.rb CHANGED
@@ -33,5 +33,6 @@ module Elected
33
33
 
34
34
  end
35
35
 
36
+ require 'elected/logging'
36
37
  require 'elected/lider'
37
38
  require 'elected/senado'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elected
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Madrid
@@ -114,6 +114,7 @@ files:
114
114
  - elected.gemspec
115
115
  - lib/elected.rb
116
116
  - lib/elected/lider.rb
117
+ - lib/elected/logging.rb
117
118
  - lib/elected/senado.rb
118
119
  - lib/elected/version.rb
119
120
  homepage: https://github.com/simple-finance/elected