asynr 1.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
+ SHA256:
3
+ metadata.gz: af65e3652194e677bf1e61997ad7e279a90f43922c0d0c55df91a104975fa94f
4
+ data.tar.gz: 890b147fb197d9d5926b2cf6f88afd1d9f3f50df672e27c06723069de9b19b10
5
+ SHA512:
6
+ metadata.gz: fb32e46c307a524d5376ce25a26458bde2c448952dee08efe94498116e3b32ea2c03d3572e7fb6ed125a3916938f77a16845a965f280d55adb1de6c1f8420b99
7
+ data.tar.gz: 40262c7d19e54bdaf196894c591d401585ccf0441bf014176886097ce7dadaaf29ae16cb0a8d8d452aee1a61295cd10ce87734728b603760b56211c5a18e4e1a
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.vendor/
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.5
7
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at gfrancois@online.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in asynr.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ asynr (1.0.1)
5
+ rufus-scheduler (~> 3.6.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ concurrent-ruby (1.1.5)
11
+ diff-lcs (1.3)
12
+ et-orbi (1.2.2)
13
+ tzinfo
14
+ fugit (1.3.3)
15
+ et-orbi (~> 1.1, >= 1.1.8)
16
+ raabro (~> 1.1)
17
+ raabro (1.1.6)
18
+ rake (10.5.0)
19
+ rspec (3.8.0)
20
+ rspec-core (~> 3.8.0)
21
+ rspec-expectations (~> 3.8.0)
22
+ rspec-mocks (~> 3.8.0)
23
+ rspec-core (3.8.2)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-expectations (3.8.4)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.8.0)
28
+ rspec-mocks (3.8.1)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.8.0)
31
+ rspec-support (3.8.2)
32
+ rufus-scheduler (3.6.0)
33
+ fugit (~> 1.1, >= 1.1.6)
34
+ tzinfo (2.0.0)
35
+ concurrent-ruby (~> 1.0)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ asynr!
42
+ bundler (~> 2.0)
43
+ rake (~> 10.0)
44
+ rspec (~> 3.0)
45
+ rufus-scheduler
46
+
47
+ BUNDLED WITH
48
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Gautier François
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # Asynr
2
+
3
+ A Ruby async scheduler based-upon Rufus::Scheduler gem.
4
+
5
+ # How to install
6
+
7
+ ```bash
8
+ $ gem install asynr
9
+ $ echo "gem 'asynr'" >> Gemfile
10
+ $ bundle install
11
+ ```
12
+
13
+ # How to use
14
+
15
+ ## Minimal setup
16
+
17
+ Asynr needs to know which class to be evaluated and scheduled. Require any class file before launching it.
18
+
19
+ ```ruby
20
+ require "asynr"
21
+ require_relative "./random_lib_with_class"
22
+ ...
23
+ ```
24
+
25
+ ## Add a new job
26
+
27
+ Jobs are minimal, since all Rufus methods aren't intented to be implemented. Here are the four parameters currently supported by Asynr.
28
+
29
+ * name: each job has a name which can be used to remove the concerned job.
30
+ * action: is the evaluated class name. Async will search for a run method inside of this class.
31
+ * first_in: directly taken from Rufus, time before the action starts.
32
+ * every: time between two class executions.
33
+
34
+ ## Remove a job
35
+
36
+ Jobs can be removed dynamically with their name:
37
+
38
+ ```ruby
39
+ ...
40
+ inst.job(name: :Test, action: :RandomTest, every: 10, first_in: 0)
41
+ inst.queue.length # 1
42
+ inst.remove :Test
43
+ inst.queue.length # 0
44
+ ...
45
+ ```
46
+
47
+ ## Start all jobs
48
+
49
+ Once launched, a new job cannot be defined and added. We need to set all the jobs and related timers before starting the scheduler.
50
+
51
+ ```ruby
52
+ require "asynr"
53
+
54
+ inst = Asynr.new
55
+ inst.job(name: :Test, action: RandomTest, every: 10, first_in: 0)
56
+ inst.job(name: :Again, action: AnotherTest, every: 3, first_in: 1)
57
+ inst.queue.length # 2
58
+
59
+ inst.start!
60
+ ```
61
+
62
+ # Working example
63
+
64
+ Since the logic behind it may confuse you regarding the class evaluation, here's a working example of how to use Asynr. Minimal yet functionnal.
65
+ \
66
+ This example will save a HTTP request launched every 60 seconds, for ever (until it is stopped).
67
+
68
+ ```ruby
69
+ require "asynr"
70
+ require "typhoeus"
71
+
72
+ class JSONFetcher
73
+ def self.run
74
+ res = Typhoeus::Request.get("https://httpbin.org/get")
75
+ f = File.open "./http_bin_get.log", "a+"
76
+ f.puts res.body
77
+ end
78
+ end
79
+
80
+ inst = Asynr.new
81
+ inst.job(name: :JSONFetcher, action: JSONFetcher, first_in: 1, every: 60)
82
+ inst.start!
83
+ ```
84
+
85
+ ## Optional architecture
86
+
87
+ With the previous example, we saw how Asynr works, but its initial goal is to allow us to create easy-to-develop softwares with integrated scheduler, such as API aggregator, or middlewares.
88
+ \
89
+ Here's a software architecture that works.
90
+
91
+ ```
92
+ Gemfile
93
+ scheduler.rb
94
+ worker/
95
+ - download_first_file.rb
96
+ - download_second_file.rb
97
+ - download_third_file.rb
98
+ lib/
99
+ - dependency.rb
100
+ ```
101
+
102
+ Asynr can easily handle all these executions every 60 seconds, for example.
103
+ \
104
+ Into scheduler.rb :
105
+ ```ruby
106
+ require "asynr"
107
+ Dir.glob(Dir.pwd + "/worker/*.rb").each &method(:require)
108
+
109
+ asynr = Asynr.new
110
+ asynr.job(name: :firstFile, action: DownloadFirstFile, first_in: 0, every: 60)
111
+ asynr.job(name: :secondFile, action: DownloadSecondFile, first_in: 0, every: 60)
112
+ asynr.job(name: :thirFile, action: DownloadThirdFile, first_in: 0, every: 60)
113
+
114
+ asynr.start!
115
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/asynr.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "asynr/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "asynr"
7
+ spec.version = Asynr::VERSION
8
+ spec.authors = ["Atille"]
9
+ spec.email = ["gfrancoisfrancois@outlook.com"]
10
+
11
+ spec.summary = %q{Async job planner based upon Rufus scheduler}
12
+ spec.description = %q{Async job planner based upon Rufus scheduler Ruby gem and threads, with history and states.}
13
+ spec.homepage = "https://rubygems.org/gems/asynr"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/atilleh/async"
20
+ spec.metadata["changelog_uri"] = "https://github.com/atilleh/async"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 2.0"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_development_dependency "rufus-scheduler"
35
+
36
+ spec.add_runtime_dependency 'rufus-scheduler', '~> 3.6.0'
37
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "asynr"
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
data/lib/asynr/core.rb ADDED
@@ -0,0 +1,52 @@
1
+ require "rufus-scheduler"
2
+
3
+ module Asynr
4
+ # Main gem class
5
+ class Core
6
+ # Creates a new Rufus Scheduler and waits to
7
+ # populate the local queue.
8
+ # This local queue is gonna be converted into a
9
+ # rufus queue.
10
+ def initialize(params={})
11
+ @params = params
12
+ @local_queue = []
13
+ @queue = Rufus::Scheduler.new
14
+ end
15
+
16
+ # @return .local_queue [Array] already registered but unlaunched jobs.
17
+ def queue
18
+ @local_queue
19
+ end
20
+
21
+ # Create a new job
22
+ # @see Job.new
23
+ # @return job [Object] newly created job object.
24
+ def job(options={})
25
+ job = Asynr::Job.new(options)
26
+ @local_queue.append job
27
+ job
28
+ end
29
+
30
+ # Removes a job from the local queue.
31
+ # Useless if the queue has already been converted by .dispatch!
32
+ # @see dispatch!
33
+ def remove(name)
34
+ @local_queue.delete_if {|i| i.name == name}
35
+ end
36
+
37
+ # Converts the local_queue to rufus queue
38
+ # execute it.
39
+ def start!
40
+ dispatch!
41
+ @queue.join
42
+ end
43
+
44
+ # dispatch! converts local queue to rufus every object queue.
45
+ protected
46
+ def dispatch!
47
+ @local_queue.each do |job|
48
+ @queue.every job.every, first_in: job.first_in do job.action.send(:run) ; end
49
+ end
50
+ end
51
+ end
52
+ end
data/lib/asynr/job.rb ADDED
@@ -0,0 +1,31 @@
1
+ module Asynr
2
+
3
+ # Class for job initialization
4
+ class Job
5
+ # Creates a new job instance, returned to Core library.
6
+ # @param options [Hash] job options before start! call
7
+ # @return nil
8
+ def initialize(options={first_in: 60, every: 60})
9
+ @options = options
10
+ end
11
+
12
+ # Setter/Getter of name option.
13
+ def name ; @options[:name] ; end
14
+ # @param name [Symbol] job short name
15
+ def name=(name) ; @options[:name] = name ; end
16
+
17
+ def action ; @options[:action] ; end
18
+ # @param action [Class] class instance to evaluate
19
+ def action=(action) ; @options[:action] = action ; end
20
+
21
+ # Time to wait before running a job twice
22
+ def every ; @options[:every] ; end
23
+ # @param every [Int] amount of seconds
24
+ def every=(every) ; @options[:every] = every ; end
25
+
26
+ # Time to wait before starting the first job occurence
27
+ def first_in ; @options[:first_in] ; end
28
+ # @param first_in [Int] amount of seconds
29
+ def first_in=(first_in) ; @options[:first_in] = first_in ; end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Asynr
2
+ VERSION = "1.0.1"
3
+ end
data/lib/asynr.rb ADDED
@@ -0,0 +1,18 @@
1
+ require "asynr/version"
2
+ require "asynr/core"
3
+ require "asynr/job"
4
+
5
+ module Asynr
6
+
7
+ # Shortcut returning Core object
8
+ # Will be called with Asynr.new instead of
9
+ # Asynr::Core.new
10
+ #
11
+ # @param params [Hash] defaults options
12
+ # @return self [Object] self from Asynr::Core
13
+ def self.new(params={})
14
+ Asynr::Core.new(params)
15
+ end
16
+
17
+ class Error < StandardError; end
18
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asynr
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Atille
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-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: '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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rufus-scheduler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rufus-scheduler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.6.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.6.0
83
+ description: Async job planner based upon Rufus scheduler Ruby gem and threads, with
84
+ history and states.
85
+ email:
86
+ - gfrancoisfrancois@outlook.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - asynr.gemspec
101
+ - bin/console
102
+ - bin/setup
103
+ - lib/asynr.rb
104
+ - lib/asynr/core.rb
105
+ - lib/asynr/job.rb
106
+ - lib/asynr/version.rb
107
+ homepage: https://rubygems.org/gems/asynr
108
+ licenses:
109
+ - MIT
110
+ metadata:
111
+ allowed_push_host: https://rubygems.org
112
+ homepage_uri: https://rubygems.org/gems/asynr
113
+ source_code_uri: https://github.com/atilleh/async
114
+ changelog_uri: https://github.com/atilleh/async
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.7.6.2
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Async job planner based upon Rufus scheduler
135
+ test_files: []