2016-05-12 22:19:48 -05:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2019-01-02 04:24:12 -06:00
|
|
|
# Copyright (C) 2013 - 2019, The pgAdmin Development Team
|
2016-05-12 22:19:48 -05:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
"""
|
|
|
|
A blueprint module providing utility functions for the notify the user about
|
|
|
|
the long running background-processes.
|
|
|
|
"""
|
|
|
|
from flask import url_for
|
2016-07-22 10:25:23 -05:00
|
|
|
from flask_security import login_required
|
2016-05-12 22:19:48 -05:00
|
|
|
from pgadmin.utils import PgAdminModule
|
2017-02-04 08:26:57 -06:00
|
|
|
from pgadmin.utils.ajax import make_response, gone, success_return
|
2016-05-12 22:19:48 -05:00
|
|
|
|
|
|
|
from .processes import BatchProcess
|
|
|
|
|
|
|
|
MODULE_NAME = 'bgprocess'
|
|
|
|
|
|
|
|
|
|
|
|
class BGProcessModule(PgAdminModule):
|
|
|
|
def get_own_javascripts(self):
|
|
|
|
return [{
|
|
|
|
'name': 'pgadmin.browser.bgprocess',
|
|
|
|
'path': url_for('bgprocess.static', filename='js/bgprocess'),
|
|
|
|
'when': None
|
2016-06-21 08:21:06 -05:00
|
|
|
}]
|
2016-05-12 22:19:48 -05:00
|
|
|
|
|
|
|
def get_own_stylesheets(self):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
list: the stylesheets used by this module.
|
|
|
|
"""
|
2018-09-04 05:24:51 -05:00
|
|
|
stylesheets = []
|
2016-05-12 22:19:48 -05:00
|
|
|
return stylesheets
|
|
|
|
|
2017-06-12 03:54:14 -05:00
|
|
|
def get_exposed_url_endpoints(self):
|
2016-05-12 22:19:48 -05:00
|
|
|
"""
|
|
|
|
Returns:
|
2017-06-12 03:54:14 -05:00
|
|
|
list: URL endpoints for bgprocess
|
2016-05-12 22:19:48 -05:00
|
|
|
"""
|
2017-06-12 03:54:14 -05:00
|
|
|
return [
|
|
|
|
'bgprocess.status', 'bgprocess.detailed_status',
|
2018-10-22 02:05:21 -05:00
|
|
|
'bgprocess.acknowledge', 'bgprocess.list',
|
|
|
|
'bgprocess.stop_process'
|
2017-06-12 03:54:14 -05:00
|
|
|
]
|
2016-05-12 22:19:48 -05:00
|
|
|
|
2016-06-21 08:21:06 -05:00
|
|
|
|
2016-05-12 22:19:48 -05:00
|
|
|
# Initialise the module
|
|
|
|
blueprint = BGProcessModule(
|
|
|
|
MODULE_NAME, __name__, url_prefix='/misc/bgprocess'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-06-12 03:54:14 -05:00
|
|
|
@blueprint.route('/', methods=['GET'], endpoint='list')
|
2016-05-12 22:19:48 -05:00
|
|
|
@login_required
|
|
|
|
def index():
|
2017-02-04 08:26:57 -06:00
|
|
|
return make_response(response=BatchProcess.list())
|
2016-05-12 22:19:48 -05:00
|
|
|
|
|
|
|
|
2017-06-12 03:54:14 -05:00
|
|
|
@blueprint.route('/<pid>', methods=['GET'], endpoint='status')
|
|
|
|
@blueprint.route(
|
|
|
|
'/<pid>/<int:out>/<int:err>/', methods=['GET'], endpoint='detailed_status'
|
|
|
|
)
|
2016-05-12 22:19:48 -05:00
|
|
|
@login_required
|
|
|
|
def status(pid, out=-1, err=-1):
|
|
|
|
"""
|
|
|
|
Check the status of the process running in background.
|
|
|
|
Sends back the output of stdout/stderr
|
|
|
|
Fetches & sends STDOUT/STDERR logs for the process requested by client
|
|
|
|
|
|
|
|
Args:
|
|
|
|
pid: Process ID
|
|
|
|
out: position of the last stdout fetched
|
|
|
|
err: position of the last stderr fetched
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Status of the process and logs (if out, and err not equal to -1)
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
process = BatchProcess(id=pid)
|
|
|
|
|
|
|
|
return make_response(response=process.status(out, err))
|
|
|
|
except LookupError as lerr:
|
|
|
|
return gone(errormsg=str(lerr))
|
|
|
|
|
|
|
|
|
2017-06-12 03:54:14 -05:00
|
|
|
@blueprint.route('/<pid>', methods=['PUT'], endpoint='acknowledge')
|
2016-05-12 22:19:48 -05:00
|
|
|
@login_required
|
|
|
|
def acknowledge(pid):
|
|
|
|
"""
|
|
|
|
User has acknowledge the process
|
|
|
|
|
|
|
|
Args:
|
|
|
|
pid: Process ID
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Positive status
|
|
|
|
"""
|
|
|
|
try:
|
2017-02-04 08:26:57 -06:00
|
|
|
BatchProcess.acknowledge(pid)
|
2016-05-12 22:19:48 -05:00
|
|
|
return success_return()
|
|
|
|
except LookupError as lerr:
|
|
|
|
return gone(errormsg=str(lerr))
|
2018-10-22 02:05:21 -05:00
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/stop/<pid>', methods=['PUT'], endpoint='stop_process')
|
|
|
|
@login_required
|
|
|
|
def stop_process(pid):
|
|
|
|
"""
|
|
|
|
User has stopped the process
|
|
|
|
|
|
|
|
:param pid: Process ID
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
BatchProcess.stop_process(pid)
|
|
|
|
return success_return()
|
|
|
|
except LookupError as lerr:
|
|
|
|
return gone(errormsg=str(lerr))
|
2019-10-10 07:28:32 -05:00
|
|
|
|
|
|
|
|
|
|
|
def escape_dquotes_process_arg(arg):
|
|
|
|
# Double quotes has special meaning for shell command line and they are
|
|
|
|
# run without the double quotes. Add extra quotes to save our double
|
|
|
|
# quotes from stripping.
|
|
|
|
|
|
|
|
# This cannot be at common place as this file executes
|
|
|
|
# separately from pgadmin
|
|
|
|
dq_id = "#DQ#"
|
|
|
|
|
|
|
|
if arg.startswith('"') and arg.endswith('"'):
|
|
|
|
return r'{0}{1}{0}'.format(dq_id, arg)
|
|
|
|
else:
|
|
|
|
return arg
|