1
1
import logging
2
+ from typing import Optional
2
3
3
4
from flask import g
4
5
from karton .core import Config as KartonConfig
@@ -18,17 +19,26 @@ class KartonProducer(Producer):
18
19
with_service_info = True
19
20
20
21
21
- karton_producer = KartonProducer (config = KartonConfig (app_config .karton .config_path ))
22
+ if app_config .mwdb .enable_karton :
23
+ karton_producer = KartonProducer (config = KartonConfig (app_config .karton .config_path ))
24
+ else :
25
+ karton_producer = None
22
26
23
27
24
- def get_karton_producer () -> Producer :
28
+ def get_karton_producer () -> Optional [ Producer ] :
25
29
return karton_producer
26
30
27
31
28
32
def send_file_to_karton (file ) -> str :
33
+ producer = get_karton_producer ()
34
+
35
+ if producer is None :
36
+ raise RuntimeError (
37
+ "This method should not be called when Karton is not enabled"
38
+ )
39
+
29
40
file_stream = file .open ()
30
41
try :
31
- producer = get_karton_producer ()
32
42
feed_quality = g .auth_user .feed_quality
33
43
task_priority = (
34
44
TaskPriority .NORMAL if feed_quality == "high" else TaskPriority .LOW
@@ -58,6 +68,12 @@ def send_file_to_karton(file) -> str:
58
68
59
69
def send_config_to_karton (config ) -> str :
60
70
producer = get_karton_producer ()
71
+
72
+ if producer is None :
73
+ raise RuntimeError (
74
+ "This method should not be called when Karton is not enabled"
75
+ )
76
+
61
77
task = Task (
62
78
headers = {
63
79
"type" : "config" ,
@@ -79,6 +95,12 @@ def send_config_to_karton(config) -> str:
79
95
80
96
def send_blob_to_karton (blob ) -> str :
81
97
producer = get_karton_producer ()
98
+
99
+ if producer is None :
100
+ raise RuntimeError (
101
+ "This method should not be called when Karton is not enabled"
102
+ )
103
+
82
104
task = Task (
83
105
headers = {
84
106
"type" : "blob" ,
@@ -98,5 +120,12 @@ def send_blob_to_karton(blob) -> str:
98
120
99
121
100
122
def get_karton_state ():
101
- karton_state = KartonState (karton_producer .backend )
123
+ producer = get_karton_producer ()
124
+
125
+ if producer is None :
126
+ raise RuntimeError (
127
+ "This method should not be called when Karton is not enabled"
128
+ )
129
+
130
+ karton_state = KartonState (producer .backend )
102
131
return karton_state
0 commit comments