Skip to content

Commit fc068d1

Browse files
committed
celery and task updates
1 parent a01261f commit fc068d1

File tree

11 files changed

+59
-64
lines changed

11 files changed

+59
-64
lines changed

errata/tasks.py

-11
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,11 @@
1414
# You should have received a copy of the GNU General Public License
1515
# along with Patchman. If not, see <http://www.gnu.org/licenses/>
1616

17-
from datetime import timedelta
18-
19-
from django.conf import settings
20-
2117
from celery import shared_task
22-
from patchman.celery import app
2318

2419
from errata.models import Erratum
2520
from security.tasks import update_cves, update_cwes
2621

27-
app.conf.beat_schedule = {
28-
'update-errata-cves-cwes-every-6-hours': {
29-
'task': 'tasks.update_errata',
30-
'schedule': timedelta(hours=6),
31-
},
32-
}
3322

3423
@shared_task
3524
def update_erratum(erratum):

etc/patchman/local_settings.py

+30-6
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,43 @@
2929
ALLOWED_HOSTS = ['127.0.0.1', '*']
3030

3131
# Maximum number of mirrors to add or refresh per repo
32-
MAX_MIRRORS = 5
32+
MAX_MIRRORS = 3
3333

34-
# Number of days to wait before notifying users that a host has not reported
34+
# Number of days to wait before raising that a host has not reported
3535
DAYS_WITHOUT_REPORT = 14
3636

3737
# Whether to run patchman under the gunicorn web server
3838
RUN_GUNICORN = False
3939

40-
# Enable redis caching for 30 seconds
4140
CACHES = {
4241
'default': {
43-
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
44-
'LOCATION': 'redis://127.0.0.1:6379',
45-
'TIMEOUT': 30,
42+
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
4643
}
4744
}
45+
46+
# Uncomment to enable redis caching for e.g. 30 seconds
47+
# Note that the UI results may be out of date for this amount of time
48+
# CACHES = {
49+
# 'default': {
50+
# 'BACKEND': 'django.core.cache.backends.redis.RedisCache',
51+
# 'LOCATION': 'redis://127.0.0.1:6379',
52+
# 'TIMEOUT': 30,
53+
# }
54+
# }
55+
56+
from datetime import timedelta # noqa
57+
from celery.schedules import crontab # noqa
58+
CELERY_BEAT_SCHEDULE = {
59+
'process_all_unprocessed_reports': {
60+
'task': 'reports.tasks.process_reports',
61+
'schedule': crontab(minute='*/5'),
62+
},
63+
'refresh_repos_daily': {
64+
'task': 'tasks.refresh_repos',
65+
'schedule': crontab(hour=6, minute=00),
66+
},
67+
'update_errata_cves_cwes_every_12_hours': {
68+
'task': 'tasks.update_errata',
69+
'schedule': timedelta(hours=12),
70+
},
71+
}

patchman/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2013-2021 Marcus Furlong <furlongm@gmail.com>
1+
# Copyright 2013-2025 Marcus Furlong <furlongm@gmail.com>
22
#
33
# This file is part of Patchman.
44
#

patchman/settings.py

-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
import site
55
import sys
66

7-
#import django
8-
#from django.utils.encoding import smart_str
9-
#django.utils.encoding.smart_text = smart_str
10-
117
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
128
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
139

reports/tasks.py

+6-14
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,16 @@
1515
# You should have received a copy of the GNU General Public License
1616
# along with Patchman. If not, see <http://www.gnu.org/licenses/>
1717

18-
from django.conf import settings
18+
from celery import shared_task
1919

20-
from reports.models import Report
20+
from django.db.utils import OperationalError
2121

22-
from celery import shared_task
23-
from celery.schedules import crontab
24-
from patchman.celery import app
22+
from reports.models import Report
2523

26-
app.conf.beat_schedule = {
27-
'process-reports': {
28-
'task': 'reports.tasks.process_reports',
29-
'schedule': crontab(minute='*/5'),
30-
},
31-
}
3224

33-
@shared_task
34-
def process_report(report_id):
35-
report = Report.objects.get(report_id)
25+
@shared_task(bind=True, autoretry_for=(OperationalError,), retry_backoff=True, retry_kwargs={'max_retries': 5})
26+
def process_report(self, report_id):
27+
report = Report.objects.get(id=report_id)
3628
report.process()
3729

3830

reports/urls.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626
path('upload/', views.upload),
2727
path('<int:report_id>/', views.report_detail, name='report_detail'),
2828
path('<int:report_id>/delete/', views.report_delete, name='report_delete'),
29-
path('<int:report_id>/process/', views.report_process, name='report_process'), # noqa
29+
path('<int:report_id>/process/', views.report_process, name='report_process'),
3030
]

repos/models.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
from packages.models import Package
2424
from util import has_setting_of_type
2525

26-
from repos.utils import refresh_deb_repo, refresh_rpm_repo, \
27-
refresh_arch_repo, refresh_gentoo_repo, update_mirror_packages
26+
from repos.utils import refresh_deb_repo, refresh_rpm_repo, refresh_arch_repo, refresh_gentoo_repo, \
27+
update_mirror_packages
2828
from patchman.signals import info_message, warning_message, error_message
2929

3030

@@ -96,8 +96,7 @@ def refresh(self, force=False):
9696
elif self.repotype == Repository.GENTOO:
9797
refresh_gentoo_repo(self)
9898
else:
99-
text = 'Error: unknown repo type for repo '
100-
text += f'{self.id}: {self.repotype}'
99+
text = 'Error: unknown repo type for repo {self.id}: {self.repotype}'
101100
error_message.send(sender=None, text=text)
102101
else:
103102
text = 'Repo requires certificate authentication, not updating'

repos/tasks.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,16 @@
1414
# You should have received a copy of the GNU General Public License
1515
# along with Patchman. If not, see <http://www.gnu.org/licenses/>
1616

17-
from django.conf import settings
18-
19-
from repos.models import Repository, Mirror
20-
2117
from celery import shared_task
22-
from celery.schedules import crontab
23-
from patchman.celery import app
2418

25-
app.conf.beat_schedule = {
26-
'refresh-repos-every-day': {
27-
'task': 'tasks.refresh_repos',
28-
'schedule': crontab(hour=6, minute=00),
29-
},
30-
}
19+
from repos.models import Repository
20+
3121

3222
@shared_task
33-
def refresh_repo(force=False):
23+
def refresh_repo(repo_id, force=False):
3424
""" Refresh metadata for a single repo
3525
"""
26+
repo = Repository.objects.get(id=repo_id)
3627
repo.refresh(force)
3728

3829

repos/urls.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
urlpatterns = [
2525
path('', views.repo_list, name='repo_list'),
2626
path('<int:repo_id>/', views.repo_detail, name='repo_detail'),
27-
path('<int:repo_id>/toggle_enabled/', views.repo_toggle_enabled, name='repo_toggle_enabled'), # noqa
28-
path('<int:repo_id>/toggle_security/', views.repo_toggle_security, name='repo_toggle_security'), # noqa
27+
path('<int:repo_id>/toggle_enabled/', views.repo_toggle_enabled, name='repo_toggle_enabled'),
28+
path('<int:repo_id>/toggle_security/', views.repo_toggle_security, name='repo_toggle_security'),
2929
path('<int:repo_id>/edit/', views.repo_edit, name='repo_edit'),
3030
path('<int:repo_id>/delete/', views.repo_delete, name='repo_delete'),
31+
path('<int:repo_id>/refresh/', views.repo_refresh, name='repo_refresh'),
3132
path('mirrors/', views.mirror_list, name='mirror_list'),
32-
path('mirrors/mirror/<int:mirror_id>/', views.mirror_detail, name='mirror_detail'), # noqa
33-
path('mirrors/mirror/<int:mirror_id>/edit/', views.mirror_edit, name='mirror_edit'), # noqa
34-
path('mirrors/mirror/<int:mirror_id>/delete/', views.mirror_delete, name='mirror_delete'), # noqa
33+
path('mirrors/mirror/<int:mirror_id>/', views.mirror_detail, name='mirror_detail'),
34+
path('mirrors/mirror/<int:mirror_id>/edit/', views.mirror_edit, name='mirror_edit'),
35+
path('mirrors/mirror/<int:mirror_id>/delete/', views.mirror_delete, name='mirror_delete'),
3536
]

repos/utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
info_message, warning_message, error_message, debug_message
4646

4747

48-
def get_or_create_repo(r_name, r_arch, r_type):
48+
def get_or_create_repo(r_name, r_arch, r_type, r_id=None):
4949
""" Get or create a Repository object. Returns the object. Returns None if
5050
it cannot get or create the object.
5151
"""
@@ -64,6 +64,9 @@ def get_or_create_repo(r_name, r_arch, r_type):
6464
except DatabaseError as e:
6565
error_message.send(sender=None, text=e)
6666
if repository:
67+
if r_id:
68+
repository.repo_id = r_id
69+
repository.save()
6770
return repository
6871

6972

security/tasks.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,33 @@
1414
# You should have received a copy of the GNU General Public License
1515
# along with Patchman. If not, see <http://www.gnu.org/licenses/>
1616

17-
from django.conf import settings
17+
from celery import shared_task
1818

1919
from security.models import CVE, CWE
2020

21-
from celery import shared_task
22-
from patchman.celery import app
23-
2421

2522
@shared_task
2623
def update_cve(cve):
2724
""" Task to update a CVE
2825
"""
2926
cve.update()
3027

28+
3129
@shared_task
3230
def update_cves():
3331
""" Task to update all CVEs
3432
"""
3533
for cve in CVE.objects.all():
3634
update_cve.delay(cve)
3735

36+
3837
@shared_task
3938
def update_cwe(cwe):
4039
""" Task to update a CWE
4140
"""
4241
cwe.update()
4342

43+
4444
@shared_task
4545
def update_cwes():
4646
""" Task to update all CWEa

0 commit comments

Comments
 (0)