-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathprocess_create.rb
More file actions
52 lines (43 loc) · 1.77 KB
/
process_create.rb
File metadata and controls
52 lines (43 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'repositories/process_event_repository'
require 'models/helpers/process_types'
require 'models/helpers/health_check_types'
module VCAP::CloudController
class ProcessCreate
class InvalidProcess < StandardError; end
class SidecarMemoryLessThanProcessMemory < StandardError; end
def initialize(user_audit_info, manifest_triggered: false)
@user_audit_info = user_audit_info
@manifest_triggered = manifest_triggered
end
def create(app, args)
type = args[:type]
attrs = args.merge({
diego: true,
instances: default_instance_count(type),
health_check_type: default_health_check_type(type),
metadata: {}
})
attrs[:guid] = app.guid if type == ProcessTypes::WEB
process = nil
app.class.db.transaction do
process = app.add_process(attrs)
app_ports = process.route_mappings_dataset.select_map(:app_port)
process.update(ports: app_ports) unless app_ports.empty?
Repositories::ProcessEventRepository.record_create(process, @user_audit_info, manifest_triggered: @manifest_triggered)
end
process
rescue Sequel::ValidationFailed => e
if e.errors.on(:memory)&.include?(:process_memory_insufficient_for_sidecars)
raise SidecarMemoryLessThanProcessMemory.new("The sidecar memory allocation defined is too large to run with the dependent \"#{type}\" process")
end
raise InvalidProcess.new(e.message)
end
private
def default_health_check_type(type)
type == ProcessTypes::WEB ? HealthCheckTypes::PORT : HealthCheckTypes::PROCESS
end
def default_instance_count(type)
type == ProcessTypes::WEB ? 1 : 0
end
end
end