sidekiq-expiring-jobs 0.1.0

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: bb92ddcc1f401edb1b9f9462e599c9800ec8ea9550bc7568f16aab9bbf88ae88
4
+ data.tar.gz: 9f2e2483ec683668a512f7580d93de0dac245ee7a1179c81b36e1da21d711532
5
+ SHA512:
6
+ metadata.gz: a01c577b380b0f4a4d5cb02660bcca4d82cb33995c7f6042b99c9a28a9aa5185e15f2d6a5408e837843b37c0df9fd304efa52c870e2aa793811ff53597af6367
7
+ data.tar.gz: c30d348d3d9bba82c26bf482e58572e05f06fb6a578a1a902d3b31d21ecbf82e383255f510718c6f6331b46c49abf727e2f511660e09124f1d3340f28f745b9c
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## master (unreleased)
2
+
3
+ ## 0.1.0 (2023-04-12)
4
+
5
+ - First release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 fatkodima
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,85 @@
1
+ # SidekiqExpiringJobs
2
+
3
+ [![Build Status](https://github.com/fatkodima/sidekiq-expiring-jobs/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fatkodima/sidekiq-expiring-jobs/actions/workflows/ci.yml)
4
+
5
+ Support for Sidekiq jobs which expire after a certain length of time.
6
+ Jobs that are set to expire can run as long as they want, but an expiring job must start executing before the expiration time.
7
+
8
+ Note: Sidekiq Pro has this feature, so please consider upgrading if you can.
9
+
10
+ ## Use Cases
11
+
12
+ 1. Perhaps you want to expire a cache which has a TTL of 30 minutes with a Sidekiq job. If the job doesn't process successfully within 30 minutes, there's no point in executing the job.
13
+ 2. You use a Sidekiq job to send a daily digest email. If the job doesn't execute within 24 hours, perhaps you want to skip that day as the user might only care about the latest digest.
14
+ 3. You enqueue periodically a Sidekiq job to do some task. If the job doesn't execute before the next period begins, you may skip that job as the newly enqueued job will do the task.
15
+
16
+ ## Requirements
17
+
18
+ - Ruby 2.7+
19
+ - Sidekiq 6.0+
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'sidekiq-expiring-jobs'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ ```sh
32
+ $ bundle
33
+ ```
34
+
35
+ Or install it yourself as:
36
+
37
+ ```sh
38
+ $ gem install sidekiq-expiring-jobs
39
+ ```
40
+
41
+ ## Defining Expiration
42
+
43
+ Statically:
44
+
45
+ ```ruby
46
+ class SomeJob
47
+ include Sidekiq::Job
48
+ sidekiq_options expires_in: 1.hour
49
+ ...
50
+ end
51
+ ```
52
+
53
+ Dynamically, per job:
54
+
55
+ ```ruby
56
+ SomeJob.set(expires_in: 1.day).perform_async(...)
57
+ ```
58
+
59
+ `expires_in` must be a relative time, not an absolute timestamp.
60
+
61
+ Expiration knows about scheduled jobs: schedule a job to run two hours from now with a one hour expiration and it will expire **three** hours from now.
62
+
63
+ ## Configuration
64
+
65
+ You can override the following default options:
66
+
67
+ ```ruby
68
+ # A callback that is called when the job is expired.
69
+ # Accepts a job hash as an argument.
70
+ SidekiqExpiringJobs.expiration_callback = ->(job) {}
71
+ ```
72
+
73
+ ## Development
74
+
75
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
76
+
77
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
78
+
79
+ ## Contributing
80
+
81
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fatkodima/sidekiq-expiring-jobs.
82
+
83
+ ## License
84
+
85
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq_expiring_jobs"
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqExpiringJobs
4
+ module Middleware
5
+ class Client
6
+ def call(_worker_class, job, _queue, _redis_pool)
7
+ if (expires_in = job.delete("expires_in"))
8
+ at = job["at"] || job["created_at"]
9
+ job["expires_at"] = at + expires_in.to_f
10
+ end
11
+ yield
12
+ end
13
+ end
14
+
15
+ class Server
16
+ def call(_worker, job, _queue)
17
+ if (expires_at = job["expires_at"])
18
+ if expires_at >= Time.now.to_f
19
+ yield
20
+ else
21
+ Sidekiq.logger.info("[SidekiqExpiringJobs] Expired #{job['class']} job (jid=#{job['jid']}) is skipped")
22
+ SidekiqExpiringJobs.expiration_callback&.call(job)
23
+ end
24
+ else
25
+ yield
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ Sidekiq.configure_client do |config|
33
+ config.client_middleware do |chain|
34
+ chain.add(SidekiqExpiringJobs::Middleware::Client)
35
+ end
36
+ end
37
+
38
+ Sidekiq.configure_server do |config|
39
+ config.client_middleware do |chain|
40
+ chain.add(SidekiqExpiringJobs::Middleware::Client)
41
+ end
42
+
43
+ config.server_middleware do |chain|
44
+ chain.add(SidekiqExpiringJobs::Middleware::Server)
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SidekiqExpiringJobs
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq"
4
+
5
+ require_relative "sidekiq_expiring_jobs/version"
6
+
7
+ module SidekiqExpiringJobs
8
+ class << self
9
+ attr_accessor :expiration_callback
10
+ end
11
+
12
+ self.expiration_callback = ->(job) {}
13
+ end
14
+
15
+ require_relative "sidekiq_expiring_jobs/middleware"
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-expiring-jobs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - fatkodima
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-04-12 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: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ description: |
28
+ Support for Sidekiq jobs which expire after a certain length of time.
29
+ Jobs that are set to expire can run as long as they want, but an expiring job
30
+ must start executing before the expiration time.
31
+ email:
32
+ - fatkodima123@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - CHANGELOG.md
38
+ - LICENSE.txt
39
+ - README.md
40
+ - lib/sidekiq-expiring-jobs.rb
41
+ - lib/sidekiq_expiring_jobs.rb
42
+ - lib/sidekiq_expiring_jobs/middleware.rb
43
+ - lib/sidekiq_expiring_jobs/version.rb
44
+ homepage: https://github.com/fatkodima/sidekiq-expiring-jobs
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/fatkodima/sidekiq-expiring-jobs
49
+ source_code_uri: https://github.com/fatkodima/sidekiq-expiring-jobs
50
+ changelog_uri: https://github.com/fatkodima/sidekiq-expiring-jobs/blob/master/CHANGELOG.md
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 2.7.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.4.7
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Expiring jobs support for Sidekiq.
70
+ test_files: []