active_job_cron_scheduler 0.1.1

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
+ SHA256:
3
+ metadata.gz: e7898345b0e7487fc694451880c70fdbf3aa360f5256afda892cc4442a655666
4
+ data.tar.gz: b3ab66183892a1ae5820d7478efea3d8496fd46564e5d3de1bc31fe9ce64cb74
5
+ SHA512:
6
+ metadata.gz: 505087e528cb2b77ef1146e6a534720c60e88f2e896f7facdc621a6e06a9c9c83cca6a66c0478e21821034135103606c5c2efdb697c31a335fbb0e73316fa2a7
7
+ data.tar.gz: 0b3f671f18fe5ef9b82689855a9e3746d87c14e10b6411d74312c82d7a6ad044e17305fbe6f919762a7df415cfa4468f330bf6f48f620440f12eb558801812a6
@@ -0,0 +1,36 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ branches: [ "main" ]
6
+ pull_request:
7
+ branches: [ "main" ]
8
+
9
+ jobs:
10
+ build:
11
+ name: Build + Publish
12
+ runs-on: ubuntu-latest
13
+ permissions:
14
+ contents: read
15
+ packages: write
16
+
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+ - name: Set up Ruby 3.3.x
20
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
21
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
22
+ # uses: ruby/setup-ruby@v1
23
+ uses: ruby/setup-ruby@v1
24
+ with:
25
+ ruby-version: 3.3.4
26
+
27
+ - name: Publish to RubyGems
28
+ run: |
29
+ mkdir -p $HOME/.gem
30
+ touch $HOME/.gem/credentials
31
+ chmod 0600 $HOME/.gem/credentials
32
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
33
+ gem build *.gemspec
34
+ gem push *.gem
35
+ env:
36
+ GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
data/.gitignore ADDED
@@ -0,0 +1,57 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
57
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Anatoly Zelenin
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # ActiveJobCronScheduler
2
+
3
+ ActiveJobCronScheduler is a Ruby on Rails gem that provides a simple and efficient way to schedule recurring jobs in your Rails application. It leverages ActiveJob and provides a clean DSL for defining job schedules.
4
+
5
+ ## Features
6
+
7
+ - Simple DSL for scheduling recurring jobs
8
+ - Built on top of ActiveJob for seamless integration with Rails
9
+ - Prevents parallel execution of the same job
10
+ - Stores execution history for debugging purposes
11
+ - No external dependencies (like Redis) required
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'active_job_cron_scheduler'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ ```
24
+ $ bundle install
25
+ ```
26
+
27
+ After installing the gem, run the installation generator:
28
+
29
+ ```
30
+ $ rails generate active_job_cron_scheduler:install
31
+ ```
32
+
33
+ This will create a migration file. Run the migration to create the necessary database table:
34
+
35
+ ```
36
+ $ rails db:migrate
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ### Defining a Job
42
+
43
+ To create a job that runs on a schedule, simply include the `ActiveJobCronScheduler::ActiveJobCronScheduler` module in your job class and use the `schedule_each` method to define the schedule:
44
+
45
+ ```ruby
46
+ class HourlyJob < ApplicationJob
47
+ include ActiveJobCronScheduler::ActiveJobCronScheduler
48
+
49
+ schedule_each 1.hour
50
+
51
+ def perform(*ignore)
52
+ # Your code here
53
+ puts "Performing hourly job at #{Time.current}"
54
+ end
55
+ end
56
+ ```
57
+
58
+ ### Schedule Options
59
+
60
+ You can schedule jobs with different intervals:
61
+
62
+ ```ruby
63
+
64
+ class DailyJob < ApplicationJob
65
+ include ActiveJobCronScheduler::ActiveJobCronScheduler
66
+
67
+ schedule_each 1.day, at: "02:00"
68
+
69
+ def perform(*ignore)
70
+ # This job will run every day at 2:00 AM
71
+ end
72
+ end
73
+
74
+ class WeeklyJob < ApplicationJob
75
+ include ActiveJobCronScheduler::ActiveJobCronScheduler
76
+
77
+ schedule_each 1.week, on: :monday, at: "09:00"
78
+
79
+ def perform(*ignore)
80
+ # This job will run every Monday at 9:00 AM
81
+ end
82
+ end
83
+
84
+ class MonthlyJob < ApplicationJob
85
+ include ActiveJobCronScheduler::ActiveJobCronScheduler
86
+
87
+ schedule_each 1.month, on: 1, at: "00:00"
88
+
89
+ def perform(*ignore)
90
+ # This job will run on the first day of every month at midnight
91
+ end
92
+ end
93
+ ```
94
+
95
+ ### Job Execution
96
+
97
+ Jobs are automatically scheduled when your Rails application starts. The `ActiveJobCronScheduler::Scheduler` takes care of scheduling all defined jobs.
98
+
99
+ ### Debugging
100
+
101
+ You can check the `active_job_cron_schedulers` table in your database for information about job executions:
102
+
103
+ ```ruby
104
+ ActiveJobCronScheduler::JobRecord.all.each do |record|
105
+ puts "Job: #{record.job}"
106
+ puts "Last execution: #{record.last_execution}"
107
+ puts "---"
108
+ end
109
+ ```
110
+
111
+ ## Configuration
112
+
113
+ By default, ActiveJobCronScheduler uses your application's default ActiveJob queue adapter. If you want to use a specific adapter for scheduled jobs, you can configure it in an initializer:
114
+
115
+ ```ruby
116
+ # config/initializers/active_job_cron_scheduler.rb
117
+ Rails.application.config.active_job.queue_adapter = :sidekiq
118
+ ```
119
+
120
+ ## Contributing
121
+
122
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yourusername/active_job_cron_scheduler.
123
+
124
+ ## License
125
+
126
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,31 @@
1
+ require_relative "lib/active_job_cron_scheduler/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "active_job_cron_scheduler"
5
+ spec.version = ActiveJobCronScheduler::VERSION
6
+ spec.authors = ["Anatoly Zelenin"]
7
+ spec.email = ["anatoly@zelenin.de"]
8
+
9
+ spec.summary = "A simple and efficient cron-like job scheduler for Ruby on Rails"
10
+ spec.description = "ActiveJobCronScheduler provides a clean DSL for defining recurring jobs in Rails applications, leveraging ActiveJob for seamless integration."
11
+ spec.homepage = "https://github.com/itadventurer/active_job_cron_scheduler"
12
+ spec.license = "MIT"
13
+
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/itadventurer/active_job_cron_scheduler"
18
+ #spec.metadata["changelog_uri"] = "https://github.com/itadventurer/active_job_cron_scheduler/blob/master/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
24
+ end
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "rails", ">= 5.2"
28
+ spec.add_dependency "activejob", ">= 5.2"
29
+
30
+ spec.add_development_dependency "rake", "~> 13.0"
31
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveJobCronScheduler
4
+ module ActiveJobCronScheduler
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ before_perform do |job|
9
+ self.class.check_singleton_token(job.arguments.first[:singleton_token])
10
+ end
11
+ after_perform do |job|
12
+ self.class.schedule_next
13
+ end
14
+ end
15
+
16
+ class_methods do
17
+ def schedule_each(interval, options = {})
18
+ @interval = interval
19
+ @schedule_options = options
20
+ end
21
+
22
+ def schedule_next
23
+ job_record = find_or_create_job_record
24
+ job_record.update(last_execution: Time.current)
25
+ next_run = calculate_next_run(job_record)
26
+ set(wait_until: next_run).perform_later(singleton_token: job_record.singleton_token)
27
+ end
28
+
29
+ def override_and_schedule
30
+ job_record = find_or_create_job_record
31
+ job_record.update(singleton_token: SecureRandom.uuid)
32
+ next_run = calculate_next_run(job_record)
33
+ set(wait_until: next_run).perform_later(singleton_token: job_record.singleton_token)
34
+ end
35
+
36
+ def find_or_create_job_record
37
+ JobRecord.find_or_create_by(job: name) do |record|
38
+ record.singleton_token = SecureRandom.uuid
39
+ end
40
+ end
41
+
42
+ def check_singleton_token(singleton_token)
43
+ job_record = find_or_create_job_record
44
+ if job_record.singleton_token != singleton_token
45
+ logger.info "Job #{self.class.name} skipped: mismatched singleton token"
46
+ throw :abort
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def calculate_next_run(job_record)
53
+ case @interval
54
+ when ActiveSupport::Duration
55
+ if job_record.last_execution.nil?
56
+ Time.current
57
+ else
58
+ job_record.last_execution + @interval
59
+ end
60
+ else
61
+ raise ArgumentError, "Unsupported interval type"
62
+ end
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveJobCronScheduler
4
+ class JobRecord < ActiveRecord::Base
5
+ self.table_name = 'active_job_schedules'
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveJobCronScheduler
4
+ class Railtie < Rails::Railtie
5
+ initializer 'active_job_cron_scheduler.initialize' do
6
+ Rails.application.config.after_initialize do
7
+ if Rails.env.development? || Rails.env.production?
8
+ if defined?(Rails::Server)
9
+ Scheduler.schedule_all
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'active_job_cron_scheduler'
3
+ require_relative 'job_record'
4
+
5
+ module ActiveJobCronScheduler
6
+ class Scheduler
7
+ def self.schedule_all
8
+ Dir[Rails.root.join('app', 'jobs', '**', '*.rb')].each { |file| require_dependency file }
9
+ ActiveJob::Base.descendants.each do |job_class|
10
+ if job_class.include?(ActiveJobCronScheduler)
11
+ # Log as info
12
+ Rails.logger.info "Scheduling ActiveJobCronScheduler #{job_class}"
13
+ job_class.override_and_schedule
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveJobCronScheduler
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ require 'active_job_cron_scheduler/railtie' if defined?(Rails)
3
+ require 'active_job_cron_scheduler/active_job_cron_scheduler'
4
+ require 'active_job_cron_scheduler/scheduler'
5
+ require 'active_job_cron_scheduler/job_record'
6
+
7
+ module ActiveJobCronScheduler
8
+ class Error < StandardError; end
9
+ end
10
+
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+ require 'rails/generators/migration'
5
+
6
+ module ActiveJobCronScheduler
7
+ module Generators
8
+ class InstallGenerator < Rails::Generators::Base
9
+ include Rails::Generators::Migration
10
+
11
+ source_root File.expand_path('templates', __dir__)
12
+
13
+ def self.next_migration_number(dirname)
14
+ next_migration_number = current_migration_number(dirname) + 1
15
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
16
+ end
17
+
18
+ def create_migration_file
19
+ migration_template 'create_active_job_schedules.rb', 'db/migrate/create_active_job_schedules.rb'
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateActiveJobSchedules < ActiveRecord::Migration[6.1]
4
+ def change
5
+ create_table :active_job_schedules, id: false do |t|
6
+ t.string :job, null: false, primary_key: true
7
+ t.datetime :last_execution
8
+ t.string :singleton_token
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_job_cron_scheduler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Anatoly Zelenin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-08-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activejob
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ description: ActiveJobCronScheduler provides a clean DSL for defining recurring jobs
56
+ in Rails applications, leveraging ActiveJob for seamless integration.
57
+ email:
58
+ - anatoly@zelenin.de
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".github/workflows/gem-push.yml"
64
+ - ".gitignore"
65
+ - Gemfile
66
+ - LICENSE
67
+ - README.md
68
+ - active_job_cron_scheduler.gemspec
69
+ - lib/active_job_cron_scheduler.rb
70
+ - lib/active_job_cron_scheduler/active_job_cron_scheduler.rb
71
+ - lib/active_job_cron_scheduler/job_record.rb
72
+ - lib/active_job_cron_scheduler/railtie.rb
73
+ - lib/active_job_cron_scheduler/scheduler.rb
74
+ - lib/active_job_cron_scheduler/version.rb
75
+ - lib/generators/active_job_cron_scheduler/install_generator.rb
76
+ - lib/generators/active_job_cron_scheduler/templates/create_active_job_schedules.rb
77
+ homepage: https://github.com/itadventurer/active_job_cron_scheduler
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ homepage_uri: https://github.com/itadventurer/active_job_cron_scheduler
82
+ source_code_uri: https://github.com/itadventurer/active_job_cron_scheduler
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 2.5.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.5.11
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A simple and efficient cron-like job scheduler for Ruby on Rails
102
+ test_files: []