@@ -2,53 +2,93 @@ class SyncQuipDocJob < ApplicationJob
22 queue_as :default
33
44 def perform ( _doc_id )
5- begin
6- document = Document . find ( _doc_id )
7- rescue ActiveRecord ::RecordNotFound => e
8- # Handle the case where the document is not found
9- Rails . logger . error ( "Document with id #{ _doc_id } not found: #{ e . message } " )
10- return # Exit early since there's nothing to sync
11- end
5+ document = fetch_document ( _doc_id )
6+ return unless document
127
13- # Log or handle cases where the document has no quip_url
148 if document . source_url . blank?
15- Rails . logger . warn ( "Document with id #{ _doc_id } has no source URL." )
9+ log_warning ( "Document with id #{ _doc_id } has no source URL." )
1610 return
1711 end
1812
19- begin
20- # Initialize the Quip client
21- quip_client = Quip ::Client . new ( access_token : ENV . fetch ( 'QUIP_TOKEN' ) )
22- uri = URI . parse ( document . source_url )
23- path = uri . path . sub ( %r{^/} , '' ) # Removes the leading /
24- quip_thread = quip_client . get_thread ( path )
25-
26- # Convert Quip HTML content to Markdown
27- markdown_quip = ReverseMarkdown . convert ( quip_thread [ 'html' ] )
28-
29- document . document = markdown_quip
30- document . synced_at = DateTime . current
31- document . last_sync_result = 'SUCCESS'
32- document . save!
33- # Reschedule the job to run again in 24 hours
34- SyncQuipDocJob . set ( wait : 24 . hours , priority : 10 ) . perform_later ( _doc_id )
35- return
36- rescue ActiveRecord ::RecordInvalid => e
37- # Handle save! failures (validation errors)
38- Rails . logger . error ( "Failed to save document with id #{ _doc_id } : #{ e . record . errors . full_messages . join ( ', ' ) } " )
39- document . document = "Document save failed: #{ e . record . errors . full_messages . join ( ', ' ) } "
40- rescue Quip ::Error => e
41- # Handle Quip-specific errors
42- Rails . logger . error ( "Quip API error while fetching document from #{ document . source_url } : #{ e . message } " )
43- document . document = "Error from Quip: #{ e . message } "
44- rescue StandardError => e
45- # Handle any other unforeseen errors
46- Rails . logger . error ( "Unexpected error during sync for document id #{ _doc_id } : #{ e . message } " )
47- document . document = "#{ e . message } "
48- end
13+ success = sync_document_with_quip ( document )
14+ update_sync_status ( document , success :)
15+ schedule_next_sync ( document . id , success :)
16+ end
17+
18+ private
19+
20+ def fetch_document ( doc_id )
21+ Document . find ( doc_id )
22+ rescue ActiveRecord ::RecordNotFound => e
23+ log_error ( "Document with id #{ doc_id } not found: #{ e . message } " )
24+ nil
25+ end
26+
27+ def sync_document_with_quip ( document )
28+ quip_client = initialize_quip_client
29+ quip_thread = fetch_quip_thread ( document . source_url , quip_client )
30+ return false unless quip_thread
31+
32+ update_document_from_quip ( document , quip_thread )
33+ true
34+ rescue ActiveRecord ::RecordInvalid => e
35+ handle_save_error ( document , e )
36+ false
37+ rescue Quip ::Error => e
38+ handle_quip_error ( document , e )
39+ false
40+ rescue StandardError => e
41+ handle_unexpected_error ( document , e )
42+ false
43+ end
44+
45+ def initialize_quip_client
46+ Quip ::Client . new ( access_token : ENV . fetch ( 'QUIP_TOKEN' ) )
47+ end
48+
49+ def fetch_quip_thread ( source_url , quip_client )
50+ uri = URI . parse ( source_url )
51+ path = uri . path . sub ( %r{^/} , '' )
52+ quip_client . get_thread ( path )
53+ rescue Quip ::Error => e
54+ log_error ( "Quip API error while fetching document from #{ source_url } : #{ e . message } " )
55+ nil
56+ end
57+
58+ def update_document_from_quip ( document , quip_thread )
59+ markdown_quip = ReverseMarkdown . convert ( quip_thread [ 'html' ] )
60+ document . update! ( document : markdown_quip , synced_at : DateTime . current , last_sync_result : 'SUCCESS' )
61+ end
62+
63+ def handle_save_error ( document , exception )
64+ log_error ( "Failed to save document with id #{ document . id } : #{ exception . record . errors . full_messages . join ( ', ' ) } " )
65+ document . update ( document : "Document save failed: #{ exception . record . errors . full_messages . join ( ', ' ) } " )
66+ end
67+
68+ def handle_quip_error ( document , exception )
69+ log_error ( "Quip API error for document id #{ document . id } : #{ exception . message } " )
70+ document . update ( document : "Error from Quip: #{ exception . message } " )
71+ end
72+
73+ def handle_unexpected_error ( document , exception )
74+ log_error ( "Unexpected error during sync for document id #{ document . id } : #{ exception . message } " )
75+ document . update ( document : exception . message )
76+ end
77+
78+ def update_sync_status ( document , success :)
79+ document . update ( synced_at : DateTime . current , last_sync_result : success ? 'SUCCESS' : 'FAILED' )
80+ end
81+
82+ def schedule_next_sync ( doc_id , success :)
83+ delay = success ? 24 . hours : 3 . hours
84+ SyncQuipDocJob . set ( wait : delay , priority : 10 ) . perform_later ( doc_id )
85+ end
86+
87+ def log_error ( message )
88+ Rails . logger . error ( message )
89+ end
4990
50- document . last_sync_result = 'FAILED'
51- document . save
52- SyncQuipDocJob . set ( wait : 30 . minutes , priority : 10 ) . perform_later ( _doc_id )
91+ def log_warning ( message )
92+ Rails . logger . warn ( message )
5393 end
5494end
0 commit comments