-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP reproduce notifications job load
- Loading branch information
1 parent
51fc6f6
commit 9ed8781
Showing
9 changed files
with
189 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use tracing::instrument; | ||
|
||
use std::collections::HashMap; | ||
|
||
use crate::{ | ||
history::NotificationHistory, notification_event::NotificationEventPayload, | ||
primitives::GaloyUserId, | ||
}; | ||
use job_executor::JobResult; | ||
|
||
use super::error::JobError; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub(super) struct MultiUserEventDispatchData { | ||
user_ids: Vec<GaloyUserId>, | ||
payload: NotificationEventPayload, | ||
#[serde(flatten)] | ||
pub(super) tracing_data: HashMap<String, serde_json::Value>, | ||
} | ||
|
||
impl From<(Vec<GaloyUserId>, NotificationEventPayload)> for MultiUserEventDispatchData { | ||
fn from((user_ids, payload): (Vec<GaloyUserId>, NotificationEventPayload)) -> Self { | ||
Self { | ||
user_ids, | ||
payload, | ||
tracing_data: HashMap::default(), | ||
} | ||
} | ||
} | ||
|
||
#[instrument( | ||
name = "job.multi_user_event_dispatch", | ||
skip(history, pool), | ||
fields(first_id, last_id, ids_len, next_ids_len), | ||
err | ||
)] | ||
pub async fn execute( | ||
data: MultiUserEventDispatchData, | ||
history: NotificationHistory, | ||
pool: sqlx::PgPool, | ||
) -> Result<JobResult, JobError> { | ||
let batch_limit = 10; | ||
let (ids, next_user_ids) = data | ||
.user_ids | ||
.split_at(std::cmp::min(data.user_ids.len(), batch_limit)); | ||
let span = tracing::Span::current(); | ||
if ids.len() > 0 { | ||
span.record("first_id", &tracing::field::display(&ids[0])); | ||
span.record("last_id", &tracing::field::display(&ids[ids.len() - 1])); | ||
span.record("ids_len", &tracing::field::display(ids.len())); | ||
span.record( | ||
"next_ids_len", | ||
&tracing::field::display(next_user_ids.len()), | ||
); | ||
} | ||
println!( | ||
"multi_user_event_dispatch: {}, {}", | ||
ids[0], | ||
ids[ids.len() - 1] | ||
); | ||
let mut tx = pool.begin().await?; | ||
if !next_user_ids.is_empty() { | ||
let data = MultiUserEventDispatchData { | ||
user_ids: next_user_ids.to_vec(), | ||
payload: data.payload.clone(), | ||
tracing_data: HashMap::default(), | ||
}; | ||
super::spawn_multi_user_event_dispatch(&mut tx, data).await?; | ||
} | ||
|
||
let payload = data.payload.clone(); | ||
|
||
history.add_events(&mut tx, ids, payload.clone()).await?; | ||
|
||
for user_id in ids { | ||
if payload.should_send_email() { | ||
super::spawn_send_email_notification(&mut tx, (user_id.clone(), payload.clone())) | ||
.await?; | ||
} | ||
if payload.should_send_push() { | ||
super::spawn_send_push_notification(&mut tx, (user_id.clone(), payload.clone())) | ||
.await?; | ||
} | ||
} | ||
Ok::<_, JobError>(JobResult::CompleteWithTx(tx)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
app: | ||
push_executor: | ||
fcm: | ||
google_application_credentials_path: "dev/config/notifications/fake_service_account.json" | ||
subgraph_server: | ||
port: 1234 | ||
grpc_server: | ||
port: 2345 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
app: | ||
push_executor: | ||
fcm: | ||
google_application_credentials_path: "./config/notifications/fake_service_account.json" | ||
google_application_credentials_path: "dev/config/notifications/fake_service_account.json" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/bin/bash | ||
|
||
set -xe | ||
|
||
source "bats/helpers/_common.bash" | ||
source "bats/helpers/user.bash" | ||
source "bats/helpers/onchain.bash" | ||
|
||
NOTIFICATIONS_GRPC_ENDPOINT="localhost:6685" | ||
IMPORT_PATH="${REPO_ROOT}/core/notifications/proto" | ||
NOTIFICATIONS_PROTO_FILE="${REPO_ROOT}/core/notifications/proto/notifications.proto" | ||
|
||
# update_user_locale_method="services.notifications.v1.NotificationsService/UpdateUserLocale" | ||
|
||
# for i in $(seq 1 10000); do | ||
# request_data=$(jq -n --arg userId "$i" --arg locale "es" '{ | ||
# "userId": $userId, | ||
# "locale": $locale | ||
# }') | ||
# grpcurl_request $IMPORT_PATH $NOTIFICATIONS_PROTO_FILE $NOTIFICATIONS_GRPC_ENDPOINT "$update_user_locale_method" "$request_data" | ||
|
||
# done | ||
localized_content_en='{"title": "Hello", "body": "World"}' | ||
localized_content_es='{"title": "Hola", "body": "World"}' | ||
# Generate user_ids array from "1" to "10000" | ||
user_ids=($(seq -f "%04g" 1 8000)) | ||
|
||
# Convert user_ids array to a JSON array of strings | ||
user_ids=$(printf '%s\n' "${user_ids[@]}" | jq -R . | jq -s .) | ||
|
||
# Create the JSON request payload using jq | ||
request_data=$(jq -n \ | ||
--argjson user_ids "$user_ids" \ | ||
--argjson localized_content_en "$localized_content_en" \ | ||
--argjson localized_content_es "$localized_content_es" \ | ||
'{ | ||
"event": { | ||
"marketingNotificationTriggered": { | ||
"user_ids": $user_ids, | ||
"localized_push_content": { | ||
"en": $localized_content_en, | ||
"es": $localized_content_es | ||
} | ||
} | ||
} | ||
}') | ||
|
||
echo $request_data | jq | ||
# Specify the GRPC method to call | ||
handle_notification_event_method="services.notifications.v1.NotificationsService/HandleNotificationEvent" | ||
|
||
# Make the GRPC request using grpcurl | ||
grpcurl_request $IMPORT_PATH $NOTIFICATIONS_PROTO_FILE $NOTIFICATIONS_GRPC_ENDPOINT "$handle_notification_event_method" "$request_data" | ||
|