lock-redis 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/bin/lock +78 -0
  3. metadata +90 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: afcec811d4788df2192dc8442e16b481047d29a6
4
+ data.tar.gz: 1de2ca8c6878e1ef27909093f77dfacdb8e6a21e
5
+ SHA512:
6
+ metadata.gz: 1d3ae1f80d2c4619162d787849fc25c908f0d08475e7e9841648d6483c4b3fd0177405355cd1398d6d3c5d6f6efb06d9f04219982ef18c1a968ef41659b93f25
7
+ data.tar.gz: 6672ec5b2bbae6aac357c14cc71962cfc92e394f8ef2365463ba53e7bc3068c4e684eef1e256e1983caceaa37f6060b7e8ff35af842b290c9d03d10150f932af
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'redlock'
5
+
6
+ def close_std!
7
+ STDIN.reopen '/dev/null'
8
+ STDOUT.reopen '/dev/null'
9
+ STDERR.reopen '/dev/null'
10
+ end
11
+
12
+ redis_opts = { host: 'localhost', port: 6379, db: 0 }
13
+ redis_key = nil
14
+
15
+ opts = OptionParser.new do |opts|
16
+ opts.banner = "USAGE: #$0 [--host REDISHOST] [--port REDISPORT] [--db REDISDB] --key REDISKEY"
17
+
18
+ opts.on('--host REDISHOST', 'Redis Host, default: localhost') do |host|
19
+ redis_opts[:host] = host
20
+ end
21
+
22
+ opts.on('--port REDISPORT', Integer, 'Redis Port, default: 6379') do |port|
23
+ redis_opts[:port] = port
24
+ end
25
+
26
+ opts.on('--db REDISDB', Integer, 'Redis DB, default: 0') do |db|
27
+ redis_opts[:db] = db
28
+ end
29
+
30
+ opts.on('--key REDISKEY', 'Redis Key to Lock') do |key|
31
+ redis_key = key
32
+ end
33
+
34
+ opts.on_tail('--help', 'Show this message') do
35
+ puts opts
36
+ exit
37
+ end
38
+ end
39
+ opts.parse!
40
+
41
+ if redis_key.nil? || redis_key.size.zero?
42
+ puts opts
43
+ exit!
44
+ end
45
+
46
+ pidr, pidw = IO.pipe
47
+
48
+ child = fork do
49
+ pidr.close
50
+ fork do
51
+ fail 'Failed to detach from controlling terminal' unless Process.setsid
52
+ trap 'SIGHUP', 'IGNORE'
53
+ Dir.chdir '/'
54
+
55
+ close_std!
56
+ lock_manager = Redlock::Client.new(["redis://#{redis_opts[:host]}:#{redis_opts[:port]}/#{redis_opts[:db]}"],
57
+ retry_delay: 1_000, retry_count: 3_600)
58
+ lockinfo = nil
59
+ until lockinfo
60
+ lockinfo = lock_manager.lock(redis_key, 3_600_000)
61
+ end
62
+ pidw.puts Process.pid
63
+ pidw.close
64
+
65
+ sigr, sigw = IO.pipe
66
+ Signal.trap('TERM') { sigw.puts }
67
+
68
+ IO.select([sigr])
69
+ lock_manager.unlock lockinfo
70
+ end
71
+ pidw.close
72
+ close_std!
73
+ end
74
+
75
+ Process.detach(child)
76
+ grandchild = pidr.readline.to_i
77
+ puts grandchild
78
+ close_std!
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lock-redis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bachue Zhou
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redlock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: To lock a key of the Redis, then daemonize. If killed, it will release
56
+ the key.
57
+ email:
58
+ - bachue.shu@gmail.com
59
+ executables:
60
+ - lock
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - bin/lock
65
+ homepage: http://github.com/bachue/lock-redis
66
+ licenses:
67
+ - Commercial License
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.5.1
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: To lock a key of the Redis, then daemonize. If killed, it will release the
89
+ key.
90
+ test_files: []