Skip to content

Commit

Permalink
Pass ExternalDataProvider to job parser in RunManual mode
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyKasmy committed Apr 5, 2023
1 parent fc31e1d commit ed5c02b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 36 deletions.
4 changes: 2 additions & 2 deletions fetcher-config/src/jobs/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Task {
match external.read_filter(job, task_name, expected_rf_type) {
ExternalDataResult::Ok(rf) => Some(Arc::new(RwLock::new(rf))),
ExternalDataResult::Unavailable => {
tracing::warn!("Read filter is unavailable, skipping");
tracing::info!("Read filter is unavailable, skipping");
None
}
ExternalDataResult::Err(e) => return Err(e.into()),
Expand Down Expand Up @@ -94,7 +94,7 @@ impl Task {
match external.entry_to_msg_map(job, task_name) {
ExternalDataResult::Ok(v) => Some(v),
ExternalDataResult::Unavailable => {
tracing::warn!("Entry to message map is unavailable, skipping...");
tracing::info!("Entry to message map is unavailable, skipping...");
None
}
ExternalDataResult::Err(e) => return Err(e.into()),
Expand Down
62 changes: 29 additions & 33 deletions fetcher/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use either::Either;
use fetcher_config::jobs::named::{JobName, JobWithTaskNames};
use crate::settings::{context::StaticContext, external_data_provider::ExternalDataFromDataDir};
use fetcher_config::jobs::{
named::{JobName, JobWithTaskNames},
Job as ConfigJob,
};

use argh::FromArgs;
use color_eyre::{eyre::eyre, Report};
use std::{iter, path::PathBuf, str::FromStr};
use color_eyre::{eyre::eyre, Report, Result};
use std::{path::PathBuf, str::FromStr};

/// Automatic news fetching and parsing
#[derive(FromArgs, Debug)]
Expand Down Expand Up @@ -128,47 +131,40 @@ impl FromStr for Setting {

/// Wrapper around Job foreign struct to implement `FromStr` from valid job JSON
#[derive(Debug)]
pub struct JsonJobs(pub Vec<(JobName, JobWithTaskNames)>);
pub struct JsonJobs(Vec<(JobName, ConfigJob)>);

impl FromStr for JsonJobs {
type Err = Report;

fn from_str(s: &str) -> Result<Self, Self::Err> {
use fetcher_config::jobs::external_data::ProvideExternalData;
use fetcher_core::read_filter::ReadFilter;

struct EmptyExternalData;

// TODO: add a way to provide external settings even in manual jobs
impl ProvideExternalData for EmptyExternalData {
// it's a lie but don't tell anybody...
type ReadFilter = Box<dyn ReadFilter>;
}

// HACK: if the input is a JSON array
let config_jobs = if s
.chars()
.next()
.ok_or_else(|| eyre!("Manual job JSON is empty"))?
== '['
{
let config_jobs = if s.chars().next().expect("Manual job JSON is empty") == '[' {
let v: Vec<fetcher_config::jobs::Job> = serde_json::from_str(s)?;
Either::Left(
v.into_iter()
.enumerate()
// use the index as the job name
.map(|(idx, job)| ((idx + 1).to_string(), job)),
)
v.into_iter()
.enumerate()
// use the index as the job name
.map(|(idx, job)| ((idx + 1).to_string().into(), job))
.collect()
} else {
let job: fetcher_config::jobs::Job = serde_json::from_str(s)?;
// just use "Manual" as the job name
Either::Right(iter::once(("Manual".to_owned(), job)))
vec![("Manual".to_owned().into(), job)]
};

let jobs: Result<Vec<_>, _> = config_jobs
.map(|(name, job)| job.parse(name.into(), &EmptyExternalData))
.collect();
Ok(Self(config_jobs))
}
}

Ok(Self(jobs?))
impl JsonJobs {
pub fn parse(
self,
cx: StaticContext,
) -> Result<impl Iterator<Item = (JobName, JobWithTaskNames)>> {
Ok(self
.0
.into_iter()
.map(|(name, config)| config.parse(name, &ExternalDataFromDataDir { cx }))
.collect::<Result<Vec<_>, _>>()?
.into_iter())
}
}
2 changes: 1 addition & 1 deletion fetcher/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ async fn async_main() -> Result<()> {
Some(args::TopLvlSubcommand::Run(run_args)) => run_command(run_args, cx).await,
None => run_command(args::Run::default(), cx).await,
Some(args::TopLvlSubcommand::RunManual(args::RunManual { jobs })) => {
run_jobs(jobs.0.into_iter(), ErrorHandling::Forward, cx).await?;
run_jobs(jobs.parse(cx)?, ErrorHandling::Forward, cx).await?;

Ok(())
}
Expand Down

0 comments on commit ed5c02b

Please sign in to comment.