├── .rspec ├── Gemfile ├── lib ├── rails_permanent_job │ ├── version.rb │ ├── worker │ │ └── configuration.rb │ ├── run.rb │ └── worker.rb └── rails_permanent_job.rb ├── .gitignore ├── bin ├── setup └── console ├── examples ├── generic │ ├── simplest.rb │ ├── singe_job_in_four_workers.rb │ ├── jobs_as_lambdas.rb │ ├── single_job_in_one_worker.rb │ └── multiple_jobs_in_one_worker.rb └── fetch_messages_from_aws_sqs.rb ├── CHANGELOG.md ├── .rubocop.yml ├── Rakefile ├── spec ├── spec_helper.rb ├── rails_permanent_job_spec.rb └── rails_permanent_job_spec │ ├── run_spec.rb │ └── worker_spec.rb ├── rails_permanent_job.gemspec ├── Gemfile.lock ├── README.md └── LICENSE /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | --require spec_helper 4 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source "https://rubygems.org" 4 | 5 | gemspec 6 | -------------------------------------------------------------------------------- /lib/rails_permanent_job/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module RailsPermanentJob 4 | VERSION = "0.2.0" 5 | end 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /doc/ 6 | /pkg/ 7 | /spec/reports/ 8 | /tmp/ 9 | 10 | .rspec_status 11 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 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 9 | -------------------------------------------------------------------------------- /examples/generic/simplest.rb: -------------------------------------------------------------------------------- 1 | require_relative "../../lib/rails_permanent_job" 2 | 3 | class Beep 4 | def self.call(logger:, **_options) 5 | logger.info "beep" 6 | end 7 | end 8 | 9 | RailsPermanentJob.jobs = [Beep] 10 | RailsPermanentJob.run(workers: 1) 11 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [Unreleased] 2 | 3 | ## [0.2.0] - 2023-05-09 4 | 5 | - Replaced `worker_count` parameter and with `workers` 6 | - Added examples in `/examples` folder 7 | - Added example use case in Readme 8 | 9 | ## [0.1.0] - 2023-05-01 10 | 11 | - Initial release 12 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | inherit_mode: 2 | merge: 3 | - Exclude 4 | 5 | require: 6 | - standard 7 | - rubocop-performance 8 | 9 | inherit_gem: 10 | standard: config/base.yml 11 | standard-performance: config/base.yml 12 | standard-custom: config/base.yml 13 | 14 | AllCops: 15 | SuggestExtensions: false 16 | TargetRubyVersion: 3.2 17 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | require "bundler/setup" 5 | require "rails_permanent_job" 6 | 7 | # You can add fixtures and/or initialization code here to make experimenting 8 | # with your gem easier. You can also use a different console, if you like. 9 | 10 | require "irb" 11 | IRB.start(__FILE__) 12 | -------------------------------------------------------------------------------- /examples/generic/singe_job_in_four_workers.rb: -------------------------------------------------------------------------------- 1 | require_relative "../../lib/rails_permanent_job" 2 | 3 | class HardThingToDo 4 | def self.call(logger:, **_options) 5 | hard_work_time = rand 6 | 7 | logger.info "working hard for #{hard_work_time} second" 8 | 9 | sleep hard_work_time 10 | end 11 | end 12 | 13 | RailsPermanentJob.jobs = [HardThingToDo] 14 | RailsPermanentJob.run(workers: 4) 15 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "bundler/gem_tasks" 4 | require_relative "lib/rails_permanent_job" 5 | 6 | desc "Example permanent job" 7 | task :example_permanent_job do 8 | job = ->(logger:, **_options) { logger.info "I am doing it" } 9 | after_job = ->(_) { sleep 3 } 10 | 11 | RailsPermanentJob.jobs = [job] 12 | RailsPermanentJob.after_job = after_job 13 | RailsPermanentJob.run(workers: 1) 14 | end 15 | -------------------------------------------------------------------------------- /examples/generic/jobs_as_lambdas.rb: -------------------------------------------------------------------------------- 1 | require_relative "../../lib/rails_permanent_job" 2 | 3 | timer_sound_job = ->(logger:, **_options) do 4 | even_second = Time.now.to_i % 2 == 0 5 | 6 | sound = even_second ? "tic" : "tac" 7 | 8 | logger.info sound 9 | end 10 | 11 | after_job = ->(_) { sleep 1 } 12 | 13 | RailsPermanentJob.jobs = [timer_sound_job] 14 | RailsPermanentJob.after_job = after_job 15 | RailsPermanentJob.run(workers: 1) 16 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "rails_permanent_job" 4 | 5 | RSpec.configure do |config| 6 | # Enable flags like --only-failures and --next-failure 7 | config.example_status_persistence_file_path = ".rspec_status" 8 | 9 | # Disable RSpec exposing methods globally on `Module` and `main` 10 | config.disable_monkey_patching! 11 | 12 | config.expect_with :rspec do |c| 13 | c.syntax = :expect 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /lib/rails_permanent_job.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "serverengine" 4 | 5 | module RailsPermanentJob 6 | Error = Class.new(StandardError) 7 | 8 | def self.jobs=(jobs) 9 | Worker::Configuration.jobs = jobs 10 | end 11 | 12 | def self.after_job=(after_job) 13 | Worker::Configuration.after_job = after_job 14 | end 15 | 16 | def self.run(**params) 17 | Run.call(params) 18 | end 19 | end 20 | 21 | require_relative "rails_permanent_job/version" 22 | require_relative "rails_permanent_job/worker" 23 | require_relative "rails_permanent_job/run" 24 | -------------------------------------------------------------------------------- /examples/generic/single_job_in_one_worker.rb: -------------------------------------------------------------------------------- 1 | require_relative "../../lib/rails_permanent_job" 2 | 3 | class Job 4 | def self.call(logger:, config:, **options) 5 | logger.info "Doing Job, using config #{config.inspect}" 6 | end 7 | end 8 | 9 | class AfterJob 10 | def self.call(logger:, config:, **_options) 11 | if config[:log_level] == "debug" 12 | logger.info "Waiting for 1 second" 13 | end 14 | 15 | sleep 1 16 | end 17 | end 18 | 19 | RailsPermanentJob.jobs = [Job] 20 | RailsPermanentJob.after_job = AfterJob 21 | RailsPermanentJob.run(workers: 1, log_level: "debug") 22 | -------------------------------------------------------------------------------- /lib/rails_permanent_job/worker/configuration.rb: -------------------------------------------------------------------------------- 1 | module RailsPermanentJob 2 | module Worker 3 | class Configuration 4 | DEFAULT_JOBS = [].freeze 5 | DEFAULT_AFTER_JOB = ->(_logger) do 6 | sleep 1 7 | end 8 | 9 | def self.jobs 10 | @jobs || DEFAULT_JOBS 11 | end 12 | 13 | def self.jobs=(jobs) 14 | @jobs = jobs 15 | end 16 | 17 | def self.after_job 18 | @after_job || DEFAULT_AFTER_JOB 19 | end 20 | 21 | def self.after_job=(after_job) 22 | @after_job = after_job 23 | end 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /examples/generic/multiple_jobs_in_one_worker.rb: -------------------------------------------------------------------------------- 1 | require_relative "../../lib/rails_permanent_job" 2 | 3 | class FirstJob 4 | def self.call(logger:, **_options) 5 | logger.info "Doing the first job" 6 | end 7 | end 8 | 9 | class SecondJob 10 | def self.call(logger:, **_options) 11 | logger.info "Doing the second job" 12 | end 13 | end 14 | 15 | class AfterJob 16 | def self.call(logger:, **_options) 17 | logger.info "Taking a 3s break" 18 | 19 | sleep 3 20 | end 21 | end 22 | 23 | RailsPermanentJob.jobs = [FirstJob, SecondJob] 24 | RailsPermanentJob.after_job = AfterJob 25 | RailsPermanentJob.run(workers: 1) 26 | -------------------------------------------------------------------------------- /lib/rails_permanent_job/run.rb: -------------------------------------------------------------------------------- 1 | module RailsPermanentJob 2 | class Run 3 | DEFAULT_WORKER_COUNT = 1 4 | DEFAULT_PID_PATH = "tmp/rails_permanent_job.pid".freeze 5 | DEFAULT_LOG_FILE_NAME = "rails_permanent_job.log".freeze 6 | DEFAULT_LOG_LEVEL = "debug".freeze 7 | DEFAULT_LOG_ROTATE_AGE = 5 8 | DEFAULT_LOG_ROTATE_SIZE = 1_048_576 # 1024 * 1024 9 | 10 | DEFAULT_PARAMS = { 11 | daemonize: false, 12 | log_file_name: DEFAULT_LOG_FILE_NAME, 13 | log_level: DEFAULT_LOG_LEVEL, 14 | log_rotate_age: DEFAULT_LOG_ROTATE_AGE, 15 | log_rotate_size: DEFAULT_LOG_ROTATE_SIZE, 16 | pid_path: DEFAULT_PID_PATH, 17 | supervisor: true, 18 | workers: DEFAULT_WORKER_COUNT, 19 | worker_type: "process" 20 | }.freeze 21 | 22 | def self.call(params = {}) 23 | ServerEngine 24 | .create(nil, Worker, DEFAULT_PARAMS.merge(params)) 25 | .run 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /spec/rails_permanent_job_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe RailsPermanentJob do 4 | it "has a version number" do 5 | expect(RailsPermanentJob::VERSION).not_to be nil 6 | end 7 | 8 | it ".jobs=" do 9 | RailsPermanentJob.jobs = :foo 10 | 11 | expect(RailsPermanentJob::Worker::Configuration.jobs).to eq(:foo) 12 | 13 | RailsPermanentJob.jobs = RailsPermanentJob::Worker::Configuration::DEFAULT_JOBS 14 | end 15 | 16 | it ".after_job=" do 17 | RailsPermanentJob.after_job = :foo 18 | 19 | expect(RailsPermanentJob::Worker::Configuration.after_job).to eq(:foo) 20 | 21 | RailsPermanentJob.after_job = RailsPermanentJob::Worker::Configuration::DEFAULT_AFTER_JOB 22 | end 23 | 24 | it ".run" do 25 | allow(RailsPermanentJob::Run).to receive(:call) 26 | 27 | RailsPermanentJob.run(foo: :bar) 28 | 29 | expect(RailsPermanentJob::Run).to have_received(:call).with(foo: :bar) 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /examples/fetch_messages_from_aws_sqs.rb: -------------------------------------------------------------------------------- 1 | require_relative "../lib/rails_permanent_job" 2 | 3 | # 4 | # Recive messages from AWS SQS every 20s 5 | # 6 | 7 | require "aws-sdk-sqs" 8 | require "aws-sdk-sts" 9 | 10 | class ReceiveFromSqs 11 | def self.call(logger:, **_options) 12 | region = "eu-central-1" 13 | queue_name = "RailsPermanentJobQueue" 14 | sts_client = ::Aws::STS::Client.new(region: region) 15 | sqs_client = ::Aws::SQS::Client.new(region: region) 16 | queue_url = "https://sqs.#{region}.amazonaws.com/" \ 17 | "#{sts_client.get_caller_identity.account}/#{queue_name}" 18 | 19 | logger.debug("Receiving messages from queue '#{queue_name}'...") 20 | 21 | response = sqs_client.receive_message(queue_url: queue_url, max_number_of_messages: 10) 22 | logger.debug("No messages to receive") if response.messages.count.zero? 23 | 24 | response.messages.each do |message| 25 | logger.info "Received message #{message.inspect}" 26 | end 27 | end 28 | end 29 | 30 | RailsPermanentJob.jobs = [ReceiveFromSqs] 31 | RailsPermanentJob.after_job = ->(_) { sleep(20) } 32 | RailsPermanentJob.run(workers: 1, log_level: "debug") 33 | -------------------------------------------------------------------------------- /lib/rails_permanent_job/worker.rb: -------------------------------------------------------------------------------- 1 | require_relative "worker/configuration" 2 | 3 | module RailsPermanentJob 4 | module Worker 5 | NoJobsConfigured = Class.new(Error) 6 | 7 | def initialize 8 | logger.info "[RailsPermanentJob] Worker Initialization in process #{Process.pid}" 9 | 10 | @worker_config = { 11 | log_level: config[:log_level], 12 | workers: config[:workers] 13 | } 14 | @after_job = Configuration.after_job 15 | @jobs = Configuration.jobs 16 | @stop_flag = ServerEngine::BlockingFlag.new 17 | end 18 | 19 | def run 20 | raise NoJobsConfigured unless Configuration.jobs.any? 21 | 22 | logger.info "[RailsPermanentJob] Worker Running in process #{Process.pid}" 23 | 24 | until stop_flag_set? 25 | @jobs.each do |job| 26 | job.call(logger: logger, config: @worker_config) 27 | end 28 | 29 | @after_job.call(logger: logger, config: @worker_config) 30 | end 31 | end 32 | 33 | def stop_flag_set? 34 | @stop_flag.set? 35 | end 36 | 37 | def stop 38 | logger.info "[RailsPermanentJob] Worker Stopping" 39 | 40 | @stop_flag.set! 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /rails_permanent_job.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative "lib/rails_permanent_job/version" 4 | 5 | Gem::Specification.new do |spec| 6 | spec.name = "rails_permanent_job" 7 | spec.version = RailsPermanentJob::VERSION 8 | spec.authors = ["Paweł Strzałkowski"] 9 | spec.email = ["pstrzalk@gmail.com"] 10 | 11 | spec.summary = "Permanent job worker runner" 12 | spec.description = "Run a permanent worker with a list of jobs to do indefinitelly." 13 | spec.homepage = "https://github.com/pstrzalk/" 14 | spec.required_ruby_version = ">= 2.6.0" 15 | 16 | spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'" 17 | 18 | spec.metadata["homepage_uri"] = spec.homepage 19 | spec.metadata["source_code_uri"] = "https://github.com/pstrzalk/rails_permanent_job" 20 | spec.metadata["changelog_uri"] = "https://github.com/pstrzalk/rails_permanent_job/CHANGELOG.md" 21 | 22 | spec.files = Dir.chdir(__dir__) do 23 | `git ls-files -z`.split("\x0").reject do |f| 24 | (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor]) 25 | end 26 | end 27 | spec.bindir = "exe" 28 | spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } 29 | spec.require_paths = ["lib"] 30 | 31 | spec.add_development_dependency "rspec", "~> 3.12" 32 | spec.add_development_dependency "standard", "~> 1.28" 33 | spec.add_development_dependency "rubocop", "~> 1.50" 34 | 35 | spec.add_dependency "rake", "~> 13" 36 | spec.add_dependency "serverengine", "~> 2.1" 37 | end 38 | -------------------------------------------------------------------------------- /spec/rails_permanent_job_spec/run_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module RailsPermanentJob 4 | RSpec.describe Run do 5 | describe ".call" do 6 | it 'runs a server engine daemon with default options' do 7 | server_engine_daemon = instance_double(ServerEngine::Daemon) 8 | allow(ServerEngine).to receive(:create).with( 9 | nil, 10 | Worker, 11 | { 12 | daemonize: false, 13 | log_file_name: "rails_permanent_job.log", 14 | log_level: "debug", 15 | log_rotate_age: 5, 16 | log_rotate_size: 1_048_576, 17 | pid_path: "tmp/rails_permanent_job.pid", 18 | supervisor: true, 19 | workers: 1, 20 | worker_type: "process" 21 | } 22 | ).and_return(server_engine_daemon) 23 | expect(server_engine_daemon).to receive(:run).with(no_args) 24 | 25 | Run.call 26 | end 27 | 28 | it 'runs a server engine daemon with overridden options' do 29 | server_engine_daemon = instance_double(ServerEngine::Daemon) 30 | allow(ServerEngine).to receive(:create).with( 31 | nil, 32 | Worker, 33 | { 34 | daemonize: true, 35 | log_file_name: "changed_rails_permanent_job.log", 36 | log_level: "info", 37 | log_rotate_age: 10, 38 | log_rotate_size: 1024, 39 | pid_path: "tmp/changed_rails_permanent_job.pid", 40 | supervisor: false, 41 | workers: 3, 42 | worker_type: "spawn" 43 | } 44 | ).and_return(server_engine_daemon) 45 | expect(server_engine_daemon).to receive(:run).with(no_args) 46 | 47 | Run.call( 48 | daemonize: true, 49 | log_file_name: "changed_rails_permanent_job.log", 50 | log_level: "info", 51 | log_rotate_age: 10, 52 | log_rotate_size: 1024, 53 | pid_path: "tmp/changed_rails_permanent_job.pid", 54 | supervisor: false, 55 | workers: 3, 56 | worker_type: "spawn" 57 | ) 58 | end 59 | end 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | rails_permanent_job (0.2.0) 5 | rake (~> 13) 6 | serverengine (~> 2.1) 7 | 8 | GEM 9 | remote: https://rubygems.org/ 10 | specs: 11 | ast (2.4.2) 12 | diff-lcs (1.5.0) 13 | json (2.6.3) 14 | language_server-protocol (3.17.0.3) 15 | lint_roller (1.0.0) 16 | parallel (1.23.0) 17 | parser (3.2.2.1) 18 | ast (~> 2.4.1) 19 | rainbow (3.1.1) 20 | rake (13.0.6) 21 | regexp_parser (2.8.0) 22 | rexml (3.2.5) 23 | rspec (3.12.0) 24 | rspec-core (~> 3.12.0) 25 | rspec-expectations (~> 3.12.0) 26 | rspec-mocks (~> 3.12.0) 27 | rspec-core (3.12.2) 28 | rspec-support (~> 3.12.0) 29 | rspec-expectations (3.12.3) 30 | diff-lcs (>= 1.2.0, < 2.0) 31 | rspec-support (~> 3.12.0) 32 | rspec-mocks (3.12.5) 33 | diff-lcs (>= 1.2.0, < 2.0) 34 | rspec-support (~> 3.12.0) 35 | rspec-support (3.12.0) 36 | rubocop (1.50.2) 37 | json (~> 2.3) 38 | parallel (~> 1.10) 39 | parser (>= 3.2.0.0) 40 | rainbow (>= 2.2.2, < 4.0) 41 | regexp_parser (>= 1.8, < 3.0) 42 | rexml (>= 3.2.5, < 4.0) 43 | rubocop-ast (>= 1.28.0, < 2.0) 44 | ruby-progressbar (~> 1.7) 45 | unicode-display_width (>= 2.4.0, < 3.0) 46 | rubocop-ast (1.28.0) 47 | parser (>= 3.2.1.0) 48 | rubocop-performance (1.16.0) 49 | rubocop (>= 1.7.0, < 2.0) 50 | rubocop-ast (>= 0.4.0) 51 | ruby-progressbar (1.13.0) 52 | serverengine (2.1.1) 53 | sigdump (~> 0.2.2) 54 | sigdump (0.2.4) 55 | standard (1.28.0) 56 | language_server-protocol (~> 3.17.0.2) 57 | lint_roller (~> 1.0) 58 | rubocop (~> 1.50.2) 59 | standard-custom (~> 1.0.0) 60 | standard-performance (~> 1.0.1) 61 | standard-custom (1.0.0) 62 | lint_roller (~> 1.0) 63 | standard-performance (1.0.1) 64 | lint_roller (~> 1.0) 65 | rubocop-performance (~> 1.16.0) 66 | unicode-display_width (2.4.2) 67 | 68 | PLATFORMS 69 | arm64-darwin-21 70 | 71 | DEPENDENCIES 72 | rails_permanent_job! 73 | rspec (~> 3.12) 74 | rubocop (~> 1.50) 75 | standard (~> 1.28) 76 | 77 | BUNDLED WITH 78 | 2.4.12 79 | -------------------------------------------------------------------------------- /spec/rails_permanent_job_spec/worker_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module RailsPermanentJob 4 | class FooWorker 5 | include Worker 6 | 7 | def logger 8 | @logger ||= Logger.new($stdout) 9 | end 10 | 11 | def config 12 | @config ||= {} 13 | end 14 | end 15 | 16 | class SingleRunFooWorker 17 | include Worker 18 | 19 | def logger 20 | @logger ||= Logger.new($stdout) 21 | end 22 | 23 | def config 24 | @config ||= {} 25 | end 26 | 27 | def stop_flag_set? 28 | return true if @checked_at_least_once 29 | 30 | @checked_at_least_once = true 31 | false 32 | end 33 | end 34 | 35 | RSpec.describe Worker do 36 | it "initializes with configurated jobs" do 37 | Worker::Configuration.jobs = [:foo, :bar] 38 | Worker::Configuration.after_job = :zoo 39 | 40 | foo_worker = FooWorker.new 41 | expect(foo_worker.instance_variable_get(:@jobs)).to eq [:foo, :bar] 42 | expect(foo_worker.instance_variable_get(:@after_job)).to eq :zoo 43 | expect(foo_worker.instance_variable_get(:@stop_flag)).to be_instance_of(ServerEngine::BlockingFlag) 44 | 45 | Worker::Configuration.jobs = Worker::Configuration::DEFAULT_JOBS 46 | Worker::Configuration.after_job = Worker::Configuration::DEFAULT_AFTER_JOB 47 | end 48 | 49 | describe "#run" do 50 | it "raises error when there are no jobs" do 51 | foo_worker = FooWorker.new 52 | 53 | expect { foo_worker.run }.to raise_error(Worker::NoJobsConfigured) 54 | end 55 | 56 | it "runs all the jobs and the after job" do 57 | foo = 0 58 | job1 = ->(_) { foo += 1 } 59 | job2 = ->(_) { foo += 2 } 60 | after_job = ->(_) { foo += 10 } 61 | Worker::Configuration.jobs = [job1, job2] 62 | Worker::Configuration.after_job = after_job 63 | 64 | foo_worker = SingleRunFooWorker.new 65 | foo_worker.run 66 | 67 | expect(foo).to eq(13) 68 | 69 | Worker::Configuration.jobs = Worker::Configuration::DEFAULT_JOBS 70 | Worker::Configuration.after_job = Worker::Configuration::DEFAULT_AFTER_JOB 71 | end 72 | end 73 | 74 | describe "#stop" do 75 | it "sets stop flag" do 76 | foo_worker = SingleRunFooWorker.new 77 | 78 | expect(foo_worker.instance_variable_get(:@stop_flag)).not_to be_set 79 | 80 | foo_worker.stop 81 | 82 | expect(foo_worker.instance_variable_get(:@stop_flag)).to be_set 83 | end 84 | end 85 | end 86 | end 87 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RailsPermanentJob 2 | 3 | RailsPermanentJob is a way to easily run a reliable process performing a set of jobs. It may be easily used to build an independent worker performing a permanent job for a Ruby on Rails application. 4 | 5 | ## Story behind the project 6 | 7 | Check it out on YouTube: [https://youtu.be/nRrxWJ4BExQ](https://youtu.be/nRrxWJ4BExQ) 8 | 9 | Every now and then there is a need to perform a constant check on application's state. It has been often implemented as a cronjob running every minute, as it's the minimal interval for such a solution. 10 | It has always been a flawed approach. Neither performing a job every second or scheduling a future job from a previous one are acceptable or reliable solutions for such a task. 11 | RailsPermanentJob is a project which aims to provide a reliable approach to running a long-lived process alongside a Ruby on Rails application. 12 | 13 | As its core, there is a `serverengine` gem. As [its repository](https://github.com/treasure-data/serverengine) says: 14 | > ServerEngine is a framework to implement robust multiprocess servers like Unicorn. 15 | 16 | `rails_permanent_job` gem simplifies usage of `serverengine` by providing a simple and reusable pattern with additional tooling. 17 | 18 | ## Installation 19 | 20 | ``` 21 | gem 'rails_permanent_job', git: 'https://github.com/pstrzalk/rails_permanent_job' 22 | ``` 23 | 24 | ## Usage 25 | 26 | Review examples in the `/examples` folder to see how to run a permanent job. Another example may be found in the `Rakefile` file included in this project. 27 | 28 | ### Minimal use case for Ruby on Rails 29 | 30 | - install `rails_permanent_job` gem 31 | - add a rake task by creating a file in `PROJECT_ROOT/lib/tasks/permanent_jobs.rake` 32 | - put the following code in the file 33 | ```ruby 34 | class BeepBeep 35 | def self.call(**_options) 36 | puts "Beep Beep" 37 | end 38 | end 39 | 40 | namespace :permanent_jobs do 41 | desc "Run a permanent job" 42 | task foo: :environment do 43 | RailsPermanentJob.jobs = [BeepBeep] 44 | RailsPermanentJob.run(worker_count: 1) 45 | end 46 | end 47 | ``` 48 | - run in CLI with `bundle exec rails permanent_jobs:foo` 49 | - kill with `Control + C` (or a similar combination appropriate for your OS) 50 | 51 | ## Development 52 | 53 | 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. 54 | 55 | ## Contributing 56 | 57 | Bug reports and pull requests are welcome on GitHub at https://github.com/pstrzalk/rails_permanent_job. 58 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------