askak
askak

Reputation: 21

How to stop minio-cpp client notification-receiving server? (stop minio::s3::Client::ListenBucketNotification)

I run MinIO C++ client's server to receive notification from MinIO server.

The following code comes from here

#include <miniocpp/client.h>

int main() {
  // Create S3 base URL.
  minio::s3::BaseUrl base_url("<MY_MINIO_SERVER>");

  // Create credential provider.
  minio::creds::StaticProvider provider(
      "<MY_ACCESS-KEY", "<MY_SECRET_KEY>");

  // Create S3 client.
  minio::s3::Client client(base_url, &provider);

  // Create listen bucket notification arguments.
  minio::s3::ListenBucketNotificationArgs args;
  args.bucket = "my-bucket";
  args.func = [](std::list<minio::s3::NotificationRecord> records) -> bool {
    for (auto& record : records) {
      std::cout << "Received: Event: " << record.event_name << ", "
                << "Bucket: " << record.s3.bucket.name << ", "
                << "Object: " << record.s3.object.key << std::endl;
    }
    return true;
  };

  // Call listen bucket notification.
  minio::s3::ListenBucketNotificationResponse resp =
      client.ListenBucketNotification(args);

  // Handle response.
  if (!resp) {
    std::cout << "unable to do listen bucket notification; "
              << resp.Error().String() << std::endl;
  }

  return 0;
}

I can't figure out how to stop the client from receiving more notification. I want to be able to create a client in a thread and keep getting notifications for as long as I want. that's why I need to be able to stop it from getting more notifications.

Upvotes: 2

Views: 57

Answers (1)

thealch3mist
thealch3mist

Reputation: 21

The ListenBucketNotification method sets up a listener that continuously waits for notifications from your MinIO server. The connection is active as long as your Client is running so if you exit the thread the connection will drop. If you want keep on listening until you receive a trigger or for any amount of time, then you can use an atomic flag that you can set to true for listening and false for pausing or stopping the thread

#include <iostream>
#include <thread>
#include <atomic>
#include <miniocpp/client.h>
#include <chrono>


// YOu create an atomic flag which will signal when you want to stop sending notifications to S3
std::atomic<bool> stop_flag(false);

void listenForNotifications() {
  // Create S3 base URL.
  minio::s3::BaseUrl base_url("<MY_MINIO_SERVER>");

  // Create credential provider.
  minio::creds::StaticProvider provider(
      "<MY_ACCESS-KEY", "<MY_SECRET_KEY>");

  // Create S3 client.
  minio::s3::Client client(base_url, &provider);

  // Create listen bucket notification arguments.
  minio::s3::ListenBucketNotificationArgs args;
  args.bucket = "my-bucket";
  args.func = [](std::list<minio::s3::NotificationRecord> records) -> bool {
    //This will stop sending notifiations to the server when the flag is set to false
    if(!stop_flag.load()){
      return false;
    }
    for (auto& record : records) {
      std::cout << "Received: Event: " << record.event_name << ", "
                << "Bucket: " << record.s3.bucket.name << ", "
                << "Object: " << record.s3.object.key << std::endl;
    }
    return true;
  };

  // Call listen bucket notification.
  minio::s3::ListenBucketNotificationResponse resp =
      client.ListenBucketNotification(args);

  // Handle response.
  if (!resp) {
    std::cout << "unable to do listen bucket notification; "
              << resp.Error().String() << std::endl;
  }
}

int main() {
  
  std::thread listener_thread(listenForNotifications);

  // You can leave your listener running for some time or you can toogle the flag when you want to stop
  std::this_thread::sleep_for(std::chrono::seconds(10));
  // [something else happened]
  stop_flag.store(true);

  listener_thread.join(); // waits for the thread to finish before it exits the program

  return 0;
}

P.S. I haven't tested this code so it might need some tweaking but I think it gives you an rough idea of how to implement this

Upvotes: 1

Related Questions