Skip to content

Commit

Permalink
Make 'add_to_custom_data' friendlier
Browse files Browse the repository at this point in the history
  • Loading branch information
rustworthy committed Jan 6, 2024
1 parent 7d55f6f commit 5239e29
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/proto/single/ent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl JobBuilder {
/// ```
pub fn expires_at(&mut self, dt: DateTime<Utc>) -> &mut Self {
self.add_to_custom_data(
"expires_at".into(),
"expires_at",
dt.to_rfc3339_opts(chrono::SecondsFormat::Nanos, true),
)
}
Expand Down Expand Up @@ -47,19 +47,19 @@ impl JobBuilder {
/// the Enterprise Faktory [docs](https://github.com/contribsys/faktory/wiki/Ent-Unique-Jobs)
/// for details on how scheduling, retries and other features live together with `unique_for`.
pub fn unique_for(&mut self, secs: usize) -> &mut Self {
self.add_to_custom_data("unique_for".into(), secs)
self.add_to_custom_data("unique_for", secs)
}

/// Remove unique lock for this job right before the job starts executing.
pub fn unique_until_start(&mut self) -> &mut Self {
self.add_to_custom_data("unique_until".into(), "start")
self.add_to_custom_data("unique_until", "start")
}

/// Do not remove unique lock for this job until it successfully finishes.
///
/// Sets `unique_until` on the Job's custom hash to `success`, which is Faktory's default.
pub fn unique_until_success(&mut self) -> &mut Self {
self.add_to_custom_data("unique_until".into(), "success")
self.add_to_custom_data("unique_until", "success")
}
}

Expand Down Expand Up @@ -114,9 +114,9 @@ mod test {
let expires_at2 = Utc::now() + chrono::Duration::seconds(300);
let job = half_stuff()
.unique_for(60)
.add_to_custom_data("unique_for".into(), 600)
.add_to_custom_data("unique_for", 600)
.unique_for(40)
.add_to_custom_data("expires_at".into(), to_iso_string(expires_at1))
.add_to_custom_data("expires_at", to_iso_string(expires_at1))
.expires_at(expires_at2)
.build();
let stored_unique_for = job.custom.get("unique_for").unwrap();
Expand Down
10 changes: 7 additions & 3 deletions src/proto/single/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,13 @@ impl JobBuilder {
}

/// Sets arbitrary key-value pairs to this job's custom data hash.
pub fn add_to_custom_data(&mut self, k: String, v: impl Into<serde_json::Value>) -> &mut Self {
pub fn add_to_custom_data(
&mut self,
k: impl Into<String>,
v: impl Into<serde_json::Value>,
) -> &mut Self {
let custom = self.custom.get_or_insert_with(HashMap::new);
custom.insert(k, v.into());
custom.insert(k.into(), v.into());
self
}

Expand Down Expand Up @@ -319,7 +323,7 @@ mod test {
fn test_arbitrary_custom_data_setter() {
let job = JobBuilder::new("order")
.args(vec!["ISBN-13:9781718501850"])
.add_to_custom_data("arbitrary_key".into(), "arbitrary_value")
.add_to_custom_data("arbitrary_key", "arbitrary_value")
.build();

assert_eq!(
Expand Down

0 comments on commit 5239e29

Please sign in to comment.