Skip to content

Commit ec0137d

Browse files
committed
add a hourly cron to evaluate all rules
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent d3aafbe commit ec0137d

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

dejacode/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,9 @@ def gettext_noop(s):
475475

476476
# Cron jobs (scheduler)
477477
daily_at_3am = "0 3 * * *"
478+
hourly = "0 * * * *"
478479
DEJACODE_VULNERABILITIES_CRON = env.str("DEJACODE_VULNERABILITIES_CRON", default=daily_at_3am)
480+
DEJACODE_POLICY_RULES_CRON = env.str("DEJACODE_POLICY_RULES_CRON", default=hourly)
479481

480482

481483
def enable_rq_eager_mode():

dje/cron_jobs.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from rq import cron
1212

1313
from dje.tasks import update_vulnerabilities
14+
from policy.tasks import evaluate_all_products_rules_task
1415

1516
two_hour = 7200
1617

@@ -20,3 +21,10 @@
2021
cron=settings.DEJACODE_VULNERABILITIES_CRON, # Daily at 3am by default
2122
job_timeout=two_hour,
2223
)
24+
25+
cron.register(
26+
func=evaluate_all_products_rules_task,
27+
queue_name="default",
28+
cron=settings.DEJACODE_POLICY_RULES_CRON, # Hourly by default
29+
job_timeout=two_hour,
30+
)

policy/rules.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ class PackageBaseRule(BaseRule):
3535
def count_violations(self, product, threshold, parameters):
3636
Package = apps.get_model("component_catalog", "package")
3737

38-
count = Package.objects.filter(
39-
productpackages__product=product,
40-
**self.package_filter,
41-
).distinct().count()
38+
count = (
39+
Package.objects.filter(
40+
productpackages__product=product,
41+
**self.package_filter,
42+
)
43+
.distinct()
44+
.count()
45+
)
4246

4347
return count if count > threshold else 0
4448

0 commit comments

Comments
 (0)