|
| 1 | +use cloud_pubsub::error; |
| 2 | +use cloud_pubsub::{Client, EncodedMessage, FromPubSubMessage, Subscription}; |
| 3 | +use ctrlc; |
| 4 | +use futures::future::lazy; |
| 5 | +use serde_derive::Deserialize; |
| 6 | +use std::sync::Arc; |
| 7 | +use std::time::Duration; |
| 8 | +use tokio::prelude::*; |
| 9 | + |
| 10 | +#[derive(Deserialize)] |
| 11 | +struct Config { |
| 12 | + topic: String, |
| 13 | + google_application_credentials: String, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug)] |
| 17 | +struct UpdatePacket(String); |
| 18 | + |
| 19 | +impl FromPubSubMessage for UpdatePacket { |
| 20 | + fn from(message: EncodedMessage) -> Result<Self, error::Error> { |
| 21 | + match message.decode() { |
| 22 | + Ok(bytes) => Ok(UpdatePacket(String::from_utf8_lossy(&bytes).into_owned())), |
| 23 | + Err(e) => Err(error::Error::from(e)), |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +fn schedule_pubsub_pull(subscription: Arc<Subscription>) { |
| 29 | + let sub = Arc::clone(&subscription); |
| 30 | + let get = subscription |
| 31 | + .clone() |
| 32 | + .get_messages::<UpdatePacket>() |
| 33 | + .map(move |(packets, acks)| { |
| 34 | + for packet in packets { |
| 35 | + println!("Received: {:?}", packet); |
| 36 | + } |
| 37 | + |
| 38 | + if !acks.is_empty() { |
| 39 | + tokio::spawn(subscription.acknowledge_messages(acks)); |
| 40 | + } |
| 41 | + }) |
| 42 | + .map_err(|e| println!("Error Checking PubSub: {}", e)) |
| 43 | + .and_then(|_| { |
| 44 | + if sub.client().is_running() { |
| 45 | + schedule_pubsub_pull(sub); |
| 46 | + } else { |
| 47 | + println!("No longer pulling"); |
| 48 | + } |
| 49 | + |
| 50 | + Ok(()) |
| 51 | + }); |
| 52 | + tokio::spawn(get); |
| 53 | +} |
| 54 | + |
| 55 | +fn main() { |
| 56 | + let parsed_env = envy::from_env::<Config>(); |
| 57 | + if let Err(e) = parsed_env { |
| 58 | + eprintln!("ENV is not valid: {}", e); |
| 59 | + std::process::exit(1); |
| 60 | + } |
| 61 | + let config = parsed_env.unwrap(); |
| 62 | + |
| 63 | + let pubsub = match Client::new(config.google_application_credentials) { |
| 64 | + Err(e) => panic!("Failed to initialize pubsub: {}", e), |
| 65 | + Ok(p) => Arc::new(p), |
| 66 | + }; |
| 67 | + |
| 68 | + let topic = Arc::new(pubsub.topic(config.topic)); |
| 69 | + |
| 70 | + tokio::run(lazy(move || { |
| 71 | + pubsub.spawn_token_renew(Duration::from_secs(2)); |
| 72 | + |
| 73 | + topic |
| 74 | + .subscribe() |
| 75 | + .map(move |subscription| { |
| 76 | + println!("Subscribed to topic with: {}", subscription.name); |
| 77 | + let sub = Arc::new(subscription); |
| 78 | + |
| 79 | + schedule_pubsub_pull(Arc::clone(&sub)); |
| 80 | + |
| 81 | + ctrlc::set_once_handler(move || { |
| 82 | + println!("Cleaning up"); |
| 83 | + pubsub.stop(); |
| 84 | + let client = Arc::clone(&pubsub); |
| 85 | + println!("Waiting for current Pull to finish...."); |
| 86 | + while Arc::strong_count(&sub) > 1 {} |
| 87 | + println!("Deleting subscription"); |
| 88 | + if let Ok(s) = Arc::try_unwrap(sub) { |
| 89 | + let destroy = s |
| 90 | + .destroy() |
| 91 | + .map(|_r| println!("Successfully deleted subscription")) |
| 92 | + .map_err(|e| eprintln!("Failed deleting subscription: {}", e)) |
| 93 | + .then(move |_| { |
| 94 | + if let Ok(c) = Arc::try_unwrap(client) { |
| 95 | + drop(c); |
| 96 | + } |
| 97 | + Ok(()) |
| 98 | + }); |
| 99 | + tokio::run(destroy); |
| 100 | + } else { |
| 101 | + eprintln!("Subscription was still ownded"); |
| 102 | + } |
| 103 | + }) |
| 104 | + .expect("Error setting Ctrl-C handler"); |
| 105 | + }) |
| 106 | + .map_err(|e| eprintln!("Failed to subscribe: {}", e)) |
| 107 | + })); |
| 108 | +} |
0 commit comments