active_job-cron 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8716c8fca2467b6158690d0468bf102a10dedfee
4
+ data.tar.gz: 1f3470b668a58861bfd470c626f703547c463552
5
+ SHA512:
6
+ metadata.gz: 60756b6feb0df156c5e8ad6f5f36ab06da6eb2c8ede69c50fcfce99e9b79bb300f7cfd8621708a605a4139119204e0231951affb9906c29727718ed556704f67
7
+ data.tar.gz: 67885ccb0a23f1bfce098380cdbcd518151732d45e8ae38b5acdcd69da684b070e19df7cbdf4262e61b6c91f8bc8cc905da36c9264f763bcfa61699fd5676afc
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ **.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+ rvm:
5
+ - 2.0
6
+ - 2.1
7
+ - 2.2
8
+ - ruby-head
9
+
10
+ notifications:
11
+ email:
12
+ recipients:
13
+ - stan001212@gmail.com
14
+ on_failure: change
15
+ on_success: never
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_job-cron.gemspec
4
+ gemspec
5
+
6
+ gem "pry"
7
+ gem "codeclimate-test-reporter", group: :test, require: nil
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Stan Lo
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.
@@ -0,0 +1,75 @@
1
+ [![Build Status](https://travis-ci.org/st0012/ActiveJob-Cron.svg)](https://travis-ci.org/st0012/ActiveJob-Cron)
2
+ [![Code Climate](https://codeclimate.com/github/st0012/ActiveJob-Cron/badges/gpa.svg)](https://codeclimate.com/github/st0012/ActiveJob-Cron)
3
+ [![Test Coverage](https://codeclimate.com/github/st0012/ActiveJob-Cron/badges/coverage.svg)](https://codeclimate.com/github/st0012/ActiveJob-Cron/coverage)
4
+
5
+ # ActiveJob::Cron
6
+
7
+ This gem provides recurrent functionality for your `ActiveJob`. The usage and implementation is highly inspired by [Sidetiq](https://github.com/tobiassvn/sidetiq).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'active_job-cron'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install active_job-cron
24
+
25
+ ## Usage
26
+
27
+ It's very simple to use, just include `ActiveJob::Cron`, and sets your job's recurrent frequency:
28
+
29
+ ```ruby
30
+ class MyJob < ActiveJob::Base
31
+ include ActiveJob::Cron
32
+
33
+ recurrence { daily }
34
+
35
+ def perform
36
+ # do something.....
37
+ end
38
+ end
39
+ ```
40
+
41
+ Or you can specify more complex recurrence like:
42
+
43
+ ```ruby
44
+ class MyJob < ActiveJob::Base
45
+ include ActiveJob::Cron
46
+
47
+ recurrence { weekly.day(:thursday) }
48
+
49
+ def perform
50
+ # do something.....
51
+ end
52
+ end
53
+ ```
54
+
55
+ I uses [IceCube](http://seejohncode.com/ice_cube/) as recurrence's DSL, you can see more usages in its documentation.
56
+
57
+ ## Development
58
+
59
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
60
+
61
+ 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).
62
+
63
+ ## Contributing
64
+
65
+ - More feature
66
+ - More documentation
67
+ - More tests
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/st0012/active_job-cron. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
70
+
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
75
+
@@ -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
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_job/cron/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_job-cron"
8
+ spec.version = ActiveJob::Cron::VERSION
9
+ spec.authors = ["Stan Lo"]
10
+ spec.email = ["a22301613@yahoo.com.tw"]
11
+
12
+ spec.summary = %q{It provides functionality for actibe_jobs being triggered recurrently.}
13
+ spec.description = %q{It provides functionality for actibe_jobs being triggered recurrently.}
14
+ spec.homepage = "https://github.com/st0012/ActiveJob-Cron"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "celluloid"
31
+ spec.add_dependency "ice_cube"
32
+ spec.add_dependency "rails", "~> 4.2"
33
+
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec"
36
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_job/cron"
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
@@ -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,60 @@
1
+ require "celluloid"
2
+ require "ice_cube"
3
+
4
+ require "active_job/cron/version"
5
+ require "active_job/cron/configuration"
6
+ require "active_job/cron/schedulable"
7
+ require "active_job/cron/trackable"
8
+ require "active_job/cron/loggable"
9
+ require "active_job/cron/clock"
10
+ require "active_job/cron/handler"
11
+
12
+ module ActiveJob
13
+ module Cron
14
+ extend Configurable
15
+ extend Trackable
16
+ extend Schedulable
17
+
18
+ def self.included(klass)
19
+ super
20
+
21
+ klass.extend(ActiveJob::Cron::Schedulable)
22
+ klass.extend(ActiveJob::Cron::Trackable)
23
+
24
+ subclasses << klass
25
+ end
26
+
27
+ class << self
28
+ def jobs
29
+ subclasses(true).select do |job|
30
+ job.method_defined?(:perform)
31
+ end
32
+ end
33
+
34
+ def handler
35
+ if handler = Supervisor.handler
36
+ handler
37
+ else
38
+ run!
39
+ Supervisor.handler
40
+ end
41
+ end
42
+
43
+ def clock
44
+ if clock = Supervisor.clock
45
+ clock
46
+ else
47
+ run!
48
+ Supervisor.clock
49
+ end
50
+ end
51
+
52
+ def run!
53
+ Supervisor.run!
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ require "active_job/cron/supervisor"
60
+ require "active_job/cron/railtie"
@@ -0,0 +1,51 @@
1
+ module ActiveJob
2
+ module Cron
3
+ class Clock
4
+ include Celluloid
5
+ include Loggable
6
+ attr_reader :schedules
7
+
8
+ def initialize # :nodoc:
9
+ super
10
+ after(1) do
11
+ loop!
12
+ end
13
+ end
14
+
15
+ # Get the schedule for `job`.
16
+ def schedule_for(job)
17
+ if job.respond_to?(:schedule)
18
+ job.schedule
19
+ end
20
+ end
21
+
22
+ # Tick a single clock.
23
+ def tick(time = gettime)
24
+ ActiveJob::Cron.jobs.each do |job|
25
+ ActiveJob::Cron.handler.dispatch(job, time)
26
+ end
27
+ end
28
+
29
+ # Returns the current time used by the clock.
30
+ def gettime
31
+ ActiveJob::Cron.config.utc ? Time.now.utc : Time.now
32
+ end
33
+
34
+ private
35
+
36
+ def loop!
37
+ after([time { tick }, 0].max) do
38
+ loop!
39
+ end
40
+ rescue StandardError => e
41
+ raise e
42
+ end
43
+
44
+ def time
45
+ start = gettime
46
+ yield
47
+ ActiveJob::Cron.config.resolution - (gettime.to_f - start.to_f)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,24 @@
1
+ require "rails"
2
+
3
+ module ActiveJob
4
+ module Cron
5
+ module Configurable
6
+ def configure
7
+ yield(config)
8
+ end
9
+
10
+ def config
11
+ @config ||= Configuration.new
12
+ end
13
+ end
14
+ class Configuration
15
+ attr_accessor :resolution, :utc, :handler_pool_size, :logger, :preload_jobs
16
+
17
+ def initialize
18
+ @resolution = 1
19
+ @utc = false
20
+ @preload_jobs = true
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveJob
2
+ module Cron
3
+ class Handler
4
+ include Celluloid
5
+
6
+ def dispatch(job, time)
7
+ job.perform_later if job.ready_to_perform?(time)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module ActiveJob
2
+ module Cron
3
+ module Loggable
4
+ %w(fatal error warn info debug).each do |level|
5
+ level = level.to_sym
6
+
7
+ define_method(level) do |msg|
8
+ if logger = ActiveJob::Cron.config.logger
9
+ logger.__send__(level, "[ActiveJob::Cron] #{msg}")
10
+ else
11
+ puts "[ActiveJob::Cron] #{level}: #{msg}"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ require "rails"
2
+
3
+ module ActiveJob
4
+ module Cron
5
+ class Railtie < Rails::Railtie
6
+ config.after_initialize do
7
+ ActiveJob::Cron.config.logger = Rails.logger
8
+
9
+ Dir.glob(Rails.root + "app/jobs/*_job*.rb").each do |c|
10
+ require_dependency(c)
11
+ end if ActiveJob::Cron.config.preload_jobs
12
+
13
+ ActiveJob::Cron.run!
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ module ActiveJob
2
+ module Cron
3
+ module Schedulable
4
+ attr_reader :last_occurrence
5
+
6
+ def ready_to_perform?(time)
7
+ next_occurrence = schedule.next_occurrence(time).to_i
8
+ # See IceCube's document about how #next_occurrence works
9
+ if next_occurrence != last_occurrence
10
+ @last_occurrence = next_occurrence
11
+ true
12
+ else
13
+ false
14
+ end
15
+ end
16
+
17
+ def schedule
18
+ @schedule ||= IceCube::Schedule.new
19
+ end
20
+
21
+ def recurrence
22
+ yield
23
+ end
24
+
25
+ def method_missing(meth, *args, &block)
26
+ if IceCube::Rule.respond_to?(meth)
27
+ rule = IceCube::Rule.send(meth, *args, &block)
28
+ schedule.add_recurrence_rule(rule)
29
+ rule
30
+ elsif schedule.respond_to?(meth)
31
+ schedule.send(meth, *args, &block)
32
+ else
33
+ super
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,29 @@
1
+ module ActiveJob
2
+ module Cron
3
+ class Supervisor < Celluloid::SupervisionGroup
4
+ supervise ActiveJob::Cron::Clock, as: :active_job_clock
5
+ if handler_pool_size = ActiveJob::Cron.config.handler_pool_size
6
+ pool ActiveJob::Cron::Handler,
7
+ as: :cron_job_handler,
8
+ size: handler_pool_size
9
+ else
10
+ pool ActiveJob::Cron::Handler,
11
+ as: :cron_job_handler
12
+ end
13
+
14
+ class << self
15
+ def clock
16
+ Celluloid::Actor[:active_job_clock]
17
+ end
18
+
19
+ def handler
20
+ Celluloid::Actor[:cron_job_handler]
21
+ end
22
+
23
+ def run
24
+ raise "Sidetiq::Supervisor should not be run in foreground."
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ module ActiveJob
2
+ module Cron
3
+ module Trackable
4
+ def subclasses(deep = false)
5
+ @subclasses ||= []
6
+
7
+ if deep
8
+ @subclasses.inject([]) do |all, subclass|
9
+ (all << subclass) + subclass.subclasses(true)
10
+ end
11
+ else
12
+ @subclasses
13
+ end
14
+ end
15
+
16
+ def inherited(klass)
17
+ super
18
+ subclasses << klass
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveJob
2
+ module Cron
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_job-cron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stan Lo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: celluloid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ice_cube
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: It provides functionality for actibe_jobs being triggered recurrently.
84
+ email:
85
+ - a22301613@yahoo.com.tw
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - active_job-cron.gemspec
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/active_job/cron.rb
102
+ - lib/active_job/cron/clock.rb
103
+ - lib/active_job/cron/configuration.rb
104
+ - lib/active_job/cron/handler.rb
105
+ - lib/active_job/cron/loggable.rb
106
+ - lib/active_job/cron/railtie.rb
107
+ - lib/active_job/cron/schedulable.rb
108
+ - lib/active_job/cron/supervisor.rb
109
+ - lib/active_job/cron/trackable.rb
110
+ - lib/active_job/cron/version.rb
111
+ homepage: https://github.com/st0012/ActiveJob-Cron
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
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.4.5.1
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: It provides functionality for actibe_jobs being triggered recurrently.
135
+ test_files: []
136
+ has_rdoc: