question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] Cancel a running job

See original GitHub issue

Is your feature request related to a problem? Please describe. I’m making a browser extension that uses this module, and the user might want to cancel an occurring FFmpeg job at any time, which is not currently possible.

Describe the solution you’d like

ffmpeg.abort();

Describe alternatives you’ve considered My current somewhat-working workaround is:

class CancellablePromise {
  private readonly symbolAbort = Symbol("cancelled");
  private readonly promiseAbort: Promise<any>;
  private resolve!: Function; // Works due to promise init

  constructor() {
    this.promiseAbort = new Promise(resolve => (this.resolve = resolve));
  }

  public async wrap<T>(promise: PromiseLike<T>): Promise<T> {
    const result = await Promise.race([promise, this.promiseAbort]);
    if (result === this.symbolAbort) {
      throw new Error("Aborting FFmpeg");
    }

    return result;
  }

  public abort() {
    this.resolve(this.symbolAbort);
  }
}

Then, in the code:

import { createFFmpeg } from "@ffmpeg/ffmpeg";

const ffmpeg = createFFmpeg({ log: true });

chrome.runtime.onConnect.addListener(async port => {
  if (port.name === "run-ffmpeg-job") {
    if (!ffmoeg.isLoaded()) {
      await ffmpeg.load();
    }

    const promise = new CancellablePromise();
    ffmpeg.FS("writeFile", "filename.mp4");
    const promiseJob = promise.wrap(ffmpeg.run("-i", "filename.mp4", /*...*/));

    port.onDisconnect.addListener(() => {
      promise.abort();
    });

    await promiseJob;
  }
});

The problem with promise.abort() is that FFmpeg will still run (output will still flow to the console).

Additional context

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
selmaleecommented, Jul 22, 2021

Interesting, and to reload FFmpeg I just call ffmpeg.load()?

createFFmpeg()and ffmpeg.load()

0reactions
avi12commented, Aug 11, 2021

After calling ffmpeg.exit(), any code that was after the ffmpeg.run() isn’t being executed, even if I wrap the whole FFmpeg code block in try-catch Think like:

let ffmpeg;

async function initFFmpeg() {
  ffmpeg = createFFmpeg({ log: true });
  await ffmpeg.load();
}

initFFmpeg();

const elRun = document.querySelector(".run-job");
elRun.addEventListener("click", async () => {
  ffmpeg.F8(...)
  await ffmpeg.run(..)

  someCode();
});

const elCancel = document.querySelector(".cancel-job");
elCancel.addEventListener("click", () => {
  try {
    ffmpeg.exit();
  } catch {}
});

The someCode() part will not execute if elCancel’s callback gets executed

How do I make a code execute afterward?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cancel a Running Job - TechDocs - Broadcom Inc.
Select. Jobs. from the. Monitor. menu. · Select the job that you want to cancel, and select. Cancel active job. from the. JOB....
Read more >
Cancel a Running Job - Analytics Cloud - Oracle Help Center
Click the Cancel Running Jobs icon at the top of the table. Click OK in the confirmation message.
Read more >
Force Cancel a Job or Task - Job Manager | Microsoft Learn
When a task is force canceled, the task and its sub-tasks skip the grace period and are stopped immediately.
Read more >
Best way to implement graceful cancel for running async jobs ...
Provide it jobs that will be run asynchronously and could return a result; Shutdown the executor so that all running jobs are cancelled....
Read more >
Ability to cancel running jobs? · Issue #684 · rq/rq - GitHub
If it gets a cancel command it will call the stop command of the worker. Has anyone implemented something like this already? Will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found