improvements

This commit is contained in:
Daniel Løvbrøtte Olsen 2020-03-30 02:21:16 +02:00
parent 2fedf3ff85
commit b5ce47f182
4 changed files with 40 additions and 152 deletions

View File

@ -18,4 +18,4 @@ uuid = { version = "0.7", features = ["serde", "v4"] }
[dependencies.rocket_contrib] [dependencies.rocket_contrib]
version = "0.4.4" version = "0.4.4"
default-features = false default-features = false
features = ["json", "uuid"] features = ["json", "uuid", "serve"]

View File

@ -9,6 +9,7 @@ use serde_json::Value;
use serde_json::json; use serde_json::json;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use rocket_contrib::uuid::Uuid; use rocket_contrib::uuid::Uuid;
use rocket_contrib::serve::StaticFiles;
use std::sync::Mutex; use std::sync::Mutex;
use std::collections::HashMap; use std::collections::HashMap;
@ -26,140 +27,10 @@ struct SharedState {
} }
#[get("/")] #[get("/")]
fn index() -> &'static str { fn index() -> String {
r#"#! /usr/bin/env nix-shell format!("Wecome to the AV1Master Server version {version}\n
#! nix-shell -i bash -p bash curl jq libaom ffmpeg-full This currently requires a working <a href=\"https://nixos.org/nix/\">nix</a> installion\n
curl -L {baseurl}/client.sh > client.sh && chmod +x ./client.sh && ./client.sh {baseurl}", version=VERSION, baseurl="https://av1.dodsorf.as")
set -euo pipefail
IFS=$'\n\t'
base_url="$1"
version="0.2.0"
while true; do
sleep 30
set +e
upsteam_version=`curl -s "$base_url"/version`
retval=$?
set -e
if [ $retval -ne 0 ]; then
echo "Is the Job Server Down?"
continue
fi
if [[ $version != $upsteam_version ]]; then
echo "Wrong version: client version is $version, while job server requires $upstream_version"
break
fi
set +e
job=`curl -s "$base_url"/request_job | jq`
retval=$?
set -e
if [[ $job = "null" ]] || [ $retval -ne 0 ]; then
echo "No Jobs Available ¯\_(ツ)_/¯"
continue
fi
echo "Got new job!"
echo "$job" | jq
job_id=`echo "$job" | jq -r .id`
echo "Reserving Job"
set +e
curl -s "$base_url"/edit_status/"$job_id"/reserved
retval=$?
set -e
if [ $retval -ne 0 ]; then
echo "Is the Job Server Down?"
continue
fi
echo "Reserved!"
source=`echo $job | jq -r .description.file_url`
sourceext=${source##*.}
echo "Downloading source file: $source"
source=`echo $job | jq -r .description.file_url`
name=`echo $job | jq -r .description.file_name`
input="$name.$job_id.$sourceext"
set +e
curl "$source" -o "$input"
retval=$?
set -e
if [ $retval -ne 0 ]; then
echo "Could not Download file!"
curl -s -L "$base_url"/edit_status/"$job_id"/error || true
continue
fi
echo ""
echo "Starting Encode"
height=`echo $job | jq -r .description.resolution[0]`
width=`echo $job | jq -r .description.resolution[1]`
echo $job | jq
aomenco=`echo $job | jq -r .description.options.aomenc`
aomenco=${aomenco//[^a-zA-Z0-9_\- =]/}
ffmpego=`echo $job | jq -r .description.options.ffmpeg`
ffmpego=${ffmpego//[^a-zA-Z0-9_\- =:]/}
two_pass=`echo $job | jq -r .description.options.two_pass`
if [[ $two_pass = true ]]; then
set +e
eval 'ffmpeg -nostats -hide_banner -loglevel warning \
-i "'$input'" '$ffmpego' -vf scale='$height':'$width' -pix_fmt yuv422p -f yuv4mpegpipe - | aomenc - --i422 '$aomenco' \
--pass=1 --passes=2 --fpf="'$input'.fpf" --webm -o "'$input'.out.webm"'
retval=$?
if [ $retval -ne 0 ]; then
echo "Error running encode pass 1"
curl -s -L "$base_url"/edit_status/"$job_id"/error || true
echo ""
continue
fi
eval 'ffmpeg -nostats -hide_banner -loglevel warning \
-i "'$input'" '$ffmpego' -vf scale='$height':'$width' -pix_fmt yuv422p -f yuv4mpegpipe - | aomenc - --i422 '$aomenco' \
--pass=2 --passes=2 --fpf="'$input'.fpf" --webm -o "'$input'.out.webm"'
retval=$?
if [ $retval -ne 0 ]; then
echo "Error running encode pass 2"
curl -s -L "$base_url"/edit_status/"$job_id"/error || true
echo ""
continue
fi
set -e
else
set +e
eval 'ffmpeg -nostats -hide_banner -loglevel warning \
-i "'$input'" '$ffmpego' -vf scale='$height':'$width' -pix_fmt yuv422p -f yuv4mpegpipe - | aomenc - --i422 '$aomenco' \
--passes=1 --fpf="'$input'.fpf" --webm -o "'$input'.out.webm"'
retval=$?
if [ $retval -ne 0 ]; then
echo "Error running encode"
curl -s -L "$base_url"/edit_status/"$job_id"/error || true
echo ""
continue
fi
set -e
fi
set +e
curl -s -L "$base_url"/edit_status/"$job_id"/completed
set -e
done
"#
} }
#[get("/version")] #[get("/version")]
@ -202,15 +73,28 @@ fn get_job(id: Uuid, shared: State<SharedState>) -> Result<Json<Value>, NotFound
} }
} }
pub struct RealIP(std::net::IpAddr);
impl<'a, 'r> rocket::request::FromRequest<'a, 'r> for RealIP {
type Error = ();
fn from_request(request: &'a rocket::Request<'r>) -> rocket::request::Outcome<Self, Self::Error> {
match request.client_ip() {
Some(ip) => rocket::Outcome::Success(RealIP(ip)),
None => rocket::Outcome::Failure((rocket::http::Status::from_code(401).unwrap(), ()))
}
}
}
#[get("/edit_status/<id>/<status>")] #[get("/edit_status/<id>/<status>")]
fn edit_status(id: Uuid, status: String, shared: State<SharedState>, remote_addr: SocketAddr) -> Result<String, Box<std::error::Error>> { fn edit_status(id: Uuid, status: String, shared: State<SharedState>, remote_addr: RealIP) -> Result<String, Box<std::error::Error>> {
let mut list = shared.jobs.lock().unwrap(); let mut list = shared.jobs.lock().unwrap();
let job = list.get_mut(&id).ok_or("what")?; let job = list.get_mut(&id).ok_or("what")?;
let status = match status.as_str() { let status = match status.as_str() {
"queued" => Ok(EStatus::Queued), "queued" => Ok(EStatus::Queued),
"reserved" => Ok(EStatus::Reserved(remote_addr.to_string())), "reserved" => Ok(EStatus::Reserved(remote_addr.0.to_string())),
"completed" => Ok(EStatus::Completed(remote_addr.to_string())), "completed" => Ok(EStatus::Completed(remote_addr.0.to_string())),
"error" => Ok(EStatus::Error(remote_addr.to_string())), "error" => Ok(EStatus::Error(remote_addr.0.to_string())),
_ => Err("Not a valid status, valid statuses are queued, reserved, completed, and error") _ => Err("Not a valid status, valid statuses are queued, reserved, completed, and error")
}?; }?;
@ -232,6 +116,7 @@ fn add_job(message: Json<workunit::WDesc>, shared: State<SharedState>) {
fn main() { fn main() {
rocket::ignite() rocket::ignite()
.manage(SharedState::default()) .manage(SharedState::default())
.mount("/", StaticFiles::from("src/static")) // switch to templates or something cause this is dumb
.mount("/", routes![index, version, get_jobs, get_job, request_job, edit_status, add_job]) .mount("/", routes![index, version, get_jobs, get_job, request_job, edit_status, add_job])
.launch(); .launch();
} }

View File

@ -10,14 +10,14 @@ version="0.2.0"
while true; do while true; do
sleep 30 sleep 30
set +e set +e
upsteam_version=`curl -s "$base_url"/version` upstream_version=`curl -s "$base_url"/version`
retval=$? retval=$?
set -e set -e
if [ $retval -ne 0 ]; then if [ $retval -ne 0 ]; then
echo "Is the Job Server Down?" echo "Is the Job Server Down?"
continue continue
fi fi
if [[ $version != $upsteam_version ]]; then if [[ $version != $upstream_version ]]; then
echo "Wrong version: client version is $version, while job server requires $upstream_version" echo "Wrong version: client version is $version, while job server requires $upstream_version"
break break
fi fi
@ -32,9 +32,9 @@ while true; do
fi fi
echo "Got new job!" echo "Got new job!"
echo "$job" | jq printf "%s\n" "$job" | jq
job_id=`echo "$job" | jq -r .id` job_id=`printf "%s\n" "$job" | jq -r .id`
echo "Reserving Job" echo "Reserving Job"
set +e set +e
@ -47,13 +47,13 @@ while true; do
fi fi
echo "Reserved!" echo "Reserved!"
source=`echo $job | jq -r .description.file_url` source=`printf "%s\n" "$job" | jq -r .description.file_url`
sourceext=${source##*.} sourceext=${source##*.}
echo "Downloading source file: $source" echo "Downloading source file: $source"
source=`echo $job | jq -r .description.file_url` source=`printf "%s\n" "$job" | jq -r .description.file_url`
name=`echo $job | jq -r .description.file_name` name=`printf "%s\n" "$job" | jq -r .description.file_name`
input="$name.$job_id.$sourceext" input="$name.$job_id.$sourceext"
set +e set +e
@ -63,6 +63,7 @@ while true; do
if [ $retval -ne 0 ]; then if [ $retval -ne 0 ]; then
echo "Could not Download file!" echo "Could not Download file!"
curl -s -L "$base_url"/edit_status/"$job_id"/error || true curl -s -L "$base_url"/edit_status/"$job_id"/error || true
echo ""
continue continue
fi fi
@ -70,17 +71,17 @@ while true; do
echo "Starting Encode" echo "Starting Encode"
height=`echo $job | jq -r .description.resolution[0]` height=`printf "%s\n" $job | jq -r .description.resolution[0]`
width=`echo $job | jq -r .description.resolution[1]` width=`printf "%s\n" $job | jq -r .description.resolution[1]`
echo $job | jq printf "%s\n" "$job" | jq
aomenco=`echo $job | jq -r .description.options.aomenc` aomenco=`printf "%s\n" "$job" | jq -r .description.options.aomenc`
aomenco=${aomenco//[^a-zA-Z0-9_\- =]/} aomenco=${aomenco//[^a-zA-Z0-9_\- =]/}
ffmpego=`echo $job | jq -r .description.options.ffmpeg` ffmpego=`printf "%s\n" "$job" | jq -r .description.options.ffmpeg`
ffmpego=${ffmpego//[^a-zA-Z0-9_\- =:]/} ffmpego=${ffmpego//[^a-zA-Z0-9_\- =:]/}
two_pass=`echo $job | jq -r .description.options.two_pass` two_pass=`printf "%s\n" "$job" | jq -r .description.options.two_pass`
if [[ $two_pass = true ]]; then if [[ $two_pass = true ]]; then
set +e set +e

View File

@ -2,6 +2,8 @@ use serde::{Serialize, Deserialize};
use serde_repr::*; use serde_repr::*;
use uuid::Uuid; use uuid::Uuid;
use std::net::IpAddr;
#[derive(Default, Debug, Serialize, Deserialize, Clone)] #[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct WUnit { pub struct WUnit {
pub id: Uuid, pub id: Uuid,