Projet

Général

Profil

0003-tokens-add-job-to-clean-expired-tokens-60665.patch

Frédéric Péters, 18 janvier 2022 17:51

Télécharger (4,89 ko)

Voir les différences:

Subject: [PATCH 3/5] tokens: add job to clean expired tokens (#60665)

 tests/test_token.py     | 13 +++++++++++++
 wcs/qommon/publisher.py |  6 ++++++
 wcs/qommon/tokens.py    | 20 +++++++++++++++++++-
 wcs/sql.py              | 11 ++++++++---
 4 files changed, 46 insertions(+), 4 deletions(-)
tests/test_token.py
62 62
    token.store()
63 63
    with pytest.raises(KeyError):
64 64
        assert get_publisher().token_class().get(token.id)
65

  
66

  
67
def test_clean_job(two_pubs):
68
    get_publisher().token_class.wipe()
69
    token = get_publisher().token_class()
70
    token.store()
71
    token = get_publisher().token_class()
72
    token.store()
73
    token = get_publisher().token_class(expiration_delay=-7200)  # already expired
74
    token.store()
75
    assert get_publisher().token_class.count() == 3
76
    get_publisher().clean_tokens()
77
    assert get_publisher().token_class.count() == 2
wcs/qommon/publisher.py
631 631
                # started more than two days ago, probably aborted job
632 632
                job.remove_self()
633 633

  
634
    def clean_tokens(self, **kwargs):
635
        token_class = getattr(self, 'token_class', None)
636
        if token_class:
637
            token_class.clean()
638

  
634 639
    def _clean_files(self, limit, dirname):
635 640
        if not os.path.exists(dirname):
636 641
            return
......
669 674
        cls.register_cronjob(CronJob(cls.clean_sessions, minutes=range(0, 60, 5), name='clean_sessions'))
670 675
        cls.register_cronjob(CronJob(cls.clean_nonces, minutes=range(0, 60, 5), name='clean_nonces'))
671 676
        cls.register_cronjob(CronJob(cls.clean_afterjobs, minutes=[0], name='clean_afterjobs'))
677
        cls.register_cronjob(CronJob(cls.clean_tokens, minutes=[0], name='clean_tokens'))
672 678
        cls.register_cronjob(CronJob(cls.clean_tempfiles, minutes=[0], name='clean_tempfiles'))
673 679
        cls.register_cronjob(CronJob(cls.clean_thumbnails, minutes=[0], name='clean_thumbnails'))
674 680
        cls.register_cronjob(CronJob(cls.clean_loggederrors, hours=[3], name='clean_loggederrors'))
wcs/qommon/tokens.py
20 20

  
21 21
from django.utils.timezone import make_aware, now
22 22

  
23
from .storage import StorableObject
23
from quixote import get_publisher
24

  
25
from .storage import Less, StorableObject
24 26

  
25 27

  
26 28
class Token(StorableObject):
......
56 58
        if self.expiration and self.expiration < now():
57 59
            self.remove_self()
58 60
            raise KeyError()
61

  
62
    @classmethod
63
    def clean(cls):
64
        if get_publisher().is_using_postgresql():
65
            # noqa pylint: disable=unexpected-keyword-arg
66
            cls.wipe(clause=[Less('expiration', now())])
67
        else:
68
            for token_id in cls.keys():
69
                try:
70
                    cls.get(token_id)  # will run migrate, will check expiration
71
                except KeyError:
72
                    pass
73
                except AttributeError:
74
                    # old python2 tokens:
75
                    # AttributeError: module 'builtins' has no attribute 'unicode'
76
                    cls.remove_object(token_id)
wcs/sql.py
120 120
        return '%s %s %%(c%s)s' % (attribute, self.sql_op, id(self.value))
121 121

  
122 122
    def as_sql_param(self):
123
        if isinstance(self.value, datetime.date):
123
        if isinstance(self.value, datetime.date) and not isinstance(self.value, datetime.datetime):
124 124
            value = self.value.strftime('%Y-%m-%d')
125 125
        elif isinstance(self.value, time.struct_time):
126 126
            value = datetime.datetime.fromtimestamp(time.mktime(self.value))
......
1969 1969

  
1970 1970
    @classmethod
1971 1971
    @guard_postgres
1972
    def wipe(cls):
1972
    def wipe(cls, drop=False, clause=None):
1973 1973
        conn, cur = get_connection_and_cursor()
1974 1974
        sql_statement = '''DELETE FROM %s''' % cls._table_name
1975
        cur.execute(sql_statement, {'id': str(id)})
1975
        parameters = {}
1976
        if clause:
1977
            where_clauses, parameters, dummy = parse_clause(clause)
1978
            if where_clauses:
1979
                sql_statement += ' WHERE ' + ' AND '.join(where_clauses)
1980
        cur.execute(sql_statement, parameters)
1976 1981
        conn.commit()
1977 1982
        cur.close()
1978 1983

  
1979
-