DEV Community

Grayson Hay
Grayson Hay

Posted on • Originally published at garden.graysonarts.com

2

Rust Snippet: Exec with Update Intervals

Recently, I needed to kick off a long running process for encoding videos using ffmpeg in the middle of handling an SQS message. In order to keep the message from showing back up in the queue before the video processing is finished.

So that means, I need to be able to send the change message visibility timeout periodically during the process. so, I came up with this little function to help. It calls a “progress” function every 10 seconds while the command is executing, and then ends once it’s done.

Using tokio as the Async Runtime

pub async fn exec<F, Fut>(mut cmd: Command, progress: F) -> Result<Output, ProgressingExecError>
where
    F: Fn() -> Fut,
    Fut: Future<Output = ()>,
{
    let (tx, mut rx) = oneshot::channel();
    let mut interval = interval(Duration::from_millis(10000));

    tokio::spawn(async move {
        let output = cmd.output().await;
        let _ = tx.send(output);
    });

    loop {
        tokio::select! {
            _ = interval.tick() => progress().await,
            msg = &mut rx => return Ok(msg??)
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay