server/image_search: add migrations for elasticsearch removal

This commit is contained in:
Shyam Sunder 2020-03-07 21:39:22 -05:00
parent bd9284b7f8
commit 6cc2a91632
3 changed files with 46 additions and 6 deletions

View file

@ -8,7 +8,8 @@ import coloredlogs
import sqlalchemy as sa import sqlalchemy as sa
import sqlalchemy.orm.exc import sqlalchemy.orm.exc
from szurubooru import config, db, errors, rest from szurubooru import config, db, errors, rest
from szurubooru.func import file_uploads from szurubooru.func.posts import update_all_post_signatures
from szurubooru.func.file_uploads import purge_old_uploads
# pylint: disable=unused-import # pylint: disable=unused-import
from szurubooru import api, middleware from szurubooru import api, middleware
@ -106,10 +107,10 @@ def validate_config() -> None:
'From address must be set to use mail-based password reset') 'From address must be set to use mail-based password reset')
def purge_old_uploads() -> None: def purge_old_uploads_daemon() -> None:
while True: while True:
try: try:
file_uploads.purge_old_uploads() purge_old_uploads()
except Exception as ex: except Exception as ex:
logging.exception(ex) logging.exception(ex)
time.sleep(60 * 5) time.sleep(60 * 5)
@ -125,10 +126,14 @@ def create_app() -> Callable[[Any, Any], Any]:
if config.config['show_sql']: if config.config['show_sql']:
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
purge_thread = threading.Thread(target=purge_old_uploads) purge_thread = threading.Thread(target=purge_old_uploads_daemon)
purge_thread.daemon = True purge_thread.daemon = True
purge_thread.start() purge_thread.start()
hashing_thread = threading.Thread(target=update_all_post_signatures)
hashing_thread.daemon = False
hashing_thread.start()
db.session.commit() db.session.commit()
rest.errors.handle(errors.AuthError, _on_auth_error) rest.errors.handle(errors.AuthError, _on_auth_error)

View file

@ -513,8 +513,13 @@ def update_all_post_signatures() -> None:
.order_by(model.Post.post_id.asc()) .order_by(model.Post.post_id.asc())
.all()) .all())
for post in posts_to_hash: for post in posts_to_hash:
logger.info('Generating hash info for %d', post.post_id) try:
generate_post_signature(post, files.get(get_post_content_path(post))) generate_post_signature(
post, files.get(get_post_content_path(post)))
db.session.commit()
logger.info('Hashed Post %d', post.post_id)
except Exception as ex:
logger.exception(ex)
def update_post_content(post: model.Post, content: Optional[bytes]) -> None: def update_post_content(post: model.Post, content: Optional[bytes]) -> None:

View file

@ -0,0 +1,30 @@
'''
Generate post signature table
Revision ID: 52d6ea6584b8
Created at: 2020-03-07 17:03:40.193512
'''
import sqlalchemy as sa
from alembic import op
revision = '52d6ea6584b8'
down_revision = '3c1f0316fa7f'
branch_labels = None
depends_on = None
def upgrade():
ArrayType = sa.dialects.postgresql.ARRAY(sa.Integer, dimensions=1)
op.create_table(
'post_signature',
sa.Column('post_id', sa.Integer(), nullable=False),
sa.Column('signature', sa.LargeBinary(), nullable=False),
sa.Column('words', ArrayType, nullable=False),
sa.ForeignKeyConstraint(['post_id'], ['post.id']),
sa.PrimaryKeyConstraint('post_id'))
def downgrade():
op.drop_table('post_signature')