sidekiq-repeating-jobs 0.0.4

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: 7ab3a25387fe420a992cbfbce90e2e0a3c3e772b
4
+ data.tar.gz: e4e0ba152095b7554676812c9d80993f7d2fa9a9
5
+ SHA512:
6
+ metadata.gz: fbd1f976e6149daa3904aa9effd806d5c25cad954b743e6a88c05048fa4ffcf9e44442f57c80dd8954fa5114c74631327d58bd1e9a57eb69d418229dbd8cfd49
7
+ data.tar.gz: 9597438f606f5480af8c2c0ac3ae7df305e353a62ae098016daddf2795ca6bb966fb9748645313391ff71c6e5033539476cedc57522dc56156125ff7e2bb52bb
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .idea
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sidekiq-repeating-jobs.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Marc Lennox
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,29 @@
1
+ # Sidekiq::Repeating::Jobs
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sidekiq-repeating-jobs'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sidekiq-repeating-jobs
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ require "yaml" if RUBY_VERSION.include?('2.0.0')
2
+ require "sidekiq_repeating_jobs/version"
3
+ require "sidekiq_repeating_jobs/middleware"
4
+ require "sidekiq_repeating_jobs/version"
5
+ require "sidekiq_repeating_jobs/config"
6
+ require "sidekiq_repeating_jobs/worker_helper"
7
+
8
+ module SidekiqRepeatingJobs
9
+
10
+ def self.config
11
+ SidekiqRepeatingJobs::Config
12
+ end
13
+
14
+ end
@@ -0,0 +1,30 @@
1
+ module SidekiqRepeatingJobs
2
+
3
+ class Config
4
+
5
+ DEFAULT_EXPIRATION_SECONDS=30*60
6
+ DEFAULT_REPEAT_SECONDS=60
7
+
8
+ class << self
9
+
10
+ def active?(worker_class)
11
+ worker_class.get_sidekiq_options['repeat'].present?
12
+ end
13
+
14
+ def job_name(worker_class)
15
+ worker_class.get_sidekiq_options['repeating_job_name'] || worker_class.name.demodulize.underscore
16
+ end
17
+
18
+ def repeat_interval(worker_class)
19
+ (r = worker_class.get_sidekiq_options['repeat']).is_a?(Fixnum) ? r : DEFAULT_REPEAT_SECONDS
20
+ end
21
+
22
+ def expiration_interval(worker_class)
23
+ self.repeat_interval(worker_class) + (worker_class.get_sidekiq_options['repeating_job_expiration'] || DEFAULT_EXPIRATION_SECONDS)
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,16 @@
1
+ require 'sidekiq'
2
+ require 'sidekiq_repeating_jobs/middleware/helpers'
3
+
4
+ Sidekiq.configure_server do |config|
5
+ config.server_middleware do |chain|
6
+ require 'sidekiq_repeating_jobs/middleware/server/repeating_jobs'
7
+ chain.add SidekiqRepeatingJobs::Middleware::Server::RepeatingJobs
8
+ end
9
+ end
10
+
11
+ Sidekiq.configure_client do |config|
12
+ config.client_middleware do |chain|
13
+ require 'sidekiq_repeating_jobs/middleware/client/repeating_jobs'
14
+ chain.add SidekiqRepeatingJobs::Middleware::Client::RepeatingJobs
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ module SidekiqRepeatingJobs
2
+ module Middleware
3
+ module Client
4
+ class RepeatingJobs
5
+ include SidekiqRepeatingJobs::Middleware::Helpers
6
+
7
+ def log_name
8
+ "RepeatingJobClient"
9
+ end
10
+
11
+ def call(worker_class, item, queue)
12
+ if SidekiqRepeatingJobs::Config.active?(worker_class)
13
+ yield if valid_arguments?(worker_class, item['args'].last)
14
+ else
15
+ yield
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,103 @@
1
+ module SidekiqRepeatingJobs
2
+ module Middleware
3
+ module Helpers
4
+
5
+ module ClassMethods
6
+ end
7
+
8
+ def self.included(base)
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ def log_message(args, message, level=:info)
13
+ Sidekiq.logger.send(level, "[#{self.log_name}] [#{/^([:_a-z]+\:repeating_job\:)([:_a-z]+)$/.match(args['repeating_job_key'])[2]}] #{message}")
14
+ end
15
+
16
+ private
17
+
18
+ def process_name
19
+ "#{Socket.gethostbyname(Socket.gethostname).first}-p#{Process.pid}-t#{Thread.current.object_id}"
20
+ end
21
+
22
+ def active_key(args)
23
+ "#{args['repeating_job_key']}:active"
24
+ end
25
+
26
+ def owner_id_key(args)
27
+ "#{args['repeating_job_key']}:owner_id"
28
+ end
29
+
30
+ def owner_name_key(args)
31
+ "#{args['repeating_job_key']}:owner_name"
32
+ end
33
+
34
+ def last_run_at_key(args)
35
+ "#{args['repeating_job_key']}:last_run_at"
36
+ end
37
+
38
+ def valid_arguments?(worker_class, arguments)
39
+ raise "Missing repeat job arguments" if arguments.nil? || !arguments.is_a?(Hash)
40
+ arguments.assert_valid_keys('scheduler_key', 'scheduler_id', 'repeating_job_key', 'repeating_job_id')
41
+ raise "Missing scheduler_key repeat job argument" if arguments['scheduler_key'].nil?
42
+ check_scheduler(worker_class, arguments)
43
+ return false if arguments['scheduler_id'].nil?
44
+ raise "Missing repeating_job_key repeat job argument" if arguments['repeating_job_key'].nil?
45
+ check_reservation(worker_class, arguments)
46
+ return false if arguments['repeating_job_id'].nil?
47
+ return true
48
+ end
49
+
50
+ def check_scheduler(worker_class, arguments)
51
+ Sidekiq.redis do |conn|
52
+ if (scheduler_id=conn.redis.get("#{arguments['scheduler_key']}:owner_id"))
53
+ if arguments['scheduler_id']
54
+ unless arguments['scheduler_id'] == scheduler_id
55
+ self.log_message(arguments, "schedule [#{arguments['scheduler_id']}] no longer active")
56
+ arguments.delete('scheduler_id')
57
+ conn.redis.del(owner_id_key(arguments))
58
+ end
59
+ else
60
+ arguments['scheduler_id']=scheduler_id
61
+ self.log_message(arguments, "Using active schedule [#{arguments['scheduler_id']}]")
62
+ end
63
+ else
64
+ self.log_message(arguments, "has no active schedule")
65
+ arguments.delete('scheduler_id')
66
+ end
67
+ end
68
+ end
69
+
70
+ def check_reservation(worker_class, arguments)
71
+ Sidekiq.redis do |conn|
72
+ if conn.redis.get(active_key(arguments)) == 'true' || conn.redis.setnx(active_key(arguments), 'true')
73
+ repeating_job_id=arguments['repeating_job_id'] || SecureRandom.hex(16).downcase
74
+ existing_repeating_job_id=conn.redis.get(owner_id_key(arguments))
75
+ unless repeating_job_id == existing_repeating_job_id || conn.redis.setnx(owner_id_key(arguments), repeating_job_id)
76
+ if existing_repeating_job_id
77
+ self.log_message(arguments, "existing reservation [#{existing_repeating_job_id}] for #{conn.redis.ttl(owner_id_key(arguments))} seconds")
78
+ else
79
+ self.log_message(arguments, "invalid reservation [#{arguments['repeating_job_id']}]")
80
+ end
81
+ arguments.delete('repeating_job_id')
82
+ else
83
+ arguments['repeating_job_id']=repeating_job_id
84
+ result=conn.redis.multi do
85
+ conn.redis.expire(owner_id_key(arguments), SidekiqRepeatingJobs::Config.expiration_interval(worker_class))
86
+ conn.redis.setex(owner_name_key(arguments), SidekiqRepeatingJobs::Config.expiration_interval(worker_class), process_name)
87
+ conn.redis.set(last_run_at_key(arguments), DateTime.now.strftime("%Y-%m-%d %H:%M:%S %Z"))
88
+ end
89
+ if existing_repeating_job_id
90
+ self.log_message(arguments, "extended reservation [#{arguments['repeating_job_id']}] by #{SidekiqRepeatingJobs::Config.expiration_interval(worker_class)} seconds")
91
+ else
92
+ self.log_message(arguments, "reserved [#{arguments['repeating_job_id']}] for #{SidekiqRepeatingJobs::Config.expiration_interval(worker_class)} seconds")
93
+ end
94
+ end
95
+ else
96
+ self.log_message(arguments, "is currently inactive")
97
+ end
98
+ end
99
+ end
100
+
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,30 @@
1
+ module SidekiqRepeatingJobs
2
+ module Middleware
3
+ module Server
4
+ class RepeatingJobs
5
+ include SidekiqRepeatingJobs::Middleware::Helpers
6
+
7
+ def log_name
8
+ "RepeatingJobServer"
9
+ end
10
+
11
+ def call(*args)
12
+ worker_class=eval(args[1]['class'])
13
+ if SidekiqRepeatingJobs::Config.active?(worker_class)
14
+ arguments=args[1]['args'].slice!(-1)
15
+ if valid_arguments?(worker_class, arguments)
16
+ begin
17
+ yield
18
+ ensure
19
+ worker_class.perform_in(SidekiqRepeatingJobs::Config.repeat_interval(worker_class), *args[1]['args'], arguments)
20
+ self.log_message(arguments, "scheduled in #{SidekiqRepeatingJobs::Config.repeat_interval(worker_class)} seconds")
21
+ end
22
+ end
23
+ else
24
+ yield
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module SidekiqRepeatingJobs
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,23 @@
1
+ module SidekiqRepeatingJobs
2
+
3
+ module WorkerHelper
4
+
5
+ module ClassMethods
6
+ def trigger(*keys)
7
+ keys.each do |key|
8
+ arguments={
9
+ 'scheduler_key' => "#{Settings[:redis].namespace}:scheduler",
10
+ 'repeating_job_key' => "#{Settings[:sidekiq].namespace}:repeating_job:#{SidekiqRepeatingJobs::Config.job_name(self)}:#{key}"
11
+ }
12
+ self.perform_async(key, arguments)
13
+ end
14
+ end
15
+ end
16
+
17
+ def self.included(base)
18
+ base.extend ClassMethods
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sidekiq_repeating_jobs/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sidekiq-repeating-jobs"
8
+ spec.version = SidekiqRepeatingJobs::VERSION
9
+ spec.authors = ["Marc Lennox"]
10
+ spec.email = ["marc.lennox@gmail.com"]
11
+ spec.description = %q{Sidekiq plugin that provides for singleton jobs that reschedule themselves after completion}
12
+ spec.summary = %q{Sidekiq plugin that provides for singleton jobs that reschedule themselves after completion}
13
+ spec.homepage = "http://github.com/marclennox/sidekiq-repeating-jobs"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_dependency "sidekiq", "~> 2.15"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-repeating-jobs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Marc Lennox
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sidekiq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.15'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: Sidekiq plugin that provides for singleton jobs that reschedule themselves
56
+ after completion
57
+ email:
58
+ - marc.lennox@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/sidekiq_repeating_jobs.rb
69
+ - lib/sidekiq_repeating_jobs/config.rb
70
+ - lib/sidekiq_repeating_jobs/middleware.rb
71
+ - lib/sidekiq_repeating_jobs/middleware/client/repeating_jobs.rb
72
+ - lib/sidekiq_repeating_jobs/middleware/helpers.rb
73
+ - lib/sidekiq_repeating_jobs/middleware/server/repeating_jobs.rb
74
+ - lib/sidekiq_repeating_jobs/version.rb
75
+ - lib/sidekiq_repeating_jobs/worker_helper.rb
76
+ - sidekiq-repeating-jobs.gemspec
77
+ homepage: http://github.com/marclennox/sidekiq-repeating-jobs
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.1.9
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Sidekiq plugin that provides for singleton jobs that reschedule themselves
101
+ after completion
102
+ test_files: []