Skip to content

Commit

Permalink
Bug 1675114 - Add Dispatcher and IPC support to FOG Pings r=janerik
Browse files Browse the repository at this point in the history
  • Loading branch information
chutten committed Nov 9, 2020
1 parent 2a17510 commit ff77c97
Showing 1 changed file with 62 additions and 24 deletions.
86 changes: 62 additions & 24 deletions toolkit/components/glean/api/src/private/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::sync::Arc;

use crate::{dispatcher, ipc::need_ipc};

/// A Glean ping.
///
/// See [Glean Pings](https://mozilla.github.io/glean/book/user/pings/index.html).
#[derive(Clone, Debug)]
pub struct Ping(glean_core::metrics::PingType);
pub enum Ping {
Parent(Arc<PingImpl>),
Child,
}
#[derive(Debug)]
pub struct PingImpl(glean_core::metrics::PingType);

impl Ping {
/// Create a new ping type for the given name, whether to include the client ID and whether to
Expand All @@ -24,18 +33,16 @@ impl Ping {
send_if_empty: bool,
reason_codes: Vec<String>,
) -> Self {
let ping = glean_core::metrics::PingType::new(
name,
include_client_id,
send_if_empty,
reason_codes,
);

crate::with_glean(|glean| {
glean.register_ping_type(&ping);
});

Self(ping)
if need_ipc() {
Ping::Child
} else {
Ping::Parent(Arc::new(PingImpl::new(
name,
include_client_id,
send_if_empty,
reason_codes,
)))
}
}

/// Collect and submit the ping for eventual upload.
Expand All @@ -56,16 +63,50 @@ impl Ping {
/// ## Parameters
/// * `reason` - The reason the ping is being submitted.
/// Must be one of the configured `reason_codes`.
///
/// ## Return value
///
/// Returns true if a ping was assembled and queued, false otherwise.
pub fn submit(&self, reason: Option<&str>) -> bool {
pub fn submit(&self, reason: Option<&str>) {
match self {
Ping::Parent(p) => {
let ping = Arc::clone(&p);
let reason = reason.map(|x| x.to_owned());
dispatcher::launch(move || ping.submit(reason.as_deref()));
}
Ping::Child => {
log::error!(
"Unable to submit ping {:?} in non-main process. Ignoring.",
self
);
// TODO: Record an error.
}
};
}
}

impl PingImpl {
pub fn new<S: Into<String>>(
name: S,
include_client_id: bool,
send_if_empty: bool,
reason_codes: Vec<String>,
) -> Self {
let ping = glean_core::metrics::PingType::new(
name,
include_client_id,
send_if_empty,
reason_codes,
);

crate::with_glean(|glean| {
glean.register_ping_type(&ping);
});

Self(ping)
}

pub fn submit(&self, reason: Option<&str>) {
let res = crate::with_glean(|glean| self.0.submit(glean, reason).unwrap_or(false));
if res {
crate::ping_upload::check_for_uploads();
}
res
}
}

Expand All @@ -82,12 +123,9 @@ mod test {
#[test]
fn smoke_test_custom_ping() {
let _lock = lock_test();
// TODO: Submit through the dispatcher.
// For now we wait for glean to initialize to not crash.
crate::dispatcher::block_on_queue();

// We can only check that nothing explodes.
// `true` signals that the ping has been succesfully submitted (and written to disk).
assert_eq!(true, PROTOTYPE_PING.submit(None));
// More comprehensive tests are blocked on bug 1673660.
PROTOTYPE_PING.submit(None);
}
}

0 comments on commit ff77c97

Please sign in to comment.