Skip to content

Commit a14718b

Browse files
committed
feat: correct flow interface implementation
1 parent c46ab53 commit a14718b

10 files changed

Lines changed: 141 additions & 9 deletions

File tree

app/grpc/flow_handler.rb

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

88
grpc_stream :update
99

10+
def self.update_flow(flow)
11+
response = Tucana::Sagittarius::FlowResponse.new(updated_flow: flow.to_grpc)
12+
send_to_project_runtimes(flow.project, response)
13+
end
14+
15+
def self.delete_flow(project, flow_id)
16+
response = Tucana::Sagittarius::FlowResponse.new(deleted_flow_id: flow_id)
17+
send_to_project_runtimes(project, response)
18+
end
19+
20+
def self.send_to_project_runtimes(project, response)
21+
project.runtime_assignments.compatible.find_each do |assignment|
22+
send_update(response, assignment.runtime_id)
23+
end
24+
end
25+
1026
def self.update_runtime(runtime)
1127
assignments = runtime.project_assignments.compatible.includes(
1228
:namespace_project,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
class DeleteFlowForProjectJob < ApplicationJob
4+
def perform(project_id, flow_id)
5+
project = NamespaceProject.find_by(id: project_id)
6+
return if project.nil?
7+
8+
FlowHandler.delete_flow(project, flow_id)
9+
end
10+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
class UpdateFlowForProjectJob < ApplicationJob
4+
def perform(flow_id)
5+
flow = Flow.find_by(id: flow_id)
6+
return unless flow&.validation_status_valid?
7+
8+
FlowHandler.update_flow(flow)
9+
end
10+
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def execute
2929
)
3030
end
3131

32-
UpdateRuntimesForProjectJob.perform_later(flow.project.id)
32+
DeleteFlowForProjectJob.perform_later(flow.project.id, flow.id)
3333

3434
AuditService.audit(
3535
:flow_deleted,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def execute
2727
validation_diagnostics: result.diagnostics
2828
)
2929

30-
UpdateRuntimesForProjectJob.perform_later(flow.project.id)
30+
UpdateFlowForProjectJob.perform_later(flow.id) if result.valid?
3131

3232
result
3333
end

spec/grpc/flow_handler_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,43 @@
5555
)
5656
end
5757
end
58+
59+
describe 'project runtime updates' do
60+
let(:flow) { create(:flow, validation_status: :valid) }
61+
let(:runtime) { create(:runtime, namespace: flow.project.namespace) }
62+
63+
before do
64+
create(
65+
:namespace_project_runtime_assignment,
66+
namespace_project: flow.project,
67+
runtime: runtime,
68+
compatible: true
69+
)
70+
allow(described_class).to receive(:send_update)
71+
end
72+
73+
describe '.update_flow' do
74+
it 'sends the updated_flow response to compatible project runtimes' do
75+
described_class.update_flow(flow)
76+
77+
expect(described_class).to have_received(:send_update) do |response, runtime_id|
78+
expect(runtime_id).to eq(runtime.id)
79+
expect(response.data).to eq(:updated_flow)
80+
expect(response.updated_flow).to eq(flow.to_grpc)
81+
end
82+
end
83+
end
84+
85+
describe '.delete_flow' do
86+
it 'sends the deleted_flow_id response to compatible project runtimes' do
87+
described_class.delete_flow(flow.project, flow.id)
88+
89+
expect(described_class).to have_received(:send_update) do |response, runtime_id|
90+
expect(runtime_id).to eq(runtime.id)
91+
expect(response.data).to eq(:deleted_flow_id)
92+
expect(response.deleted_flow_id).to eq(flow.id)
93+
end
94+
end
95+
end
96+
end
5897
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe DeleteFlowForProjectJob do
6+
include ActiveJob::TestHelper
7+
8+
it 'sends the deleted flow id' do
9+
project = create(:namespace_project)
10+
flow_id = 123
11+
allow(FlowHandler).to receive(:delete_flow)
12+
13+
perform_enqueued_jobs { described_class.perform_later(project.id, flow_id) }
14+
15+
expect(FlowHandler).to have_received(:delete_flow).with(project, flow_id)
16+
end
17+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe UpdateFlowForProjectJob do
6+
include ActiveJob::TestHelper
7+
8+
let(:flow) { create(:flow) }
9+
10+
before do
11+
allow(FlowHandler).to receive(:update_flow)
12+
allow(FlowHandler).to receive(:delete_flow)
13+
end
14+
15+
it 'sends a valid flow update' do
16+
flow.update!(validation_status: :valid)
17+
18+
perform_enqueued_jobs { described_class.perform_later(flow.id) }
19+
20+
expect(FlowHandler).to have_received(:update_flow).with(flow)
21+
expect(FlowHandler).not_to have_received(:delete_flow)
22+
end
23+
24+
it 'does not send a deletion for a newly created invalid flow' do
25+
flow.update!(validation_status: :invalid)
26+
27+
perform_enqueued_jobs { described_class.perform_later(flow.id) }
28+
29+
expect(FlowHandler).not_to have_received(:delete_flow)
30+
expect(FlowHandler).not_to have_received(:update_flow)
31+
end
32+
end

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@
6060
end
6161

6262
it 'queues job to update runtimes' do
63-
allow(UpdateRuntimesForProjectJob).to receive(:perform_later)
63+
allow(DeleteFlowForProjectJob).to receive(:perform_later)
6464

6565
service_response
6666

67-
expect(UpdateRuntimesForProjectJob).to have_received(:perform_later).with(namespace_project.id)
67+
expect(DeleteFlowForProjectJob).to have_received(:perform_later).with(namespace_project.id, flow.id)
6868
end
6969
end
7070
end

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
context 'with fixed validation results' do
4747
before do
4848
flow.update!(starting_node: node_function)
49-
allow(UpdateRuntimesForProjectJob).to receive(:perform_later)
49+
allow(UpdateFlowForProjectJob).to receive(:perform_later)
5050

5151
result = Triangulum::Validation::Result.new(valid?: valid, return_type: nil, diagnostics: diagnostics)
5252
allow(Triangulum::Validation).to receive(:new).and_return(
@@ -71,10 +71,10 @@
7171
expect(flow.reload.validation_diagnostics).to eq([])
7272
end
7373

74-
it 'enqueues UpdateRuntimesForProjectJob' do
74+
it 'enqueues UpdateFlowForProjectJob' do
7575
service.execute
7676

77-
expect(UpdateRuntimesForProjectJob).to have_received(:perform_later).with(namespace_project.id)
77+
expect(UpdateFlowForProjectJob).to have_received(:perform_later).with(flow.id)
7878
end
7979
end
8080

@@ -128,10 +128,18 @@
128128
)
129129
end
130130

131-
it 'enqueues UpdateRuntimesForProjectJob' do
131+
it 'does not enqueue a runtime update for a newly created invalid flow' do
132132
service.execute
133133

134-
expect(UpdateRuntimesForProjectJob).to have_received(:perform_later).with(namespace_project.id)
134+
expect(UpdateFlowForProjectJob).not_to have_received(:perform_later)
135+
end
136+
137+
it 'does not enqueue a deletion when a previously valid flow becomes invalid' do
138+
flow.update!(validation_status: :valid)
139+
140+
service.execute
141+
142+
expect(UpdateFlowForProjectJob).not_to have_received(:perform_later)
135143
end
136144
end
137145
end

0 commit comments

Comments
 (0)