Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 1 | // Copyright 2018 Open Networking Foundation |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 18 | "os" |
| 19 | "os/signal" |
| 20 | "sync" |
| 21 | |
| 22 | "github.com/Shopify/sarama" |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 23 | "gerrit.opencord.org/kafka-topic-exporter/common/logger" |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 26 | func topicListener(topic *string, master sarama.Consumer, wg sync.WaitGroup) { |
| 27 | logger.Info("Starting topicListener for [%s]", *topic) |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 28 | defer wg.Done() |
| 29 | consumer, err := master.ConsumePartition(*topic, 0, sarama.OffsetOldest) |
| 30 | if err != nil { |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 31 | logger.Error("topicListener panic") |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 32 | panic(err) |
| 33 | } |
| 34 | signals := make(chan os.Signal, 1) |
| 35 | signal.Notify(signals, os.Interrupt) |
| 36 | doneCh := make(chan struct{}) |
| 37 | go func() { |
| 38 | for { |
| 39 | select { |
| 40 | case err := <-consumer.Errors(): |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 41 | logger.Error("%s", err) |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 42 | case msg := <-consumer.Messages(): |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 43 | logger.Debug("Message on %s: %s", *topic, string(msg.Value)) |
| 44 | export(topic, msg.Value) |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 45 | case <-signals: |
Ganesh Bhure | 8d0c994 | 2019-05-24 11:42:09 +0530 | [diff] [blame] | 46 | logger.Warn("Interrupt is detected") |
Matteo Scandolo | aab36db | 2018-10-09 19:54:11 -0700 | [diff] [blame] | 47 | doneCh <- struct{}{} |
| 48 | } |
| 49 | } |
| 50 | }() |
| 51 | <-doneCh |
| 52 | } |