-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rb
More file actions
98 lines (75 loc) · 2.67 KB
/
main.rb
File metadata and controls
98 lines (75 loc) · 2.67 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require 'dotenv'
Dotenv.load
require_relative "lib/sentry_init"
begin
require "active_support/all"
require 'optparse'
require_relative "lib/etl"
require_relative "lib/utils"
include Utils
config_url = {
"rdvi" => "https://raw.githubusercontent.com/betagouv/rdv-insertion/main/config/anonymizer.yml",
"rdvs" => "https://raw.githubusercontent.com/betagouv/rdv-service-public/production/config/anonymizer.yml",
"rdvsp" => "https://raw.githubusercontent.com/betagouv/rdv-service-public/production/config/anonymizer.yml"
}
rdv_db_url_list = {
"rdvi" => "RDV_INSERTION_DB_URL",
"rdvs" => "RDV_SOLIDARITES_DB_URL",
"rdvsp" => "RDV_SERVICE_PUBLIC_DB_URL"
}
app = ENV["APP"]
from_cron = false
dry_run = false
OptionParser.new do |opts|
opts.on('-a', '--app APP', config_url.keys) { app = _1 }
opts.on('-c', '--from-cron', "Monitor run if it comes from a CRON schedule") { from_cron = true }
opts.on('-d', '--dry-run', "Skip actually running the ETL") { dry_run = true }
end.parse!
if app.nil?
raise "Définissez une variable d'environnement APP ou passez un argument --app"
end
sentry_monitor_helper = get_sentry_monitor_helpers[app] if from_cron
sentry_monitor_helper.capture_start if from_cron
if config_url.key?(app)
config_path = config_url[app]
origin_db_url_env_var = rdv_db_url_list[app]
else
unless ENV["CONFIG_PATH"]
raise "La variable d'environnement CONFIG_PATH n'est pas définie"
end
config_path = ENV["CONFIG_PATH"]
origin_db_url_env_var = "ORIGIN_DB_URL"
end
# Si le nom du fichier commence par https://, alors il s'agit d'une URL
if config_path.starts_with?("https://")
# Télécharger le fichier
run_command "curl -o config.yml \"#{config_path}\""
config_path = "config.yml"
end
unless File.exist?(config_path)
raise "La variable d'environnement CONFIG_PATH pointe vers un fichier inexistant"
end
etl_db_url_env_var = "ETL_DB_URL"
metabase_username_env_var = "METABASE_USERNAME"
[
origin_db_url_env_var,
etl_db_url_env_var,
metabase_username_env_var
].each do |env_var|
raise "Missing environment variable #{env_var}" if ENV[env_var].blank?
end
origin_db_url = ENV[origin_db_url_env_var]
etl_db_url = ENV[etl_db_url_env_var]
metabase_username = ENV[metabase_username_env_var]
if dry_run
puts "pretend working…"
sleep 2
else
Etl.new(app:, etl_db_url:, origin_db_url:, config_path:, metabase_username:).run
end
sentry_monitor_helper.capture_end_successful if from_cron
rescue Exception => e
sentry_monitor_helper.capture_error if from_cron && sentry_monitor_helper
Sentry.capture_exception(e)
raise e
end