Adding button to kill and disable all downloads.

This commit is contained in:
Roland 2022-01-11 10:08:47 -07:00
parent daf795662b
commit ad9b970212
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
4 changed files with 130 additions and 2 deletions

View file

@ -358,6 +358,18 @@ export async function killDownload(download_id) {
return response;
}
export async function killDownloads() {
let response = await apiPost(`${API_URI}/download/kill`);
return response;
}
export async function startDownloads() {
let response = await apiPost(`${API_URI}/download/enable`);
return response;
}
export async function getDownloaders() {
let response = await apiGet(`${API_URI}/downloaders`);
return await response.json();

View file

@ -12,7 +12,16 @@ import {
Tab,
Table
} from "semantic-ui-react";
import {getConfig, getDownloads, killDownload, postDownload, saveConfig} from "../api";
import {
getConfig,
getDownloaders,
getDownloads,
killDownload,
killDownloads,
postDownload,
saveConfig,
startDownloads
} from "../api";
import TimezoneSelect from 'react-timezone-select';
import {secondsToDate, secondsToFrequency} from "./Common";
@ -288,11 +297,32 @@ class Downloads extends React.Component {
once_downloads: null,
recurring_downloads: null,
pending_downloads: null,
stopOpen: false,
disabled: true,
};
}
async componentDidMount() {
await this.fetchDownloads();
await this.fetchStatus();
}
killDownloads = async () => {
await killDownloads();
this.setState({stopOpen: false}, this.fetchStatus);
}
startDownloads = async () => {
await startDownloads();
this.setState({stopOpen: false}, this.fetchStatus);
}
closeStop = () => {
this.setState({stopOpen: false});
}
openStop = () => {
this.setState({stopOpen: true});
}
fetchDownloads = async () => {
@ -304,6 +334,11 @@ class Downloads extends React.Component {
});
}
fetchStatus = async () => {
let data = await getDownloaders();
this.setState({disabled: data['manager_disabled']});
}
render() {
let tablePlaceholder = (
<Placeholder>
@ -359,8 +394,40 @@ class Downloads extends React.Component {
</Table>);
}
let {stopOpen} = this.state;
let allButton = (<>
<Button
onClick={this.openStop}
color='red'
>Stop All</Button>
<Confirm
open={stopOpen}
content='Are you sure you want to stop all downloads?'
confirmButton='Stop'
onCancel={this.closeStop}
onConfirm={this.killDownloads}
/>
</>
);
if (this.state.disabled) {
allButton = (<>
<Button
onClick={this.openStop}
color='green'
>Start All</Button>
<Confirm
open={stopOpen}
content='Are you sure you want to start all downloads?'
confirmButton='Start'
onCancel={this.closeStop}
onConfirm={this.startDownloads}
/>
</>);
}
return (
<div>
{allButton}
<Header as='h1'>Downloads</Header>
{onceTable}

View file

@ -204,6 +204,7 @@ class DownloadManager:
def __init__(self):
self.instances = tuple()
self._instances = dict()
self.disabled = multiprocessing.Event()
def register_downloader(self, instance: Downloader):
if not isinstance(instance, Downloader):
@ -316,6 +317,9 @@ class DownloadManager:
This method calls the Downloader's do_download method.
"""
# This is a long-running function, lets get a session that can be used for a long time.
if self.disabled.is_set():
raise InvalidDownload('DownloadManager is disabled')
try:
downloads = self.get_new_downloads(session)
@ -516,6 +520,31 @@ class DownloadManager:
downloader.kill()
download.fail()
def kill(self):
"""
Kill all downloads.
"""
self.disabled.set()
try:
with get_db_session(commit=True) as session:
downloads = self.get_pending_downloads(session)
for download in downloads:
if not download.downloader:
downloader, _ = self.get_downloader(download.url)
else:
downloader = self.get_downloader_by_name(download.downloader)
downloader.kill()
download.defer()
except Exception as e:
logger.critical(f'Failed to kill downloads!', exc_info=e)
def enable(self):
"""
Enable downloading. Start downloading.
"""
self.disabled.clear()
self.start_downloads()
FINISHED_STATUSES = ('complete', 'failed')
def delete_old_once_downloads(self):
@ -544,6 +573,10 @@ class DownloadManager:
)
return data
@optional_session
def get_pending_downloads(self, session: Session):
return session.query(Download).filter_by(status='pending').all()
# The global DownloadManager. This should be used everywhere!
download_manager = DownloadManager()

View file

@ -152,6 +152,9 @@ def update_settings(_: Request, data: dict):
save_settings_config(data)
if data.get('wrolpi_mode'):
download_manager.kill()
return response.raw('', HTTPStatus.NO_CONTENT)
@ -203,6 +206,18 @@ async def kill_download(request: Request, download_id: int):
return response.empty()
@root_api.post('/download/kill')
async def kill_downloads(request: Request):
download_manager.kill()
return response.empty()
@root_api.post('/download/enable')
async def enable_downloads(request: Request):
download_manager.enable()
return response.empty()
@root_api.delete('/download/<download_id:integer>')
@wrol_mode_check
async def delete_download(request: Request, download_id: int):
@ -214,7 +229,8 @@ async def delete_download(request: Request, download_id: int):
async def get_downloaders(request: Request):
downloaders = download_manager.list_downloaders()
downloaders.insert(0, dict(name='auto', pretty_name='Automatic'))
ret = dict(downloaders=downloaders)
disabled = download_manager.disabled.is_set()
ret = dict(downloaders=downloaders, manager_disabled=disabled)
return json_response(ret)