-
Notifications
You must be signed in to change notification settings - Fork 649
Implement ProcessCdnLog
and ProcessCdnLogQueue
background jobs
#8036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f23b362
worker/environment: Add `config` field
Turbo87 e457746
Implement `ProcessCdnLog` background job
Turbo87 5bec7d5
worker/jobs/downloads: Extract `CdnLogStorageConfig` enum
Turbo87 643c2c9
worker/jobs/downloads: Extract inner `run()` fn
Turbo87 17204e7
worker/jobs/downloads: Add tests for `ProcessCdnLog`
Turbo87 f8bb485
Implement `ProcessCdnLogQueue` background job
Turbo87 c6f7e32
Extract `SqsQueue` trait
Turbo87 08836d0
worker/jobs/downloads: Extract `CdnLogQueueConfig` enum
Turbo87 9a12705
worker/jobs/downloads: Extract inner `run()` fn
Turbo87 c27b101
worker/jobs/downloads: Add tests for `ProcessCdnLogQueue`
Turbo87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,11 +1,15 @@ | ||
mod balance_capacity; | ||
mod base; | ||
mod cdn_log_queue; | ||
mod cdn_log_storage; | ||
mod database_pools; | ||
mod sentry; | ||
mod server; | ||
|
||
pub use self::balance_capacity::BalanceCapacityConfig; | ||
pub use self::base::Base; | ||
pub use self::cdn_log_queue::CdnLogQueueConfig; | ||
pub use self::cdn_log_storage::CdnLogStorageConfig; | ||
pub use self::database_pools::{DatabasePools, DbPoolConfig}; | ||
pub use self::sentry::SentryConfig; | ||
pub use self::server::Server; |
This file contains hidden or 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,33 @@ | ||
use crates_io_env_vars::{required_var, var}; | ||
use secrecy::SecretString; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum CdnLogQueueConfig { | ||
SQS { | ||
access_key: String, | ||
secret_key: SecretString, | ||
queue_url: String, | ||
region: String, | ||
}, | ||
Mock, | ||
} | ||
|
||
impl CdnLogQueueConfig { | ||
pub fn from_env() -> anyhow::Result<Self> { | ||
if let Some(queue_url) = var("CDN_LOG_QUEUE_URL")? { | ||
let access_key = required_var("CDN_LOG_QUEUE_ACCESS_KEY")?; | ||
let secret_key = required_var("CDN_LOG_QUEUE_SECRET_KEY")?.into(); | ||
let region = required_var("CDN_LOG_QUEUE_REGION")?; | ||
|
||
return Ok(Self::SQS { | ||
access_key, | ||
secret_key, | ||
queue_url, | ||
region, | ||
}); | ||
} | ||
|
||
warn!("Falling back to mocked CDN log queue"); | ||
Ok(Self::Mock) | ||
} | ||
} |
This file contains hidden or 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,53 @@ | ||
use anyhow::Context; | ||
use crates_io_env_vars::{required_var, var}; | ||
use secrecy::SecretString; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum CdnLogStorageConfig { | ||
S3 { | ||
access_key: String, | ||
secret_key: SecretString, | ||
}, | ||
Local { | ||
path: PathBuf, | ||
}, | ||
Memory, | ||
} | ||
|
||
impl CdnLogStorageConfig { | ||
pub fn s3(access_key: String, secret_key: SecretString) -> Self { | ||
Self::S3 { | ||
access_key, | ||
secret_key, | ||
} | ||
} | ||
|
||
pub fn local(path: PathBuf) -> Self { | ||
Self::Local { path } | ||
} | ||
|
||
pub fn memory() -> Self { | ||
Self::Memory | ||
} | ||
|
||
pub fn from_env() -> anyhow::Result<Self> { | ||
if let Some(access_key) = var("AWS_ACCESS_KEY")? { | ||
let secret_key = required_var("AWS_SECRET_KEY")?.into(); | ||
return Ok(Self::s3(access_key, secret_key)); | ||
} | ||
|
||
let current_dir = std::env::current_dir(); | ||
let current_dir = current_dir.context("Failed to read the current directory")?; | ||
|
||
let path = current_dir.join("local_uploads"); | ||
let path_display = path.display(); | ||
if path.exists() { | ||
info!("Falling back to local CDN log storage at {path_display}"); | ||
return Ok(Self::local(path)); | ||
} | ||
|
||
warn!("Falling back to in-memory CDN log storage because {path_display} does not exist"); | ||
Ok(Self::memory()) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,94 @@ | ||
use anyhow::Context; | ||
use async_trait::async_trait; | ||
use aws_credential_types::Credentials; | ||
use aws_sdk_sqs::config::{BehaviorVersion, Region}; | ||
use aws_sdk_sqs::operation::receive_message::ReceiveMessageOutput; | ||
use mockall::automock; | ||
|
||
/// The [SqsQueue] trait defines a basic interface for interacting with an | ||
/// AWS SQS queue. | ||
/// | ||
/// A [MockSqsQueue] struct is automatically generated by the [automock] | ||
/// attribute. This struct can be used in unit tests to mock the behavior of | ||
/// the [SqsQueue] trait. | ||
/// | ||
/// The [SqsQueueImpl] struct is the actual implementation of the trait. | ||
#[automock] | ||
#[async_trait] | ||
pub trait SqsQueue { | ||
async fn receive_messages(&self, max_messages: i32) -> anyhow::Result<ReceiveMessageOutput>; | ||
async fn delete_message(&self, receipt_handle: &str) -> anyhow::Result<()>; | ||
} | ||
|
||
/// The [SqsQueueImpl] struct is the actual implementation of the [SqsQueue] | ||
/// trait, which interacts with the real AWS API servers. | ||
#[derive(Debug, Clone)] | ||
pub struct SqsQueueImpl { | ||
client: aws_sdk_sqs::Client, | ||
queue_url: String, | ||
} | ||
|
||
impl SqsQueueImpl { | ||
pub fn new(queue_url: impl Into<String>, region: Region, credentials: Credentials) -> Self { | ||
let config = aws_sdk_sqs::Config::builder() | ||
.credentials_provider(credentials) | ||
.region(region) | ||
.behavior_version(BehaviorVersion::v2023_11_09()) | ||
.build(); | ||
|
||
let client = aws_sdk_sqs::Client::from_conf(config); | ||
let queue_url = queue_url.into(); | ||
|
||
SqsQueueImpl { client, queue_url } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl SqsQueue for SqsQueueImpl { | ||
async fn receive_messages(&self, max_messages: i32) -> anyhow::Result<ReceiveMessageOutput> { | ||
let response = self | ||
.client | ||
.receive_message() | ||
.max_number_of_messages(max_messages) | ||
.queue_url(&self.queue_url) | ||
.send() | ||
.await | ||
.context("Failed to receive SQS queue message")?; | ||
|
||
Ok(response) | ||
} | ||
|
||
async fn delete_message(&self, receipt_handle: &str) -> anyhow::Result<()> { | ||
self.client | ||
.delete_message() | ||
.receipt_handle(receipt_handle) | ||
.queue_url(&self.queue_url) | ||
.send() | ||
.await | ||
.context("Failed to delete SQS queue message")?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_constructor() { | ||
let credentials = Credentials::new( | ||
"ANOTREAL", | ||
"notrealrnrELgWzOk3IfjzDKtFBhDby", | ||
None, | ||
None, | ||
"test", | ||
); | ||
|
||
let queue_url = "https://sqs.us-west-1.amazonaws.com/359172468976/cdn-log-event-queue"; | ||
let region = Region::new("us-west-1"); | ||
|
||
// Check that `SqsQueueImpl::new()` does not panic. | ||
let _queue = SqsQueueImpl::new(queue_url, region, credentials); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.