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
99
100
101
102
103
104
105
|
# File 'app/jobs/export_json_job.rb', line 10
def perform(request, current_user, query_params, export_service)
logger.debug("\n\n Background Job: ♞")
logger.debug("Request: #{request.inspect}")
logger.debug("User: #{current_user.inspect}")
logger.debug("Query: #{query_params.inspect}")
logger.debug("Export Service: #{export_service.inspect}")
logger.debug("\n\n")
ActionCable.server.broadcast("export_channel", {data: "Hello from Export JSON Job!"})
document_ids = query_params[:ids] || crawl_query(request, query_params)
documents = export_service.call(document_ids)
FileUtils.mkdir_p("ogm")
Dir.mktmpdir do |dir|
documents.each do |doc|
Rails.logger.debug { "==== Query Format #{query_params[:format]} ====" }
Rails.logger.debug { "==== Writing - #{doc.friendlier_id} - #{query_params[:format]} ====" }
begin
code = doc.b1g_code_s.presence || "_b1g_code_s_missing"
resource_class = doc.gbl_resourceClass_sm.present? ? doc.gbl_resourceClass_sm.first : "_gbl_resourceClass_sm_missing"
tree = Pathname("#{dir}/#{resource_class}/#{code}/#{doc.friendlier_id}.json")
tree.dirname.mkpath
Rails.logger.debug tree.inspect
json_output = Admin::DocumentsController.render("_#{query_params[:format]}",
locals: {document: doc})
json_obj = JSON.parse(json_output)
Rails.logger.debug json_obj
json_obj.compact!
tree.write(JSON.pretty_generate(json_obj))
rescue NoMethodError => e
Rails.logger.debug { "==== Error! - #{doc.friendlier_id} ====" }
Rails.logger.debug e.inspect
next
end
end
@zip_file = Rails.root.join("tmp", "export-json-#{Time.zone.today}-#{SecureRandom.hex(8)}.zip")
zf = ZipFileGenerator.new(dir, @zip_file)
zf.write
Rails.logger.debug { "File size: #{@zip_file.size}" }
end
notification = ExportNotification.with(message: "#{query_params[:format].humanize.upcase}|#{ActionController::Base.helpers.number_with_delimiter(documents.size)} rows|ZIP")
notification.deliver(current_user)
notification.record.file.attach(io: File.open(@zip_file), filename: "geomg-export-#{Time.zone.today}.zip",
content_type: "application/zip")
Rails.logger.debug "Notification File attached!"
ActionCable.server.broadcast("export_channel", {
data: "Notification ready!",
actions: [
{
method: "RefreshNotifications",
payload: current_user.notifications.unread.count
}
]
})
end
|