chronofage 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 708bb7f6a4836dbe1089bc0a2a699fe8fad2740c
4
- data.tar.gz: 2a717b066b6454e01a5c88ac38c8da5e0eda1f77
3
+ metadata.gz: 498b0dbe5d30b21f61a61378999edeb50f9cabc6
4
+ data.tar.gz: 424313e1ee691839fdf396d649b1e8698e767cc6
5
5
  SHA512:
6
- metadata.gz: 170565c678b3c0d277b523657e198ebf1d89e4dd2612955c2550c60166d475088a85a4cd0f81645fbc20bfd870fdc9b4fd7b8f9ab59a12d415260ec7e49e900d
7
- data.tar.gz: 8a39277b4cf3572751ef1a1e2e47b86e6cdcd1530b72319ced3bb604bca8c33d6727a663ed1f0e34e53b7a14549a2f2a8352b8baa0470fb2f4942c3289e970e8
6
+ metadata.gz: 35f775eba0d751a1bc759b36e329ac96db33e9b3bd09f36fa57f2fa7fabbd3551c5907ae0d24e952389e2f7c38fd8c90324d82e9dd88218e14ba54778fbb4ef7
7
+ data.tar.gz: 41c3823387b3f5da300959c5b7b03ef51e3c1d7e82e057925ac68efffe440231cd8034373689911de8f4af812e61cbd0d8900330358235af90393b5addfaccdb
data/README.md CHANGED
@@ -6,7 +6,7 @@ is run in its own process and you can spread the work on multiple hosts.
6
6
 
7
7
  ## Installation
8
8
 
9
- Copy the migration file and run it to create the `chronofage_jobs` and `chronofage_runners` tables.
9
+ Copy the migration file and run it to create the `chronofage_jobs` table.
10
10
 
11
11
  ```
12
12
  rake chronofage_engine:install:migrations db:migrate
@@ -29,17 +29,6 @@ create_table "chronofage_jobs", force: :cascade do |t|
29
29
  end
30
30
  ```
31
31
 
32
- The `chronofage_runners` table hold informations about the process currently running.
33
-
34
- ```
35
- create_table "chronofage_runners", force: :cascade do |t|
36
- t.string "queue_name"
37
- t.string "host"
38
- t.datetime "created_at", null: false
39
- t.datetime "updated_at", null: false
40
- end
41
- ```
42
-
43
32
  ## Usage
44
33
 
45
34
  You can selectively override the ActiveJob backend for your very long-runny task.
@@ -1,15 +1,20 @@
1
1
  class CreateJobTable < ActiveRecord::Migration[5.0]
2
2
  def change
3
3
  create_table :chronofage_jobs do |t|
4
- t.string :job_class
5
- t.string :job_id
6
- t.string :queue_name
7
- t.text :arguments
8
- t.integer :priority
4
+ t.string :job_class
5
+ t.string :job_id
6
+ t.string :queue_name
7
+ t.text :arguments
8
+ t.integer :priority
9
+
10
+ t.string :host
11
+ t.text :stdout
12
+ t.text :stderr
13
+
14
+ t.datetime :started_at
15
+ t.datetime :completed_at
16
+ t.datetime :failed_at
9
17
 
10
- t.datetime :started_at
11
- t.datetime :completed_at
12
- t.datetime :failed_at
13
18
  t.timestamps
14
19
  end
15
20
  end
data/lib/chronofage.rb CHANGED
@@ -3,7 +3,6 @@ require 'active_record/version'
3
3
  require 'active_support/core_ext/module'
4
4
  require 'active_job/queue_adapters/chronofage_adapter'
5
5
  require 'chronofage/job'
6
- require 'chronofage/runner'
7
6
 
8
7
  begin
9
8
  require 'rails/engine'
@@ -3,6 +3,11 @@ module Chronofage
3
3
  self.table_name = "chronofage_jobs"
4
4
 
5
5
  scope :ready, -> { where(started_at: nil) }
6
+ scope :started, -> { where.not(started_at: nil).where(failed_at: nil, completed_at: nil) }
7
+
8
+ def self.next(queue_name)
9
+ ready.where(queue_name: queue_name).order(priority: :asc).first
10
+ end
6
11
 
7
12
  def execute!
8
13
  start!
@@ -14,20 +19,42 @@ module Chronofage
14
19
  end
15
20
 
16
21
  def start!
17
- update(started_at: Time.now)
22
+ update!(started_at: Time.now, host: Chronofage::Job.host)
18
23
  end
19
24
 
20
25
  def done!
21
- update(completed_at: Time.now)
26
+ update!(completed_at: Time.now)
22
27
  end
23
28
 
24
29
  def failed!
25
- update(failed_at: Time.now)
30
+ update!(failed_at: Time.now)
26
31
  end
27
32
 
28
33
  def deserialized_arguments
29
34
  ActiveJob::Arguments.deserialize(JSON.parse(arguments))
30
35
  end
31
36
 
37
+ def concurrents
38
+ Chronofage::Job.started.where(queue_name: queue_name, host: Chronofage::Job.host)
39
+ end
40
+
41
+ def state
42
+ if started_at.present? && completed_at.present?
43
+ :completed
44
+ elsif started_at.present? && failed_at.present?
45
+ :failed
46
+ elsif started_at.present?
47
+ :running
48
+ else
49
+ :ready
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def self.host
56
+ Socket.gethostname
57
+ end
58
+
32
59
  end
33
60
  end
@@ -1,3 +1,3 @@
1
1
  module Chronofage
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,17 +1,9 @@
1
1
  namespace :chronofage_engine do
2
2
  namespace :jobs do
3
3
  task :execute, [:queue_name, :concurrency] => :environment do |t, args|
4
- job = Chronofage::Job.ready.where(queue_name: args.queue_name).order(priority: :asc).first
5
-
6
- if job.nil?
7
- Rails.logger.info "chronofage[#{args.queue_name}]: no job to execute."
8
- else
9
- runner = Chronofage::Runner.register!(args.queue_name, args.concurrency.to_i)
10
- begin
11
- job.execute!
12
- ensure
13
- runner.unregister!
14
- end
4
+ job = Chronofage::Job.next(args.queue_name)
5
+ if job.present? && job.concurrents.count < args.concurrency.to_i
6
+ job.execute!
15
7
  end
16
8
  end
17
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chronofage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Goya
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-11-27 00:00:00.000000000 Z
12
+ date: 2017-11-29 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Cron based job scheduler
15
15
  email:
@@ -25,12 +25,10 @@ files:
25
25
  - Rakefile
26
26
  - chronofage.gemspec
27
27
  - db/migrate/1_create_job_table.rb
28
- - db/migrate/2_create_runner_table.rb
29
28
  - lib/active_job/queue_adapters/chronofage_adapter.rb
30
29
  - lib/chronofage.rb
31
30
  - lib/chronofage/engine.rb
32
31
  - lib/chronofage/job.rb
33
- - lib/chronofage/runner.rb
34
32
  - lib/chronofage/version.rb
35
33
  - lib/tasks/chronofage.rake
36
34
  homepage: https://www.amusenetwork.com/
@@ -1,10 +0,0 @@
1
- class CreateRunnerTable < ActiveRecord::Migration[5.0]
2
- def change
3
- create_table :chronofage_runners do |t|
4
- t.string :queue_name
5
- t.string :host
6
-
7
- t.timestamps
8
- end
9
- end
10
- end
@@ -1,33 +0,0 @@
1
- require 'socket'
2
-
3
- module Chronofage
4
- class Runner < ::ActiveRecord::Base
5
- self.table_name = "chronofage_runners"
6
-
7
- class MaxConcurrencyReached < StandardError
8
- end
9
-
10
- def self.register!(queue_name, concurrency)
11
- if concurrent_runnners(queue_name).count >= concurrency
12
- raise MaxConcurrencyReached
13
- else
14
- create!(queue_name: queue_name, host: host)
15
- end
16
- end
17
-
18
- def self.concurrent_runnners(queue_name)
19
- where(queue_name: queue_name, host: host)
20
- end
21
-
22
- def unregister!
23
- destroy!
24
- end
25
-
26
- private
27
-
28
- def self.host
29
- Socket.gethostname
30
- end
31
-
32
- end
33
- end