cuetip 1.4.1 → 2.0.0
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 +4 -4
- data/lib/cuetip/config.rb +2 -1
- data/lib/cuetip/models/job.rb +64 -59
- data/lib/cuetip/version.rb +1 -1
- data/lib/cuetip/worker_group.rb +23 -19
- metadata +26 -6
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 6c55fc81c458fe59398107580afc01e293ad447e04627345adf0fce1a65bb58d
         | 
| 4 | 
            +
              data.tar.gz: 05a2f8814e4f40d6e665d19015cb3dd24b371c5ea9371a47d5e37a47f3ef7ff0
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: ff6e9e82ea4f10c7e1980eabb0b24cf234c76aea8ec310d494ab258844abbd7c1fecfec5db0953a38634bfcdecf3f4ffd64f31c900ff0b39db367535177b42f6
         | 
| 7 | 
            +
              data.tar.gz: 349d426cda2a0b3651ad61cb93d66b4db07ebcf32941ac852f1b069001d7d80a78d9f73fb7e79fafe5996ba42833efdfcf2cb5c105950e3e0f3bca31eb13220d
         | 
    
        data/lib/cuetip/config.rb
    CHANGED
    
    | @@ -1,5 +1,6 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 2 |  | 
| 3 | 
            +
            require 'klogger'
         | 
| 3 4 | 
             
            require 'active_support'
         | 
| 4 5 | 
             
            require 'active_support/core_ext/numeric/bytes'
         | 
| 5 6 | 
             
            require 'active_support/core_ext/numeric/time'
         | 
| @@ -20,7 +21,7 @@ module Cuetip | |
| 20 21 |  | 
| 21 22 | 
             
                # Return the logger
         | 
| 22 23 | 
             
                def logger
         | 
| 23 | 
            -
                  @logger ||=  | 
| 24 | 
            +
                  @logger ||= Klogger.new(:cuetip)
         | 
| 24 25 | 
             
                end
         | 
| 25 26 | 
             
                attr_writer :logger
         | 
| 26 27 |  | 
    
        data/lib/cuetip/models/job.rb
    CHANGED
    
    | @@ -49,11 +49,11 @@ module Cuetip | |
| 49 49 | 
             
                  def remove_from_queue
         | 
| 50 50 | 
             
                    queued_job&.destroy
         | 
| 51 51 | 
             
                    self.queued_job = nil
         | 
| 52 | 
            -
                     | 
| 52 | 
            +
                    Cuetip.logger.debug 'Removed from queue'
         | 
| 53 53 |  | 
| 54 54 | 
             
                    if delete_after_execution?
         | 
| 55 55 | 
             
                      destroy
         | 
| 56 | 
            -
                       | 
| 56 | 
            +
                      Cuetip.logger.debug 'Removed job from database'
         | 
| 57 57 | 
             
                    end
         | 
| 58 58 |  | 
| 59 59 | 
             
                  end
         | 
| @@ -61,74 +61,78 @@ module Cuetip | |
| 61 61 | 
             
                  # Log some text about this job
         | 
| 62 62 | 
             
                  #
         | 
| 63 63 | 
             
                  # @param text [String]
         | 
| 64 | 
            -
                  def log(text)
         | 
| 65 | 
            -
                    Cuetip.logger.info  | 
| 64 | 
            +
                  def log(text, **tags)
         | 
| 65 | 
            +
                    Cuetip.logger.info text, **tags
         | 
| 66 66 | 
             
                  end
         | 
| 67 67 |  | 
| 68 68 | 
             
                  # Execute the job
         | 
| 69 69 | 
             
                  #
         | 
| 70 70 | 
             
                  # @return [Boolean] whether the job executed successfully or not
         | 
| 71 71 | 
             
                  def execute(&block)
         | 
| 72 | 
            -
                     | 
| 73 | 
            -
             | 
| 74 | 
            -
             | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 77 | 
            -
             | 
| 78 | 
            -
             | 
| 79 | 
            -
                       | 
| 80 | 
            -
                       | 
| 81 | 
            -
             | 
| 82 | 
            -
             | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 72 | 
            +
                    Klogger.tagged(job_id: id, job_class: class_name) do
         | 
| 73 | 
            +
                      begin
         | 
| 74 | 
            +
                      Cuetip.logger.info "Executing"
         | 
| 75 | 
            +
                      # Initialize a new instance of the job we wish to execute
         | 
| 76 | 
            +
                      job_klass = class_name.constantize.new(self)
         | 
| 77 | 
            +
             | 
| 78 | 
            +
                      # If the job has expired, we should not be executing this so we'll just
         | 
| 79 | 
            +
                      # remove it from the queue and mark it as expired.
         | 
| 80 | 
            +
                      if expired?
         | 
| 81 | 
            +
                        Cuetip.logger.warn 'Job has expired, removed from queue'
         | 
| 82 | 
            +
                        self.status = 'Expired'
         | 
| 83 | 
            +
                        remove_from_queue
         | 
| 84 | 
            +
                        Cuetip.config.emit(:expired, self, job_klass)
         | 
| 85 | 
            +
                        return false
         | 
| 86 | 
            +
                      end
         | 
| 85 87 |  | 
| 86 | 
            -
             | 
| 88 | 
            +
                      Cuetip.config.emit(:before_perform, self, job_klass)
         | 
| 87 89 |  | 
| 88 | 
            -
             | 
| 89 | 
            -
             | 
| 90 | 
            -
             | 
| 90 | 
            +
                      # If we have a block, call this so we can manipulate our actual job class
         | 
| 91 | 
            +
                      # before execution if needed (mostly for testing)
         | 
| 92 | 
            +
                      block.call(job_klass) if block_given?
         | 
| 91 93 |  | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            +
                      # Mark the job as runnign
         | 
| 95 | 
            +
                      update!(status: 'Running', started_at: Time.now, executions: executions + 1)
         | 
| 94 96 |  | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
| 99 | 
            -
             | 
| 100 | 
            -
                      # Mark the job as complete and remove it from the queue
         | 
| 101 | 
            -
                      self.status = 'Complete'
         | 
| 102 | 
            -
                      log 'Job completed successfully'
         | 
| 103 | 
            -
                      remove_from_queue
         | 
| 104 | 
            -
             | 
| 105 | 
            -
                      Cuetip.config.emit(:completed, self, job_klass)
         | 
| 106 | 
            -
             | 
| 107 | 
            -
                      true
         | 
| 108 | 
            -
                    rescue Exception, Timeout::TimeoutError => e
         | 
| 109 | 
            -
                      log "Job failed with #{e.class} (#{e.message})"
         | 
| 110 | 
            -
             | 
| 111 | 
            -
                      # If there's an error, mark the job as failed and copy exception
         | 
| 112 | 
            -
                      # data into the job
         | 
| 113 | 
            -
                      self.status = 'Failed'
         | 
| 114 | 
            -
                      self.exception_class = e.class.name
         | 
| 115 | 
            -
                      self.exception_message = e.message
         | 
| 116 | 
            -
                      self.exception_backtrace = e.backtrace.join("\n")
         | 
| 117 | 
            -
             | 
| 118 | 
            -
                      # Handle requeing the job if needed.
         | 
| 119 | 
            -
                      if requeue_on_failure?
         | 
| 120 | 
            -
                        # Requeue this job for execution again after the retry interval.
         | 
| 121 | 
            -
                        new_job = queued_job.requeue(run_after: Time.now + retry_interval.to_i)
         | 
| 122 | 
            -
                        log "Requeing job to run after #{new_job.run_after.to_s(:long)}"
         | 
| 123 | 
            -
                        self.status = 'Pending'
         | 
| 124 | 
            -
                      else
         | 
| 125 | 
            -
                        # We're done with this job. We can't do any more retries.
         | 
| 126 | 
            -
                        remove_from_queue
         | 
| 127 | 
            -
                      end
         | 
| 97 | 
            +
                      begin
         | 
| 98 | 
            +
                        # Perform the job within a timeout
         | 
| 99 | 
            +
                        Timeout.timeout(maximum_execution_time || 1.year) do
         | 
| 100 | 
            +
                          job_klass.perform
         | 
| 101 | 
            +
                        end
         | 
| 128 102 |  | 
| 129 | 
            -
             | 
| 103 | 
            +
                        # Mark the job as complete and remove it from the queue
         | 
| 104 | 
            +
                        self.status = 'Complete'
         | 
| 105 | 
            +
                        Cuetip.logger.info 'Execution complete'
         | 
| 106 | 
            +
                        remove_from_queue
         | 
| 130 107 |  | 
| 131 | 
            -
             | 
| 108 | 
            +
                        Cuetip.config.emit(:completed, self, job_klass)
         | 
| 109 | 
            +
             | 
| 110 | 
            +
                        true
         | 
| 111 | 
            +
                      rescue Exception, Timeout::TimeoutError => e
         | 
| 112 | 
            +
                        Cuetip.logger.exception(e, "Job failed")
         | 
| 113 | 
            +
             | 
| 114 | 
            +
                        # If there's an error, mark the job as failed and copy exception
         | 
| 115 | 
            +
                        # data into the job
         | 
| 116 | 
            +
                        self.status = 'Failed'
         | 
| 117 | 
            +
                        self.exception_class = e.class.name
         | 
| 118 | 
            +
                        self.exception_message = e.message
         | 
| 119 | 
            +
                        self.exception_backtrace = e.backtrace.join("\n")
         | 
| 120 | 
            +
             | 
| 121 | 
            +
                        # Handle requeing the job if needed.
         | 
| 122 | 
            +
                        if requeue_on_failure?
         | 
| 123 | 
            +
                          # Requeue this job for execution again after the retry interval.
         | 
| 124 | 
            +
                          new_job = queued_job.requeue(run_after: Time.now + retry_interval.to_i)
         | 
| 125 | 
            +
                          Cuetip.logger.info "Requeueing job", run_after: new_job.run_after.to_s(:long)
         | 
| 126 | 
            +
                          self.status = 'Pending'
         | 
| 127 | 
            +
                        else
         | 
| 128 | 
            +
                          # We're done with this job. We can't do any more retries.
         | 
| 129 | 
            +
                          remove_from_queue
         | 
| 130 | 
            +
                        end
         | 
| 131 | 
            +
             | 
| 132 | 
            +
                        Cuetip.config.emit(:exception, e, self, job_klass)
         | 
| 133 | 
            +
             | 
| 134 | 
            +
                        false
         | 
| 135 | 
            +
                      end
         | 
| 132 136 | 
             
                    end
         | 
| 133 137 | 
             
                  ensure
         | 
| 134 138 | 
             
                    unless destroyed?
         | 
| @@ -136,8 +140,9 @@ module Cuetip | |
| 136 140 | 
             
                      save!
         | 
| 137 141 | 
             
                    end
         | 
| 138 142 | 
             
                    Cuetip.config.emit(:finished, self, job_klass)
         | 
| 139 | 
            -
                     | 
| 143 | 
            +
                    Cuetip.logger.debug 'Finished processing'
         | 
| 140 144 | 
             
                  end
         | 
| 141 145 | 
             
                end
         | 
| 146 | 
            +
                end
         | 
| 142 147 | 
             
              end
         | 
| 143 148 | 
             
            end
         | 
    
        data/lib/cuetip/version.rb
    CHANGED
    
    
    
        data/lib/cuetip/worker_group.rb
    CHANGED
    
    | @@ -20,30 +20,34 @@ module Cuetip | |
| 20 20 | 
             
                end
         | 
| 21 21 |  | 
| 22 22 | 
             
                def start
         | 
| 23 | 
            -
                   | 
| 24 | 
            -
             | 
| 25 | 
            -
                    @queues. | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
                  exit_trap = proc do
         | 
| 29 | 
            -
                    @workers.each { |_, worker| worker.request_exit! }
         | 
| 30 | 
            -
                    puts 'Exiting...'
         | 
| 31 | 
            -
                  end
         | 
| 23 | 
            +
                  Klogger.tagged(pid: Process.pid) do
         | 
| 24 | 
            +
                    Cuetip.logger.info "Starting workers", quantity: @quantity
         | 
| 25 | 
            +
                    if @queues.any?
         | 
| 26 | 
            +
                      @queues.each { |q| Cuetip.logger.info "Joined queue", queue: q }
         | 
| 27 | 
            +
                    end
         | 
| 32 28 |  | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 29 | 
            +
                    exit_trap = proc do
         | 
| 30 | 
            +
                      @workers.each { |_, worker| worker.request_exit! }
         | 
| 31 | 
            +
                      puts 'Exiting...'
         | 
| 32 | 
            +
                    end
         | 
| 35 33 |  | 
| 36 | 
            -
             | 
| 37 | 
            -
                     | 
| 38 | 
            -
             | 
| 39 | 
            -
                    @ | 
| 40 | 
            -
                       | 
| 41 | 
            -
             | 
| 34 | 
            +
                    trap('INT', &exit_trap)
         | 
| 35 | 
            +
                    trap('TERM', &exit_trap)
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                    @quantity.times do |i|
         | 
| 38 | 
            +
                      @workers[i] = Worker.new(self, i, @queues)
         | 
| 39 | 
            +
                      Klogger.tagged worker_index: i do
         | 
| 40 | 
            +
                        Cuetip.logger.info "Starting worker"
         | 
| 41 | 
            +
                        @threads[i] = Thread.new(@workers[i]) do |worker|
         | 
| 42 | 
            +
                          run_callbacks :run_worker do
         | 
| 43 | 
            +
                            worker.run
         | 
| 44 | 
            +
                          end
         | 
| 45 | 
            +
                        end
         | 
| 42 46 | 
             
                      end
         | 
| 47 | 
            +
                      @threads[i].abort_on_exception = true
         | 
| 43 48 | 
             
                    end
         | 
| 44 | 
            -
                    @threads | 
| 49 | 
            +
                    @threads.values.each(&:join)
         | 
| 45 50 | 
             
                  end
         | 
| 46 | 
            -
                  @threads.values.each(&:join)
         | 
| 47 51 | 
             
                end
         | 
| 48 52 |  | 
| 49 53 | 
             
                def set_process_name
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: cuetip
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version:  | 
| 4 | 
            +
              version: 2.0.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Adam Cooke
         | 
| 8 | 
            -
            autorequire: | 
| 8 | 
            +
            autorequire:
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date:  | 
| 11 | 
            +
            date: 2023-03-10 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: activerecord
         | 
| @@ -38,6 +38,26 @@ dependencies: | |
| 38 38 | 
             
                - - ">="
         | 
| 39 39 | 
             
                  - !ruby/object:Gem::Version
         | 
| 40 40 | 
             
                    version: '0'
         | 
| 41 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 42 | 
            +
              name: klogger-logger
         | 
| 43 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 44 | 
            +
                requirements:
         | 
| 45 | 
            +
                - - ">="
         | 
| 46 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 47 | 
            +
                    version: '1.3'
         | 
| 48 | 
            +
                - - "<"
         | 
| 49 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 50 | 
            +
                    version: '2.0'
         | 
| 51 | 
            +
              type: :runtime
         | 
| 52 | 
            +
              prerelease: false
         | 
| 53 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 54 | 
            +
                requirements:
         | 
| 55 | 
            +
                - - ">="
         | 
| 56 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 57 | 
            +
                    version: '1.3'
         | 
| 58 | 
            +
                - - "<"
         | 
| 59 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 60 | 
            +
                    version: '2.0'
         | 
| 41 61 | 
             
            description: An ActiveRecord job queueing system
         | 
| 42 62 | 
             
            email:
         | 
| 43 63 | 
             
            - me@adamcooke.io
         | 
| @@ -66,7 +86,7 @@ homepage: https://github.com/adamcooke/cuetip | |
| 66 86 | 
             
            licenses:
         | 
| 67 87 | 
             
            - MIT
         | 
| 68 88 | 
             
            metadata: {}
         | 
| 69 | 
            -
            post_install_message: | 
| 89 | 
            +
            post_install_message:
         | 
| 70 90 | 
             
            rdoc_options: []
         | 
| 71 91 | 
             
            require_paths:
         | 
| 72 92 | 
             
            - lib
         | 
| @@ -81,8 +101,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 81 101 | 
             
                - !ruby/object:Gem::Version
         | 
| 82 102 | 
             
                  version: '0'
         | 
| 83 103 | 
             
            requirements: []
         | 
| 84 | 
            -
            rubygems_version: 3. | 
| 85 | 
            -
            signing_key: | 
| 104 | 
            +
            rubygems_version: 3.2.32
         | 
| 105 | 
            +
            signing_key:
         | 
| 86 106 | 
             
            specification_version: 4
         | 
| 87 107 | 
             
            summary: An ActiveRecord job queueing system
         | 
| 88 108 | 
             
            test_files: []
         |