Repository navigation
add cleaner for trashed ScaleDB databases #913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zwfxxxxx
wants to merge
1
commit into
master
Choose a base branch
from
feature/scaledb-trash-cleaner
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import os | ||
| import logging | ||
| from threading import Thread, Event | ||
|
|
||
| from dtable_events.utils import get_python_executable, run | ||
| from dtable_events.app.config import dtable_web_dir, TRASH_CLEAN_AFTER_DAYS | ||
|
|
||
| __all__ = [ | ||
| 'SeaDBDatabasesCleaner', | ||
| ] | ||
|
|
||
|
|
||
| class SeaDBDatabasesCleaner(object): | ||
|
|
||
| def __init__(self): | ||
| self._enabled = True | ||
| self._logfile = None | ||
| self._interval = 60 * 60 * 24 | ||
| self._prepare_logfile() | ||
| self._parse_config() | ||
|
|
||
| def _prepare_logfile(self): | ||
| logdir = os.path.join(os.environ.get('LOG_DIR', '')) | ||
| self._logfile = os.path.join(logdir, 'seadb_databases_cleaner.log') | ||
|
|
||
| def _parse_config(self): | ||
| self._expire_seconds = 60 * 60 * 24 * TRASH_CLEAN_AFTER_DAYS | ||
|
|
||
| def start(self): | ||
| if not self.is_enabled(): | ||
| logging.warning('Can not start seadb databases cleaner: it is not enabled!') | ||
| return | ||
|
|
||
| logging.info('Start seadb databases cleaner, interval = %s sec', self._interval) | ||
|
|
||
| SeaDBDatabasesCleanerTimer(self._interval, self._logfile, self._expire_seconds).start() | ||
|
|
||
| def is_enabled(self): | ||
| return self._enabled | ||
|
|
||
|
|
||
| class SeaDBDatabasesCleanerTimer(Thread): | ||
|
|
||
| def __init__(self, interval, logfile, | ||
| expire_seconds=30*60): | ||
| super(SeaDBDatabasesCleanerTimer, self).__init__() | ||
| self._interval = interval | ||
| self._logfile = logfile | ||
| self._expire_seconds = expire_seconds | ||
|
|
||
| self.finished = Event() | ||
|
|
||
| def run(self): | ||
| while not self.finished.is_set(): | ||
| self.finished.wait(self._interval) | ||
| if not self.finished.is_set(): | ||
| logging.info('Starts to clean trash seadb databases...') | ||
| try: | ||
| python_exec = get_python_executable() | ||
| manage_py = os.path.join(dtable_web_dir, 'manage.py') | ||
| cmd = [ | ||
| python_exec, | ||
| manage_py, | ||
| 'clean_trash_seadb_databases', | ||
| self._expire_seconds, | ||
| ] | ||
| with open(self._logfile, 'a') as fp: | ||
| run(cmd, cwd=dtable_web_dir, output=fp) | ||
| except Exception as e: | ||
| logging.exception('error when cleaning trash seadb databases: %s', e) | ||
|
|
||
| def cancel(self): | ||
| self.finished.set() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as low quality.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
复核更正:这里的 会将整数转换为字符串,不会抛出 TypeError; 可正常传给 Django 的 参数。因此原评论所述的阻塞问题不成立。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
复核更正:run() 中的 "%s" % arg 会把整数转换为字符串,不会抛出 TypeError;expire_seconds 可正常传给 Django 的 int 参数。因此原评论所述的阻塞问题不成立。