Skip to content

Commit

Permalink
refactor: add secrets file to gitignore, remove deps on external serv…
Browse files Browse the repository at this point in the history
…ice in local
  • Loading branch information
tushar-saxena-yral committed Dec 6, 2024
1 parent 77b8c05 commit 394fb80
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target
/.direnv
gha-creds-*.json
.DS_Store
.DS_Store
google_service_account.json
9 changes: 7 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ FROM debian:bookworm-20240211

WORKDIR /app

COPY ./target/x86_64-unknown-linux-musl/release/icp-off-chain-agent .

RUN apt-get update \
&& apt-get install -y ca-certificates \
&& apt-get -y install curl \
&& apt-get -y install ffmpeg

EXPOSE 50051

ENV GOOGLE_SA_KEY=test
ENV YRAL_METADATA_TOKEN="test_token"
ENV QSTASH_CURRENT_SIGNING_KEY="test_key"
ENV QSTASH_AUTH_TOKEN="test_token"


# Latest releases available at https://github.com/aptible/supercronic/releases
# ENV SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.2.29/supercronic-linux-amd64 \
# SUPERCRONIC=supercronic-linux-amd64 \
Expand All @@ -25,4 +29,5 @@ EXPOSE 50051
# # You might need to change this depending on where your crontab is located
# COPY crontab crontab

COPY ./target/x86_64-unknown-linux-musl/release/icp-off-chain-agent .
CMD ["./icp-off-chain-agent"]
26 changes: 21 additions & 5 deletions src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ use yup_oauth2::{authenticator::Authenticator, ServiceAccountAuthenticator};
pub struct AppState {
pub agent: ic_agent::Agent,
pub yral_metadata_client: MetadataClient<true>,
#[cfg(not(feature = "local-bin"))]
pub auth: Authenticator<HttpsConnector<HttpConnector>>,
#[cfg(not(feature = "local-bin"))]
pub firestoredb: FirestoreDb,
pub qstash: QStashState,
#[cfg(not(feature = "local-bin"))]
pub bigquery_client: Client,
pub nsfw_detect_channel: Channel,
pub qstash_client: QStashClient,
#[cfg(not(feature = "local-bin"))]
pub gcs_client: Arc<cloud_storage::Client>,
}

Expand All @@ -34,24 +38,36 @@ impl AppState {
AppState {
yral_metadata_client: init_yral_metadata_client(&app_config),
agent: init_agent().await,
#[cfg(not(feature = "local-bin"))]
auth: init_auth().await,
// ml_server_grpc_channel: init_ml_server_grpc_channel().await,
#[cfg(not(feature = "local-bin"))]
firestoredb: init_firestoredb().await,
qstash: init_qstash(),
#[cfg(not(feature = "local-bin"))]
bigquery_client: init_bigquery_client().await,
nsfw_detect_channel: init_nsfw_detect_channel().await,
qstash_client: init_qstash_client().await,
#[cfg(not(feature = "local-bin"))]
gcs_client: Arc::new(cloud_storage::Client::default()),
}
}

pub async fn get_access_token(&self, scopes: &[&str]) -> String {
let auth = &self.auth;
let token = auth.token(scopes).await.unwrap();
#[cfg(feature = "local-bin")]
{
"localtoken".into()
}

#[cfg(not(feature = "local-bin"))]
{
let auth = &self.auth;
let token = auth.token(scopes).await.unwrap();

match token.token() {
Some(t) => t.to_string(),
_ => panic!("No access token found"),
match token.token() {
Some(t) => t.to_string(),
_ => panic!("No access token found"),
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/events/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ impl Event {
});
}

#[cfg(not(feature = "local-bin"))]
pub fn stream_to_firestore(&self, app_state: &AppState) {
if self.event.event == "token_creation_completed" {
let app_state = app_state.clone();
Expand Down Expand Up @@ -305,6 +306,15 @@ fn system_time_to_custom(time: std::time::SystemTime) -> SystemTime {
}
}

#[cfg(feature = "local-bin")]
pub async fn stream_to_bigquery_token_metadata_impl_v2(
app_state: &AppState,
data: ICPumpTokenMetadata,
) -> Result<(), anyhow::Error> {
Ok(())
}

#[cfg(not(feature = "local-bin"))]
pub async fn stream_to_bigquery_token_metadata_impl_v2(
app_state: &AppState,
data: ICPumpTokenMetadata,
Expand Down
3 changes: 3 additions & 0 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl WarehouseEvents for WarehouseEventsService {
let params: Value = serde_json::from_str(&event.event.params).expect("Invalid JSON");
let event_type: &str = &event.event.event;

#[cfg(not(feature = "local-bin"))]
event.stream_to_bigquery(&shared_state.clone());

event.upload_to_gcs(&shared_state.clone());
Expand All @@ -47,8 +48,10 @@ impl WarehouseEvents for WarehouseEventsService {

event.update_success_history(&shared_state.clone());

#[cfg(not(feature = "local-bin"))]
event.stream_to_firestore(&shared_state.clone());

#[cfg(not(feature = "local-bin"))]
event.stream_to_bigquery_token_metadata(&shared_state.clone());

let _ = dispatch_notif(event_type, params, &shared_state.clone()).await;
Expand Down
10 changes: 10 additions & 0 deletions src/events/nsfw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub async fn extract_frames_and_upload(
);
let output_dir = create_output_directory(&video_id)?;
let frames = extract_frames(&video_path, output_dir.clone())?;
#[cfg(not(feature = "local-bin"))]
upload_frames_to_gcs(&state.gcs_client, frames, &video_id).await?;
// delete output directory
fs::remove_dir_all(output_dir)?;
Expand Down Expand Up @@ -176,6 +177,15 @@ struct VideoNSFWData {
nsfw_gore: String,
}

#[cfg(feature = "local-bin")]
pub async fn nsfw_job(
State(state): State<Arc<AppState>>,
Json(payload): Json<VideoRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
Err(anyhow::anyhow!("not implemented for local binary").into())
}

#[cfg(not(feature = "local-bin"))]
pub async fn nsfw_job(
State(state): State<Arc<AppState>>,
Json(payload): Json<VideoRequest>,
Expand Down

0 comments on commit 394fb80

Please sign in to comment.