Skip to content

Commit af1ea04

Browse files
authored
Merge pull request #652 from code0-tech/647-send-updates-to-runtime
Send updates to runtime on flow creation
2 parents b2f7cc0 + 443a13a commit af1ea04

File tree

10 files changed

+104
-24
lines changed

10 files changed

+104
-24
lines changed

app/grpc/flow_handler.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@ class FlowHandler < Tucana::Sagittarius::FlowService::Service
77

88
grpc_stream :update
99

10-
def self.update_started(runtime_id)
11-
runtime = Runtime.find(runtime_id)
12-
runtime.connected!
13-
runtime.save
14-
15-
logger.info(message: 'Runtime connected', runtime_id: runtime.id)
16-
10+
def self.update_runtime(runtime)
1711
flows = []
1812
runtime.projects.each do |project|
1913
project.flows.each do |flow|
@@ -33,6 +27,16 @@ def self.update_started(runtime_id)
3327
)
3428
end
3529

30+
def self.update_started(runtime_id)
31+
runtime = Runtime.find(runtime_id)
32+
runtime.connected!
33+
runtime.save
34+
35+
logger.info(message: 'Runtime connected', runtime_id: runtime.id)
36+
37+
update_runtime(runtime)
38+
end
39+
3640
def self.update_died(runtime_id)
3741
runtime = Runtime.find(runtime_id)
3842
runtime.disconnected!

app/jobs/application_job.rb

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,29 @@ class ApplicationJob < ActiveJob::Base
88
# Most jobs are safe to ignore if the underlying records are no longer available
99
# discard_on ActiveJob::DeserializationError
1010

11-
retry_on StandardError, wait: :polynomially_longer, attempts: 10
11+
ActiveJob::Base.instance_eval do
12+
retry_on StandardError, wait: :polynomially_longer, attempts: 10
1213

13-
before_enqueue do |job|
14-
next if job.arguments.first&.key?(:sagittarius_context)
14+
before_enqueue do |job|
15+
possible_context = job.arguments.last
16+
next if possible_context.is_a?(Hash) && possible_context&.try(:key?, :sagittarius_context)
1517

16-
job.arguments.unshift Code0::ZeroTrack::Context.current.to_h.merge(sagittarius_context: true)
17-
end
18+
job.arguments << Code0::ZeroTrack::Context.current.to_h.merge(sagittarius_context: true)
19+
end
1820

19-
around_perform do |job, block|
20-
context = job.arguments.shift
21-
context.delete(:sagittarius_context)
22-
source_application = context.fetch(Code0::ZeroTrack::Context.log_key(:application), nil)
23-
Code0::ZeroTrack::Context.with_context(
24-
**context,
25-
application: 'good_job',
26-
source_application: source_application,
27-
job_id: job.job_id,
28-
job_class: self.class.name
29-
) do
30-
block.call
21+
around_perform do |job, block|
22+
context = job.arguments.pop
23+
context.delete(:sagittarius_context)
24+
source_application = context.fetch(Code0::ZeroTrack::Context.log_key(:application), nil)
25+
Code0::ZeroTrack::Context.with_context(
26+
**context,
27+
application: 'good_job',
28+
source_application: source_application,
29+
job_id: job.job_id,
30+
job_class: self.class.name
31+
) do
32+
block.call
33+
end
3134
end
3235
end
3336
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class UpdateRuntimesForProjectJob < ApplicationJob
4+
def perform(project_id)
5+
project = NamespaceProject.find_by(id: project_id)
6+
return if project.nil?
7+
8+
project.runtimes.each do |runtime|
9+
FlowHandler.update_runtime(runtime)
10+
end
11+
end
12+
end

app/services/namespaces/projects/flows/create_service.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def execute
5959
)
6060
end
6161

62+
UpdateRuntimesForProjectJob.perform_later(namespace_project.id)
63+
6264
AuditService.audit(
6365
:flow_created,
6466
author_id: current_authentication.user.id,

app/services/namespaces/projects/flows/delete_service.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def execute
2828
)
2929
end
3030

31+
UpdateRuntimesForProjectJob.perform_later(flow.project.id)
32+
3133
AuditService.audit(
3234
:flow_deleted,
3335
author_id: current_authentication.user.id,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe UpdateRuntimesForProjectJob do
6+
include ActiveJob::TestHelper
7+
8+
let(:flow) { create(:flow) }
9+
let(:runtimes) { create_list(:runtime, 2, namespace: flow.project.namespace) }
10+
let!(:other_runtime) { create(:runtime, namespace: flow.project.namespace) }
11+
12+
before do
13+
runtimes.each do |runtime|
14+
create(:namespace_project_runtime_assignment, namespace_project: flow.project, runtime: runtime)
15+
end
16+
end
17+
18+
it 'sends update to all relevant runtimes' do
19+
allow(FlowHandler).to receive(:update_runtime)
20+
21+
perform_enqueued_jobs do
22+
described_class.perform_later(flow.project.id)
23+
end
24+
25+
runtimes.each do |runtime|
26+
expect(FlowHandler).to have_received(:update_runtime).with(runtime)
27+
end
28+
29+
expect(FlowHandler).not_to have_received(:update_runtime).with(other_runtime)
30+
end
31+
end

spec/rails_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@
8888
config.include AuthenticationHelpers, type: :policy
8989
config.include AuthenticationHelpers, type: :service
9090

91+
# Load job helpers
92+
config.include JobHelpers, type: :job
93+
9194
config.before eager_load: true do
9295
Rails.application.eager_load!
9396
end

spec/services/namespaces/projects/flows/create_service_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,13 @@
6666
target_type: 'NamespaceProject'
6767
)
6868
end
69+
70+
it 'queues job to update runtimes' do
71+
allow(UpdateRuntimesForProjectJob).to receive(:perform_later)
72+
73+
service_response
74+
75+
expect(UpdateRuntimesForProjectJob).to have_received(:perform_later).with(namespace_project.id)
76+
end
6977
end
7078
end

spec/services/namespaces/projects/flows/delete_service_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,13 @@
5858
target_type: 'NamespaceProject'
5959
)
6060
end
61+
62+
it 'queues job to update runtimes' do
63+
allow(UpdateRuntimesForProjectJob).to receive(:perform_later)
64+
65+
service_response
66+
67+
expect(UpdateRuntimesForProjectJob).to have_received(:perform_later).with(namespace_project.id)
68+
end
6169
end
6270
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
module JobHelpers
4+
def queue_adapter_for_test
5+
GoodJob::Adapter.new(execution_mode: :inline)
6+
end
7+
end

0 commit comments

Comments
 (0)