Skip to main content

iggy_cli/commands/binary_consumer_offsets/
set_consumer_offset.rs

1/* Licensed to the Apache Software Foundation (ASF) under one
2 * or more contributor license agreements.  See the NOTICE file
3 * distributed with this work for additional information
4 * regarding copyright ownership.  The ASF licenses this file
5 * to you under the Apache License, Version 2.0 (the
6 * "License"); you may not use this file except in compliance
7 * with the License.  You may obtain a copy of the License at
8 *
9 *   http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied.  See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18
19use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
20use anyhow::Context;
21use async_trait::async_trait;
22use iggy_common::Client;
23use iggy_common::Identifier;
24use iggy_common::{Consumer, ConsumerKind};
25use tracing::{Level, event};
26
27pub struct SetConsumerOffsetCmd {
28    consumer: Consumer,
29    stream_id: Identifier,
30    topic_id: Identifier,
31    partition_id: u32,
32    offset: u64,
33}
34
35impl SetConsumerOffsetCmd {
36    pub fn new(
37        consumer_id: Identifier,
38        stream_id: Identifier,
39        topic_id: Identifier,
40        partition_id: u32,
41        offset: u64,
42        kind: ConsumerKind,
43    ) -> Self {
44        Self {
45            consumer: Consumer {
46                kind,
47                id: consumer_id,
48            },
49            stream_id,
50            topic_id,
51            partition_id,
52            offset,
53        }
54    }
55}
56
57#[async_trait]
58impl CliCommand for SetConsumerOffsetCmd {
59    fn explain(&self) -> String {
60        format!(
61            "set consumer offset for consumer with ID: {} for stream with ID: {} and topic with ID: {} and partition with ID: {} to {}",
62            self.consumer.id, self.stream_id, self.topic_id, self.partition_id, self.offset,
63        )
64    }
65
66    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
67        client
68            .store_consumer_offset(&self.consumer, &self.stream_id, &self.topic_id, Some(self.partition_id), self.offset)
69            .await
70            .with_context(|| {
71                format!(
72                    "Problem setting consumer offset for consumer with ID: {} for stream with ID: {} and topic with ID: {} and partition with ID: {}",
73                    self.consumer.id, self.stream_id, self.topic_id, self.partition_id
74                )
75            })?;
76
77        event!(target: PRINT_TARGET, Level::INFO,
78            "Consumer offset for consumer with ID: {} for stream with ID: {} and topic with ID: {} and partition with ID: {} set to {}",
79            self.consumer.id,
80            self.stream_id,
81            self.topic_id,
82            self.partition_id,
83            self.offset,
84        );
85
86        Ok(())
87    }
88}