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
|
# File 'app/jobs/export_job.rb', line 9
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 Job!"})
document_ids = query_params[:ids] || crawl_query(request, query_params)
logger.debug("Document Ids: #{document_ids}")
file_content = export_service.call(document_ids)
@tempfile = Tempfile.new(["export-#{Time.zone.today}", ".csv"]).tap do |file|
CSV.open(file, "wb") do |csv|
file_content.each do |row|
csv << row
end
end
end
notification = ExportNotification.with(message: "CSV (#{export_service.short_name})|#{ActionController::Base.helpers.number_with_delimiter(file_content.size - 1)} rows|CSV")
notification.deliver(current_user)
notification.record.file.attach(io: @tempfile, filename: "geomg-export-#{Time.zone.today}.csv",
content_type: "text/csv")
ActionCable.server.broadcast("export_channel", {
data: "Notification ready!",
actions: [
{
method: "RefreshNotifications",
payload: current_user.notifications.unread.count
}
]
})
end
|