shadowmoose / RedditDownloader

Scrapes Reddit to download media of your choice.
1.11k stars 99 forks source link

Stop the workers gracefully #90

Open xatnight opened 4 years ago

xatnight commented 4 years ago

Is your feature request related to a problem? Please describe.

Maybe I'm dumb, but I'd like a way to stop the downloaders gracefully so they can finish what ever task they are doing. I'm hitting the daily imgur API limit and I'm scared of database corruption when hitting CTRL+C or just closing the script.

Describe the solution you'd like:

A way to stop the downloaders with a button in the WebUI.

Additional context:

Is this even possible without huge amount of work?

shadowmoose commented 4 years ago

I agree, I need to look at properly catching kill commands, in order to suppress the huge wall of error text that they cause. However, as far as termination during runtime goes, it shouldn't be possible to corrupt RMD's database (or even the current progress) by terminating at any time.

All relevant database work is handled atomically, wrapped in transactional units of work. The worst-case is that RMD might have to re-download some files it was in the process of downloading, though handlers like YTDL are resilient even against this sort of progress loss.

I'm going to track this to remind myself to look at more graceful error handling though. In theory I've already built in support for a global kill event, I just need to wrap the right section of code to handle interrupts.

B13rg commented 2 years ago

I looked at this a little. It doesn't look too difficult, Would just need to add the functionality to the button "start" button, then clear the queue and wait for the downloader threads to join (or just kill them).

One thing about the stopping is that it currently stops all threads, downloader, loader, and deduplicator.

If it is safe, it might be useful for the deduplicator thread to continue running, since it should just be acting locally and is useful outside of the download process, but it also might be better to just manage all the threads at once instead of trying to leave it running.