plok 0.2.7 → 0.2.8
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/app/models/queued_task.rb +28 -8
 - data/db/migrate/20211119095633_add_weight_to_queued_tasks.rb +7 -0
 - data/db/migrate/20211119103846_add_attempts_to_queued_tasks.rb +7 -0
 - data/db/migrate/20211119123900_add_index_to_queued_tasks_locked.rb +7 -0
 - data/lib/plok/version.rb +1 -1
 - data/spec/factories/queued_tasks.rb +1 -0
 - metadata +10 -7
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: e19b5d0848888824ff2eb0d4ec7a667dd745a10641abc54e82182296eaf0f02f
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: ec066ca66b5ea4cc671d540343d824794045e2b1741307ea1eb19897d35c9f9a
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 2d59516ea8336cd4cf8ee48d94381862129ca87317d9db55c0031c6c560f4a857c83ca99e0f248f08bf49885d3fcb99db3ad6489a3261bfa13b827da6d45aa53
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 82d00ed77cb76bcdf41df081c0e98f5a68ca7ccd78388298ad89a41e95ab8433763c5f084d97f27cbd2e2aa66a8815fee4528493cc586a5b405552d6fcf248d4
         
     | 
    
        data/app/models/queued_task.rb
    CHANGED
    
    | 
         @@ -10,12 +10,15 @@ class QueuedTask < ActiveRecord::Base 
     | 
|
| 
       10 
10 
     | 
    
         
             
              scope :in_past, -> { where(perform_at: nil).or(where('perform_at <= ?', Time.zone.now)) }
         
     | 
| 
       11 
11 
     | 
    
         
             
              scope :in_future, -> { where('perform_at > ?', Time.zone.now) }
         
     | 
| 
       12 
12 
     | 
    
         | 
| 
      
 13 
     | 
    
         
            +
              DEFAULT_PRIORITY = 0
         
     | 
| 
      
 14 
     | 
    
         
            +
              HIGH_PRIORITY = 10
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
       13 
16 
     | 
    
         
             
              def lock!
         
     | 
| 
       14 
     | 
    
         
            -
                update_attribute 
     | 
| 
      
 17 
     | 
    
         
            +
                update_attribute(:locked, true)
         
     | 
| 
       15 
18 
     | 
    
         
             
              end
         
     | 
| 
       16 
19 
     | 
    
         | 
| 
       17 
20 
     | 
    
         
             
              def unlock!
         
     | 
| 
       18 
     | 
    
         
            -
                update_attribute 
     | 
| 
      
 21 
     | 
    
         
            +
                update_attribute(:locked, false)
         
     | 
| 
       19 
22 
     | 
    
         
             
              end
         
     | 
| 
       20 
23 
     | 
    
         | 
| 
       21 
24 
     | 
    
         
             
              def unlocked?
         
     | 
| 
         @@ -26,22 +29,36 @@ class QueuedTask < ActiveRecord::Base 
     | 
|
| 
       26 
29 
     | 
    
         
             
                klass.to_s.constantize.new(data).execute!
         
     | 
| 
       27 
30 
     | 
    
         
             
              end
         
     | 
| 
       28 
31 
     | 
    
         | 
| 
       29 
     | 
    
         
            -
               
     | 
| 
       30 
     | 
    
         
            -
             
     | 
| 
      
 32 
     | 
    
         
            +
              # TODO: Might be good to use named parameters for data and weight here.
         
     | 
| 
      
 33 
     | 
    
         
            +
              # Might be able to use the data var to store weight like the perform_at key.
         
     | 
| 
      
 34 
     | 
    
         
            +
              #
         
     | 
| 
      
 35 
     | 
    
         
            +
              # TODO: Refactor to a separate class.
         
     | 
| 
      
 36 
     | 
    
         
            +
              def self.queue(klass, data, weight = DEFAULT_PRIORITY)
         
     | 
| 
      
 37 
     | 
    
         
            +
                task = create!(
         
     | 
| 
      
 38 
     | 
    
         
            +
                  klass: klass.to_s,
         
     | 
| 
      
 39 
     | 
    
         
            +
                  weight: weight,
         
     | 
| 
      
 40 
     | 
    
         
            +
                  attempts: 0,
         
     | 
| 
      
 41 
     | 
    
         
            +
                  data: data.except(:perform_at)
         
     | 
| 
      
 42 
     | 
    
         
            +
                )
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
       31 
44 
     | 
    
         
             
                task.update(perform_at: data[:perform_at]) if data[:perform_at].present?
         
     | 
| 
       32 
45 
     | 
    
         
             
                task
         
     | 
| 
       33 
46 
     | 
    
         
             
              end
         
     | 
| 
       34 
47 
     | 
    
         | 
| 
       35 
     | 
    
         
            -
              def self.queue_unless_already_queued(klass, data)
         
     | 
| 
       36 
     | 
    
         
            -
                task = find_by(klass: klass, data: data)
         
     | 
| 
      
 48 
     | 
    
         
            +
              def self.queue_unless_already_queued(klass, data, weight = DEFAULT_PRIORITY)
         
     | 
| 
      
 49 
     | 
    
         
            +
                task = find_by(klass: klass, data: data, weight: weight)
         
     | 
| 
       37 
50 
     | 
    
         
             
                return task if task.present?
         
     | 
| 
       38 
     | 
    
         
            -
                self.queue(klass, data)
         
     | 
| 
      
 51 
     | 
    
         
            +
                self.queue(klass, data, weight)
         
     | 
| 
       39 
52 
     | 
    
         
             
              end
         
     | 
| 
       40 
53 
     | 
    
         | 
| 
       41 
54 
     | 
    
         
             
              def dequeue!
         
     | 
| 
       42 
55 
     | 
    
         
             
                destroy
         
     | 
| 
       43 
56 
     | 
    
         
             
              end
         
     | 
| 
       44 
57 
     | 
    
         | 
| 
      
 58 
     | 
    
         
            +
              def increase_attempts!
         
     | 
| 
      
 59 
     | 
    
         
            +
                update_column(:attempts, attempts + 1)
         
     | 
| 
      
 60 
     | 
    
         
            +
              end
         
     | 
| 
      
 61 
     | 
    
         
            +
             
     | 
| 
       45 
62 
     | 
    
         
             
              def process!
         
     | 
| 
       46 
63 
     | 
    
         
             
                lock!
         
     | 
| 
       47 
64 
     | 
    
         | 
| 
         @@ -51,7 +68,10 @@ class QueuedTask < ActiveRecord::Base 
     | 
|
| 
       51 
68 
     | 
    
         
             
                rescue
         
     | 
| 
       52 
69 
     | 
    
         
             
                  raise
         
     | 
| 
       53 
70 
     | 
    
         
             
                ensure
         
     | 
| 
       54 
     | 
    
         
            -
                   
     | 
| 
      
 71 
     | 
    
         
            +
                  if persisted?
         
     | 
| 
      
 72 
     | 
    
         
            +
                    increase_attempts!
         
     | 
| 
      
 73 
     | 
    
         
            +
                    unlock!
         
     | 
| 
      
 74 
     | 
    
         
            +
                  end
         
     | 
| 
       55 
75 
     | 
    
         
             
                end
         
     | 
| 
       56 
76 
     | 
    
         
             
              end
         
     | 
| 
       57 
77 
     | 
    
         | 
    
        data/lib/plok/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: plok
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.2. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.2.8
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Davy Hellemans
         
     | 
| 
         @@ -9,7 +9,7 @@ authors: 
     | 
|
| 
       9 
9 
     | 
    
         
             
            autorequire:
         
     | 
| 
       10 
10 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       11 
11 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       12 
     | 
    
         
            -
            date: 2021- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2021-11-19 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: rails
         
     | 
| 
         @@ -29,16 +29,16 @@ dependencies: 
     | 
|
| 
       29 
29 
     | 
    
         
             
              name: mysql2
         
     | 
| 
       30 
30 
     | 
    
         
             
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
       31 
31 
     | 
    
         
             
                requirements:
         
     | 
| 
       32 
     | 
    
         
            -
                - - " 
     | 
| 
      
 32 
     | 
    
         
            +
                - - "~>"
         
     | 
| 
       33 
33 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       34 
     | 
    
         
            -
                    version: '0'
         
     | 
| 
       35 
     | 
    
         
            -
              type: : 
     | 
| 
      
 34 
     | 
    
         
            +
                    version: '0.5'
         
     | 
| 
      
 35 
     | 
    
         
            +
              type: :runtime
         
     | 
| 
       36 
36 
     | 
    
         
             
              prerelease: false
         
     | 
| 
       37 
37 
     | 
    
         
             
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
       38 
38 
     | 
    
         
             
                requirements:
         
     | 
| 
       39 
     | 
    
         
            -
                - - " 
     | 
| 
      
 39 
     | 
    
         
            +
                - - "~>"
         
     | 
| 
       40 
40 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       41 
     | 
    
         
            -
                    version: '0'
         
     | 
| 
      
 41 
     | 
    
         
            +
                    version: '0.5'
         
     | 
| 
       42 
42 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       43 
43 
     | 
    
         
             
              name: rspec-rails
         
     | 
| 
       44 
44 
     | 
    
         
             
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
         @@ -86,6 +86,9 @@ files: 
     | 
|
| 
       86 
86 
     | 
    
         
             
            - config/routes.rb
         
     | 
| 
       87 
87 
     | 
    
         
             
            - db/migrate/20211008130809_create_plok_logs.rb
         
     | 
| 
       88 
88 
     | 
    
         
             
            - db/migrate/20211015141837_create_plok_queued_tasks.rb
         
     | 
| 
      
 89 
     | 
    
         
            +
            - db/migrate/20211119095633_add_weight_to_queued_tasks.rb
         
     | 
| 
      
 90 
     | 
    
         
            +
            - db/migrate/20211119103846_add_attempts_to_queued_tasks.rb
         
     | 
| 
      
 91 
     | 
    
         
            +
            - db/migrate/20211119123900_add_index_to_queued_tasks_locked.rb
         
     | 
| 
       89 
92 
     | 
    
         
             
            - lib/plok.rb
         
     | 
| 
       90 
93 
     | 
    
         
             
            - lib/plok/engine.rb
         
     | 
| 
       91 
94 
     | 
    
         
             
            - lib/plok/version.rb
         
     |