@@ -5,62 +5,71 @@ class Channels::FetchPostsJob < ApplicationJob
55 retry_on StandardError , wait : 1 . minute , attempts : 3
66
77 def perform ( channel_id )
8- channel = Channel . find ( channel_id )
8+ with_error_context ( channel_id : channel_id , action : 'fetch_posts' ) do
9+ channel = Channel . find ( channel_id )
910
10- Rails . logger . info "Fetching posts for channel #{ channel . username } "
11+ Rails . logger . info "Fetching posts for channel #{ channel . username } "
1112
12- # Получаем Telegram ID канала
13- chat_id = channel . telegram_id . to_s
13+ # Получаем Telegram ID канала
14+ chat_id = channel . telegram_id . to_s
1415
15- # Инициализируем клиент
16- bot = Telegram . bots [ :default ]
17- fetcher = TelegramClient ::ChannelFetcher . new ( bot )
16+ # Инициализируем клиент
17+ bot = Telegram . bots [ :default ]
18+ fetcher = TelegramClient ::ChannelFetcher . new ( bot )
1819
19- # Проверяем доступность канала
20- unless fetcher . channel_available? ( channel . username )
21- Rails . logger . warn "Channel #{ channel . username } is not available, deactivating"
22- channel . deactivate!
23- return
24- end
20+ # Проверяем доступность канала
21+ unless fetcher . channel_available? ( channel . username )
22+ Rails . logger . warn "Channel #{ channel . username } is not available, deactivating"
23+ channel . deactivate!
24+ return
25+ end
2526
26- # Получаем последние посты
27- posts_data = fetcher . get_channel_posts ( channel . username , limit : 20 )
27+ # Получаем последние посты
28+ posts_data = fetcher . get_channel_posts ( channel . username , limit : 20 )
2829
29- Rails . logger . info "Fetched #{ posts_data . count } posts from channel #{ channel . username } "
30+ Rails . logger . info "Fetched #{ posts_data . count } posts from channel #{ channel . username } "
3031
31- # Обновляем время последнего поста если есть посты
32- if posts_data . any?
33- channel . update_last_post!
34- end
32+ # Обновляем время последнего поста если есть посты
33+ if posts_data . any?
34+ channel . update_last_post!
35+ end
3536
36- # Обрабатываем каждый пост
37- posts_data . each do |post_data |
38- begin
39- # Проверяем, нет ли уже такого поста в БД
40- existing_post = Post . find_by (
41- channel : channel ,
42- telegram_message_id : post_data [ :telegram_message_id ]
43- )
44-
45- if existing_post
46- Rails . logger . debug "Post #{ post_data [ :telegram_message_id ] } already exists, skipping"
47- next
48- end
49-
50- # Запускаем задачу для сохранения и обработки поста
51- Content ::ProcessPostJob . perform_later ( channel . id , post_data )
52-
53- Rails . logger . debug "Scheduled processing for post #{ post_data [ :telegram_message_id ] } "
54- rescue StandardError => e
55- Rails . logger . error "Error processing post #{ post_data [ :telegram_message_id ] } : #{ e . message } "
37+ # Обрабатываем каждый пост
38+ posts_data . each do |post_data |
39+ process_single_post ( channel , post_data )
5640 end
41+
42+ Rails . logger . info "Completed fetching posts for channel #{ channel . username } "
5743 end
44+ end
45+
46+ private
47+
48+ def process_single_post ( channel , post_data )
49+ # Проверяем, нет ли уже такого поста в БД
50+ existing_post = Post . find_by (
51+ channel : channel ,
52+ telegram_message_id : post_data [ :telegram_message_id ]
53+ )
54+
55+ if existing_post
56+ Rails . logger . debug "Post #{ post_data [ :telegram_message_id ] } already exists, skipping"
57+ return
58+ end
59+
60+ # Запускаем задачу для сохранения и обработки поста
61+ Content ::ProcessPostJob . perform_later ( channel . id , post_data )
5862
59- Rails . logger . info "Completed fetching posts for channel #{ channel . username } "
60- rescue ActiveRecord ::RecordNotFound => e
61- Rails . logger . error "Channel #{ channel_id } not found: #{ e . message } "
63+ Rails . logger . debug "Scheduled processing for post #{ post_data [ :telegram_message_id ] } "
6264 rescue StandardError => e
63- Rails . logger . error "Error fetching posts for channel #{ channel_id } : #{ e . message } "
64- raise
65+ # Handle individual post processing errors without failing the entire job
66+ handle_error ( e ,
67+ metadata : {
68+ channel_id : channel . id ,
69+ message_id : post_data [ :telegram_message_id ] ,
70+ action : 'process_single_post'
71+ } ,
72+ severity : :warn ,
73+ reraise : false )
6574 end
6675end
0 commit comments