background_proxy 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c284b354b76f9161cdf34e9e85b5a4b93597aec5
4
+ data.tar.gz: 676b8fbe4d05b2dd0f2eb65622892316f3d3d0ae
5
+ SHA512:
6
+ metadata.gz: a972f69b934c0375c53199aceac53624a974280f89ff93358f66b683a236ccf4006e1928668cf69d998aa8e7b04aa7863126dabbdd6a4ef683b3417b42f96292
7
+ data.tar.gz: 8bf09457fd619f31ab3406130df0b6e30f20052b4bc104d6b255e599e79926faac26d91cefbe0e7fb59a573e4117fd7c4ecfaac57b53de9b7c8b677b40d985af
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in background_proxy.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # BackgroundProxy
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/background_proxy`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'background_proxy'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install background_proxy
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/background_proxy.
36
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'background_proxy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "background_proxy"
8
+ spec.version = BackgroundProxy::VERSION
9
+ spec.authors = ["Jeff Ching"]
10
+ spec.email = ["ching.jeff@gmail.com"]
11
+
12
+ spec.summary = %q{Generic wrapper for a client library to call methods asynchronously}
13
+ spec.description = %q{Generic wrapper for a client library to call methods asynchronously}
14
+ spec.homepage = "https://github.com/chingor13/background_proxy"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.10"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "minitest"
32
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "background_proxy"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+ module BackgroundProxy
2
+ class Proxy
3
+
4
+ def initialize(client, options = {})
5
+ @client = client
6
+ @options = options
7
+ end
8
+
9
+ private
10
+
11
+ attr_reader :client, :options
12
+
13
+ def worker
14
+ @worker ||= begin
15
+ Worker.new(client, options)
16
+ end
17
+ end
18
+
19
+ def method_missing(method, *args)
20
+ if client.respond_to?(method)
21
+ worker.async(method, *args)
22
+ else
23
+ super
24
+ end
25
+ end
26
+
27
+ def respond_to_missing?(method)
28
+ client.respond_to?(method) || super
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module BackgroundProxy
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,72 @@
1
+ module BackgroundProxy
2
+ class Worker
3
+ SLEEP_INTERVAL = 5
4
+
5
+ def initialize(client, options = {})
6
+ @queue = Queue.new
7
+ @client = client
8
+ @logger = options[:logger]
9
+ spawn_threads!(options.fetch(:threads, 3))
10
+
11
+ at_exit do
12
+ check_background_queue until @queue.empty?
13
+ end
14
+ end
15
+
16
+ def current_threads
17
+ Thread.list.select {|t| t[:background_logger] == self.object_id}
18
+ end
19
+
20
+ def current_thread_count
21
+ Thread.list.count {|t| t[:background_logger] == self.object_id}
22
+ end
23
+
24
+ def stopped?
25
+ !!@stopped
26
+ end
27
+
28
+ def stop!
29
+ @stopped = true
30
+ end
31
+
32
+ def async(method, *args)
33
+ log :debug, "async run: #{method}, #{args}"
34
+ @queue.push({
35
+ method: method,
36
+ args: args
37
+ })
38
+ end
39
+
40
+ def spawn_threads!(num_threads)
41
+ num_threads.times do |thread_num|
42
+ log :debug, "Spawning background worker thread #{thread_num}."
43
+
44
+ Thread.new do
45
+ Thread.current[:background_logger] = self.object_id
46
+
47
+ while !stopped?
48
+ self.check_background_queue(thread_num)
49
+ sleep rand(SLEEP_INTERVAL)
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ def check_background_queue(thread_num = 0)
56
+ log :debug, "Checking background queue on thread #{thread_num} (#{self.current_thread_count} active)"
57
+
58
+ while !@queue.empty?
59
+ p = @queue.pop(true) rescue next;
60
+ @client.send(p[:method], *p[:args])
61
+ end
62
+
63
+ end
64
+
65
+ def log(level, msg)
66
+ return unless @logger
67
+ @logger.log(level, msg)
68
+ end
69
+
70
+ end
71
+
72
+ end
@@ -0,0 +1,6 @@
1
+ require "background_proxy/version"
2
+
3
+ module BackgroundProxy
4
+ autoload :Proxy, "background_proxy/proxy"
5
+ autoload :Worker, "background_proxy/worker"
6
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: background_proxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Ching
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-30 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Generic wrapper for a client library to call methods asynchronously
56
+ email:
57
+ - ching.jeff@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - background_proxy.gemspec
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/background_proxy.rb
71
+ - lib/background_proxy/proxy.rb
72
+ - lib/background_proxy/version.rb
73
+ - lib/background_proxy/worker.rb
74
+ homepage: https://github.com/chingor13/background_proxy
75
+ licenses: []
76
+ metadata:
77
+ allowed_push_host: https://rubygems.org
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.8
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Generic wrapper for a client library to call methods asynchronously
98
+ test_files: []
99
+ has_rdoc: