Commit 51357e3e authored by AdminXVII's avatar AdminXVII

Spawn threads to handle the jobs

parent 24e33082
/target
**/*.rs.bk
log
......@@ -201,6 +201,24 @@ name = "constant_time_eq"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "crossbeam-channel"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "crossbeam-utils"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "decimal"
version = "2.0.4"
......@@ -553,6 +571,7 @@ dependencies = [
name = "parallelion"
version = "0.1.0"
dependencies = [
"crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"ion-shell 1.0.0-alpha (git+https://gitlab.redox-os.org/redox-os/ion)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -1067,6 +1086,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
"checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e"
"checksum crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0f0ed1a4de2235cabda8558ff5840bffb97fcb64c97827f354a451307df5f72b"
"checksum crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f8306fcef4a7b563b76b7dd949ca48f52bc1141aa067d2ea09565f3e2652aa5c"
"checksum decimal 2.0.4 (git+https://github.com/alkis/decimal.git)" = "<none>"
"checksum derive_utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8051728ec2caf87b15d051687024572810187693f9d8f4ec561714d7a3fa7d9c"
"checksum dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901"
......
......@@ -8,6 +8,7 @@ edition = "2018"
ion-shell = { git = "https://gitlab.redox-os.org/redox-os/ion" }
num_cpus = "0.2"
structopt = "0.2"
crossbeam-channel = "0.3"
# logging
log = "0.4"
......
use crossbeam_channel::{Receiver, Sender};
use log::{debug, error, info, trace, warn};
use simplelog::*;
use std::fs::File;
use std::path::PathBuf;
use std::sync::Arc;
use std::thread;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "parallelion", about = "An example of StructOpt usage.")]
struct Opt {
/// Use NUL as delimiter instead of \n (newline). Useful if arguments can contain \n
#[structopt(short = "0", long)]
null: bool,
struct Opts {
/// Show progress
///
/// Displays % of jobs completed, ETA, number of jobs running, number of jobs started
......@@ -29,10 +28,11 @@ struct Opt {
/// (start), duration in floating-point seconds (duration), command run (cmd), exit status (status)
#[structopt(short, long, parse(from_os_str))]
log: Option<PathBuf>,
/// Timestamp (sec, ms, ns, none)
#[structopt(long)]
timestamp: Option<String>,
// /// Timestamp (sec, ms, ns, none)
// #[structopt(long)]
// timestamp: Option<String>,
// dry run
/// Print the jobs to stdout, but don't execute them
#[structopt(long = "dry-run")]
dry_run: bool,
......@@ -48,7 +48,7 @@ struct Opt {
/// Start n jobs in parallel. Defaults to the number of cores available. 0 indicates to run one
/// thread per job
#[structopt(short, long)]
jobs: Option<u8>,
jobs: Option<usize>,
/// Each line of the argfile will be treated as a replacement on the input
#[structopt(short, long = "arg-file", parse(from_os_str))]
......@@ -61,22 +61,19 @@ struct Opt {
arguments: Vec<String>,
}
fn main() {
let opt = Opt::from_args();
println!("{:#?}", opt);
let level = match (opt.quiet, opt.verbose) {
fn create_logger(opts: &Opts) {
let level = match (opts.quiet, opts.verbose) {
(true, _) => LevelFilter::Error,
(_, 0) => LevelFilter::Warn,
(_, 1) => LevelFilter::Info,
(_, 2) => LevelFilter::Debug,
(..) => LevelFilter::Trace,
};
let mut config = Config::default();
let config = Config::default();
// config.time_format = opt.timestamp;
let mut loggers: Vec<Box<dyn SharedLogger>> =
vec![TermLogger::new(level, config, TerminalMode::Stderr).unwrap()];
if let Some(file) = opt.log {
if let Some(file) = &opts.log {
loggers.push(WriteLogger::new(
LevelFilter::Info,
config,
......@@ -84,10 +81,48 @@ fn main() {
));
}
CombinedLogger::init(loggers).unwrap();
}
fn start_workers(n: usize, task: Arc<String>, jobs: Receiver<String>, results: Sender<usize>) {
info!("Starting {} worker threads", n);
for _ in 0..n {
let jobs = jobs.clone();
let results = results.clone();
let task = task.clone();
thread::spawn(move || {
while let Ok(job) = jobs.recv() {
let job = task.replace("{}", &job);
info!("Command: {}", job);
results.send(1).unwrap();
}
});
}
}
trace!("trace message");
debug!("debug message");
info!("info message");
warn!("warn message");
error!("error message");
fn main() {
let opts = Opts::from_args();
trace!("{:#?}", opts);
create_logger(&opts);
let (tx, rx) = crossbeam_channel::unbounded();
let (rtx, rrx) = crossbeam_channel::unbounded();
let command = Arc::new(opts.command);
start_workers(
opts.jobs
.unwrap_or(num_cpus::get())
.min(opts.arguments.len()),
command,
rx,
rtx,
);
for argument in opts.arguments {
tx.send(argument).unwrap();
}
std::mem::drop(tx);
while let Ok(result) = rrx.recv() {
eprintln!("result: {}", result);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment