davinci_threader 1.0.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: 993763e15a5925b9ddf6b573291691d90981c7e3
4
+ data.tar.gz: 6e6e4c2cc7c76a2109ac178bdd17fb357a09515f
5
+ SHA512:
6
+ metadata.gz: '01539586ff72cd4b619229a707efad51b6b61535608dab0c53acc4a49fb8529a32f52ce67efc918aa0942bc06c63b242eaae14ca217622b82c9168ad6f162178'
7
+ data.tar.gz: b158a7beebb8f78efcaf283fec4384796c367f710f6e463f177f4f4e4842c4d5dd6165c64039b1f68d6326957a13998f7beb50e3b2c16f8c4112654405e0201b
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # DavinciThreader
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/davinci_threader`. 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 'davinci_threader'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install davinci_threader
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. 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]/davinci_threader. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the DavinciThreader project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/davinci_threader/blob/master/CODE_OF_CONDUCT.md).
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "davinci_threader"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ require 'davinci_threader/version'
2
+ require 'davinci_threader/main'
3
+
4
+
5
+ module DavinciThreader
6
+ end
@@ -0,0 +1,165 @@
1
+ require 'colorize'
2
+ require 'active_support/core_ext/numeric/time'
3
+
4
+
5
+ module DavinciThreader
6
+ class Main
7
+
8
+
9
+ # Attributes
10
+ # ======================================================
11
+ def rpm=(value)
12
+ @rpm = value
13
+ self.max_threads = value*0.03
14
+ end
15
+ def rpm
16
+ 1.minute.to_f / @rpm.to_f
17
+ end
18
+ attr_accessor :log
19
+ attr_accessor :log_errors
20
+ attr_accessor :total
21
+ attr_accessor :successful
22
+ attr_accessor :errors
23
+ attr_accessor :extras
24
+ attr_accessor :max_threads
25
+ attr_accessor :asynchronous
26
+ attr_reader :thread_count
27
+ # ======================================================
28
+
29
+ # Initialize
30
+ # ======================================================
31
+ def initialize
32
+
33
+ self.log = true
34
+ self.log_errors = true
35
+ self.rpm = 60
36
+ self.total = 0
37
+ self.successful = 0
38
+ self.errors = 0
39
+ self.extras = []
40
+ self.max_threads = 10
41
+ self.asynchronous = true
42
+ @threads = []
43
+ @thread_count = 0
44
+
45
+ @monitor = Thread.new do
46
+ while self.log
47
+ printout if @start_time
48
+ sleep 0.1
49
+ end
50
+ end
51
+
52
+ yield self
53
+
54
+ @threads.each &:join
55
+ sleep 1
56
+ @monitor.exit
57
+ puts "\n-> Done!".light_green if self.log
58
+
59
+ rescue Interrupt
60
+
61
+ @monitor.exit
62
+
63
+ begin
64
+ puts "\n-> Waiting for remaining threads to finish...".yellow
65
+ @threads.each(&:join)
66
+ puts "-> Exited!".yellow
67
+ rescue Interrupt
68
+ force_exit
69
+ end
70
+
71
+ exit
72
+
73
+ end
74
+ # ======================================================
75
+
76
+
77
+ # Make Thread
78
+ # ======================================================
79
+ def make_thread *args
80
+
81
+ @start_time ||= Time.now
82
+
83
+ if !self.asynchronous
84
+ yield(*args)
85
+ self.printout
86
+ return
87
+ end
88
+
89
+ @threads << Thread.new(*args) do
90
+ begin
91
+ @thread_count += 1
92
+ yield(*args)
93
+ @thread_count -= 1
94
+ rescue => e
95
+ @thread_count -= 1
96
+ self.errors += 1
97
+ if self.log_errors
98
+ output = [e.message.light_red]
99
+ output += e.backtrace.map(&:yellow)
100
+ print "\n#{output.join("\n")}\n"
101
+ end
102
+ end
103
+ end
104
+
105
+ sleep self.rpm
106
+ sleep 0.1 while self.thread_count > self.max_threads
107
+
108
+ @threads.each_with_index do |t,i|
109
+ if !t.alive?
110
+ t.exit
111
+ @threads.delete_at(i)
112
+ end
113
+ end
114
+
115
+ end
116
+ # ======================================================
117
+
118
+ # Printout
119
+ # ======================================================
120
+ def printout
121
+
122
+ c = Time.now - @start_time
123
+ output = []
124
+ if self.asynchronous
125
+ output << "#{"#{@rpm.to_i}/MIN".cyan}"
126
+ output << "#{Time.at(c.to_f).utc.strftime("%H:%M:%S")}"
127
+ if self.completed > 100
128
+ actual_rate = (self.completed.to_f / c.to_f) * 60.0
129
+ output << "#{actual_rate.round}/MIN".light_green
130
+ output << "#{Time.at(((self.total-self.completed).to_f / actual_rate) * 60.0).utc.strftime("%H:%M:%S")}".light_green
131
+ end
132
+ end
133
+ output << "#{self.completed}/#{self.total} (#{self.successful.to_s.light_green} <--> #{self.errors.to_s.light_red})"
134
+ output += self.extras
135
+ print "\r#{output.join(" :: ".yellow)} "
136
+
137
+ end
138
+ # ======================================================
139
+
140
+ # Increment Methods
141
+ # ======================================================
142
+ def success
143
+ self.successful += 1
144
+ end
145
+ def error
146
+ self.errors += 1
147
+ end
148
+ def completed
149
+ self.errors+self.successful
150
+ end
151
+ # ======================================================
152
+
153
+ # Force Exit
154
+ # ======================================================
155
+ def force_exit
156
+ @monitor.try(:exit)
157
+ puts "\n-> Killing remaining threads...".light_red
158
+ @threads.each(&:exit)
159
+ puts "-> Forced Exit!".light_red
160
+ end
161
+ # ======================================================
162
+
163
+
164
+ end
165
+ end
@@ -0,0 +1,3 @@
1
+ module DavinciThreader
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: davinci_threader
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - onlyexcellence
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-11 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
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: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: colorize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.1
69
+ description: Useful for APIs that limit requests
70
+ email:
71
+ - will@wambl.com
72
+ executables:
73
+ - console
74
+ - setup
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - README.md
79
+ - bin/console
80
+ - bin/setup
81
+ - lib/davinci_threader.rb
82
+ - lib/davinci_threader/main.rb
83
+ - lib/davinci_threader/version.rb
84
+ homepage: https://github.com/onlyexcellence/davinci-theader
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.6.13
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Thread Throttling
108
+ test_files: []