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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'app/jobs/ragdoll/process_file_job.rb', line 7
def perform(file_id, session_id, filename, temp_path)
::Rails.logger.info "๐ Ragdoll::ProcessFileJob starting: file_id=#{file_id}, session_id=#{session_id}, filename=#{filename}"
::Rails.logger.info "๐ Temp file path: #{temp_path}"
::Rails.logger.info "๐ Temp file exists: #{File.exist?(temp_path)}"
::Rails.logger.info "๐ Temp file size: #{File.exist?(temp_path) ? File.size(temp_path) : 'N/A'} bytes"
begin
unless File.exist?(temp_path)
raise "Temporary file not found: #{temp_path}"
end
broadcast_data = {
file_id: file_id,
filename: filename,
status: 'started',
progress: 0,
message: 'Starting file processing...'
}
::Rails.logger.info "๐ก Broadcasting start: #{broadcast_data}"
begin
ActionCable.server.broadcast("ragdoll_file_processing_#{session_id}", broadcast_data)
::Rails.logger.info "โ
ActionCable broadcast sent successfully"
track_job_progress(session_id, file_id, filename, 0, 'started')
rescue => e
::Rails.logger.error "โ ActionCable broadcast failed: #{e.message}"
::Rails.logger.error e.backtrace.first(3)
end
broadcast_progress(session_id, file_id, filename, 25, 'Reading file...')
track_job_progress(session_id, file_id, filename, 25, 'processing')
result = ::Ragdoll.add_document(path: temp_path)
broadcast_progress(session_id, file_id, filename, 75, 'Generating embeddings...')
track_job_progress(session_id, file_id, filename, 75, 'processing')
if result[:success] && result[:document_id]
document = ::Ragdoll::Document.find(result[:document_id])
completion_data = {
file_id: file_id,
filename: filename,
status: 'completed',
progress: 100,
message: 'Processing completed successfully',
document_id: document.id
}
::Rails.logger.info "๐ Broadcasting completion: #{completion_data}"
begin
ActionCable.server.broadcast("ragdoll_file_processing_#{session_id}", completion_data)
::Rails.logger.info "โ
Completion broadcast sent successfully"
mark_job_completed(session_id, file_id)
rescue => e
::Rails.logger.error "โ Completion broadcast failed: #{e.message}"
end
else
raise "Processing failed: #{result[:error] || 'Unknown error'}"
end
rescue => e
::Rails.logger.error "๐ฅ Ragdoll::ProcessFileJob error: #{e.message}"
::Rails.logger.error e.backtrace.first(5)
error_data = {
file_id: file_id,
filename: filename,
status: 'error',
progress: 0,
message: "Error: #{e.message}"
}
::Rails.logger.info "๐ก Broadcasting error: #{error_data}"
begin
ActionCable.server.broadcast("ragdoll_file_processing_#{session_id}", error_data)
::Rails.logger.info "โ
Error broadcast sent successfully"
mark_job_failed(session_id, file_id)
rescue => e
::Rails.logger.error "โ Error broadcast failed: #{e.message}"
end
raise e
ensure
if temp_path && File.exist?(temp_path)
::Rails.logger.info "๐งน Cleaning up temp file: #{temp_path}"
begin
File.delete(temp_path)
::Rails.logger.info "โ
Temp file deleted successfully"
rescue => e
::Rails.logger.error "โ Failed to delete temp file: #{e.message}"
end
else
::Rails.logger.info "๐ Temp file already cleaned up or doesn't exist: #{temp_path}"
end
end
end
|