codefly 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bdf8a7ed23f0e857dcd2c7cf51460b2ecb4ec80d
4
+ data.tar.gz: 7101eef0854af60b7496ce6553eb8ebc695c8181
5
+ SHA512:
6
+ metadata.gz: 0999928e52779c0bca027a1de8f432c1f39b12c2f67a8c9a806b49e4ab9f17728fe9e55d3e8a7f69d3e88c1c3ecdcc145f95afd9f53bb473d0365f032f26aa0d
7
+ data.tar.gz: a0373f0823745f21759b5fa1b7b8bb0054e7803cf6104aabe30beca9d7a1932e80b98ea27b1a6a0477536ac6ef1a9a4ad762518d4cc9f97e0e79f8cb5ad46b83
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in codefly.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Daniel Loureiro
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ CodeFly is a gem that allows you to execute parallel / asynchronous code in Rails, in a easy way.
2
+
3
+ This is how a classic code looks like:
4
+ ```ruby
5
+ blue_cars = Vehicle.where(type: "car", color: "blue") # 1 sec
6
+ accessories = Accessory.where(type: "car") # 1 sec
7
+ some_code(blue_cars, accessories) # 0.5 sec
8
+ ```
9
+
10
+ This is how a flied code looks like:
11
+ ```ruby
12
+ fly(:A) { blue_cars = Vehicle.where(type: "car", color: "blue") } # 1 sec
13
+ fly(:B) { accessories = Accessory.where(type: "car") } # 1 sec
14
+ wait_fly(:A, :B)
15
+ some_code(blue_cars, accessories) # 0.5 sec
16
+ ```
17
+
18
+ The first code will take 2.5 sec, but the second will take only 1.5, almost the twice of speed. On the second code, the A and B queries will be executed at the same time (I will assume that your DB have more than 1 core), and the Ruby will be free to execute your own code while these 2 queries are being processed.
19
+
20
+ The CodeFly methods are:
21
+
22
+ fly(id) { async code }
23
+ wait_fly(id_1[, id_2, …]): block until the async codes terminate
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/codefly.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'codefly/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "codefly"
8
+ spec.version = Codefly::VERSION
9
+ spec.authors = ["Daniel Loureiro"]
10
+ spec.email = ["loureirorg@gmail.com"]
11
+ spec.summary = %q{Parallel / async code in Rails}
12
+ spec.description = %q{CodeFly is a gem that allows you to execute parallel / asynchronous code in Rails, in a easy way.}
13
+ spec.homepage = "https://github.com/loureirorg/codefly"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,3 @@
1
+ module Codefly
2
+ VERSION = "0.0.1"
3
+ end
data/lib/codefly.rb ADDED
@@ -0,0 +1,51 @@
1
+ require "codefly/version"
2
+
3
+ # we need to hack ActiveRecord to read :connection variable from the thread
4
+ module ActiveRecord
5
+ module ConnectionAdapters
6
+ class ConnectionHandler
7
+ def retrieve_connection(klass) #:nodoc:
8
+ pool = retrieve_connection_pool(klass)
9
+ raise ConnectionNotEstablished, "No connection pool for #{klass}" unless pool
10
+ conn = if Thread.current[:connection].blank?
11
+ pool.connection
12
+ else
13
+ if Thread.current[:connection] == :new
14
+ ActiveRecord::Base.connection_pool.checkout()
15
+ else
16
+ Thread.current[:connection]
17
+ end
18
+ end
19
+ raise ConnectionNotEstablished, "No connection for #{klass} in connection pool" unless conn
20
+ conn
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ @fly_thrs = {}
27
+
28
+ def fly(id)
29
+ return unless block_given?
30
+ raise 'ID already in use' if @fly_thrs.has_key? id
31
+ @fly_thrs[id] = Thread.new do
32
+ Thread.current[:connection] = :new
33
+ yield
34
+ ActiveRecord::Base.connection_pool.checkin(Thread.current[:connection]) if Thread.current[:connection] != :new
35
+ end
36
+ end
37
+
38
+ def wait_fly(*ids)
39
+ ids.each do |id|
40
+ if @fly_thrs[id].present?
41
+ begin
42
+ @fly_thrs[id].value # wait until ends
43
+ ensure
44
+ @fly_thrs.delete(id) # remove from threads pool
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ # module CodeFly
51
+ # end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codefly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Loureiro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-07 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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
+ description: CodeFly is a gem that allows you to execute parallel / asynchronous code
42
+ in Rails, in a easy way.
43
+ email:
44
+ - loureirorg@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - codefly.gemspec
55
+ - lib/codefly.rb
56
+ - lib/codefly/version.rb
57
+ homepage: https://github.com/loureirorg/codefly
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.4.5
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Parallel / async code in Rails
81
+ test_files: []