Skip to content

Commit 868857c

Browse files
committed
498
1 parent 99e1501 commit 868857c

File tree

12 files changed

+66
-5
lines changed

12 files changed

+66
-5
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
class WelcomeController < ApplicationController
22
def index
3+
if params[:jobs]
4+
params[:jobs].to_i.times { ExpensiveBackgroundJob.perform_later(rand) }
5+
end
36
end
47
end

app/jobs/application_job.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,13 @@ class ApplicationJob < ActiveJob::Base
44

55
# Most jobs are safe to ignore if the underlying records are no longer available
66
# discard_on ActiveJob::DeserializationError
7+
8+
def gpu_queue_full?
9+
active_jobs = SolidQueue::ClaimedExecution
10+
.joins(:job)
11+
.where(solid_queue_jobs: { queue_name: "gpu" })
12+
.count
13+
14+
active_jobs >= GPU_QUEUE_SIZE
15+
end
716
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class ExpensiveBackgroundJob < ApplicationJob
2+
queue_as do
3+
gpu_queue_full? ? :gpuexternal : :gpu
4+
end
5+
6+
def perform(some_record)
7+
if self.queue_name == "gpu"
8+
Gpus::Processor.execute(some_record)
9+
elsif self.queue_name == "gpuexternal"
10+
Gpus::ExternalProcessor.execute(some_record)
11+
end
12+
end
13+
end

app/models/gpus/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Gpus::Base
2+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Gpus::ExternalProcessor < Gpus::Base
2+
def self.execute(some_record)
3+
Rails.logger.info "✅ gpuexternal #{some_record}"
4+
sleep 5
5+
Rails.logger.info "🏁 gpuexternal #{some_record}"
6+
end
7+
end

app/models/gpus/processor.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Gpus::Processor < Gpus::Base
2+
def self.execute(some_record)
3+
Rails.logger.info "✅ gpu #{some_record}"
4+
sleep 5
5+
Rails.logger.info "🏁 gpu #{some_record}"
6+
end
7+
end

app/views/welcome/index.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<div>
22
<h1 class="font-bold text-4xl">Welcome#index</h1>
33
<p>Find me in app/views/welcome/index.html.erb</p>
4+
5+
<%= link_to "Queue 1 Job", root_path(jobs: 1), "data-turbo-prefetch": false %>
6+
<%= link_to "Queue 10 Jobs", root_path(jobs: 10), "data-turbo-prefetch": false %>
47
</div>

config/database.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
#
77
default: &default
88
adapter: sqlite3
9-
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
9+
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 50 } %>
1010
timeout: 5000
1111

1212
development:
13-
<<: *default
14-
database: storage/development.sqlite3
13+
primary:
14+
<<: *default
15+
database: storage/development.sqlite3
16+
queue:
17+
<<: *default
18+
database: storage/production_queue.sqlite3
19+
migrations_paths: db/queue_migrate
1520

1621
# Warning: The database defined as "test" will be erased and
1722
# re-generated from your development database when you run "rake".

config/environments/development.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,7 @@
6969

7070
# Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
7171
# config.generators.apply_rubocop_autocorrect_after_generate!
72+
73+
config.active_job.queue_adapter = :solid_queue
74+
config.solid_queue.connects_to = { database: { writing: :queue } }
7275
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GPU_QUEUE_SIZE = 3

0 commit comments

Comments
 (0)