lib/wrapbox/log_fetcher/awslogs.rb in wrapbox-0.9.0 vs lib/wrapbox/log_fetcher/awslogs.rb in wrapbox-0.10.0

- old
+ new

@@ -25,10 +25,18 @@ @displayed_event_ids = {} end def run(task:) @loop_thread = Thread.start do + # It smees that task.contaienrs is empty + # if capacity_provider_strategy is specified and there are no remaining capacity + while task.containers.empty? + Wrapbox.logger.warn("The task has no containers, so fetch it again") + sleep 10 + task = ecs_client.describe_tasks(cluster: task.cluster_arn, tasks: [task.task_arn]).tasks.first + end + main_loop(task) end end def stop @@ -43,33 +51,37 @@ end filter_log_opts = { log_group_name: @log_group, log_stream_names: log_stream_names, filter_pattern: @filter_pattern, - interleaved: true, }.compact @max_timestamp = ((Time.now.to_f - 120) * 1000).round until @stop do filter_log_opts[:start_time] = @max_timestamp + 1 - resp = client.filter_log_events(filter_log_opts) rescue nil - resp&.each do |r| - r.events.each do |ev| - next if @displayed_event_ids.member?(ev.event_id) - display_message(ev) - @displayed_event_ids[ev.event_id] = ev.timestamp - @max_timestamp = ev.timestamp if @max_timestamp < ev.timestamp + begin + client.filter_log_events(filter_log_opts).each do |r| + r.events.each do |ev| + next if @displayed_event_ids.member?(ev.event_id) + display_message(ev) + @displayed_event_ids[ev.event_id] = ev.timestamp + @max_timestamp = ev.timestamp if @max_timestamp < ev.timestamp + end end - end - Thread.start do + @displayed_event_ids.each do |event_id, ts| if ts < (Time.now.to_f - 600) * 1000 @displayed_event_ids.delete(event_id) end end - end.tap do - sleep @delay - end.join + rescue Aws::CloudWatchLogs::Errors::ResourceNotFoundException + # Ignore the error because it is an error like "The specified log stream does not exist.", + # which occurs when the log stream hasn't been created yet, that is, the task hasn't started yet. + rescue Aws::CloudWatchLogs::Errors::ThrottlingException + Wrapbox.logger.warn("Failed to fetch logs due to Aws::CloudWatchLogs::Errors::ThrottlingException") + end + + sleep @delay end end COLOR_ESCAPE_SEQUENCES = [33, 31, 32, 34, 35, 36] def display_message(ev, output: $stdout)