-
Notifications
You must be signed in to change notification settings - Fork 376
/
Copy pathconfiguration.py
273 lines (238 loc) · 9.32 KB
/
configuration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from __future__ import annotations
from datetime import timedelta
from typing import TYPE_CHECKING, Annotated, Literal
from pydantic import AliasChoices, BeforeValidator, Field
from pydantic_settings import BaseSettings, SettingsConfigDict
from crawlee._utils.docs import docs_group
from crawlee._utils.models import timedelta_ms
if TYPE_CHECKING:
from typing_extensions import Self
__all__ = ['Configuration']
@docs_group('Data structures')
class Configuration(BaseSettings):
"""Configuration settings for the Crawlee project.
This class stores common configurable parameters for Crawlee. Default values are provided for all settings,
so typically, no adjustments are necessary. However, you may modify settings for specific use cases,
such as changing the default storage directory, the default storage IDs, the timeout for internal
operations, and more.
Settings can also be configured via environment variables, prefixed with `CRAWLEE_`.
"""
model_config = SettingsConfigDict(populate_by_name=True)
internal_timeout: Annotated[timedelta | None, Field(alias='crawlee_internal_timeout')] = None
"""Timeout for the internal asynchronous operations."""
default_browser_path: Annotated[
str | None,
Field(
validation_alias=AliasChoices(
'apify_default_browser_path',
'crawlee_default_browser_path',
)
),
] = None
"""Specifies the path to the browser executable. Currently primarily for Playwright-based features. This option
is passed directly to Playwright's `browser_type.launch` method as `executable_path` argument. For more details,
refer to the Playwright documentation:
https://playwright.dev/docs/api/class-browsertype#browser-type-launch.
"""
disable_browser_sandbox: Annotated[
bool,
Field(
validation_alias=AliasChoices(
'apify_disable_browser_sandbox',
'crawlee_disable_browser_sandbox',
)
),
] = False
"""Disables the sandbox for the browser. Currently primarily for Playwright-based features. This option
is passed directly to Playwright's `browser_type.launch` method as `chromium_sandbox`. For more details,
refer to the Playwright documentation:
https://playwright.dev/docs/api/class-browsertype#browser-type-launch."""
log_level: Annotated[
Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
Field(
validation_alias=AliasChoices(
'apify_log_level',
'crawlee_log_level',
)
),
BeforeValidator(lambda value: str(value).upper()),
] = 'INFO'
"""The logging level."""
default_dataset_id: Annotated[
str,
Field(
validation_alias=AliasChoices(
'actor_default_dataset_id',
'apify_default_dataset_id',
'crawlee_default_dataset_id',
)
),
] = 'default'
"""The default `Dataset` ID. This option is utilized by the storage client."""
default_key_value_store_id: Annotated[
str,
Field(
validation_alias=AliasChoices(
'actor_default_key_value_store_id',
'apify_default_key_value_store_id',
'crawlee_default_key_value_store_id',
)
),
] = 'default'
"""The default `KeyValueStore` ID. This option is utilized by the storage client."""
default_request_queue_id: Annotated[
str,
Field(
validation_alias=AliasChoices(
'actor_default_request_queue_id',
'apify_default_request_queue_id',
'crawlee_default_request_queue_id',
)
),
] = 'default'
"""The default `RequestQueue` ID. This option is utilized by the storage client."""
purge_on_start: Annotated[
bool,
Field(
validation_alias=AliasChoices(
'apify_purge_on_start',
'crawlee_purge_on_start',
)
),
] = True
"""Whether to purge the storage on the start. This option is utilized by the `MemoryStorageClient`."""
write_metadata: Annotated[bool, Field(alias='crawlee_write_metadata')] = True
"""Whether to write the storage metadata. This option is utilized by the `MemoryStorageClient`."""
persist_storage: Annotated[
bool,
Field(
validation_alias=AliasChoices(
'apify_persist_storage',
'crawlee_persist_storage',
)
),
] = True
"""Whether to persist the storage. This option is utilized by the `MemoryStorageClient`."""
persist_state_interval: Annotated[
timedelta_ms,
Field(
validation_alias=AliasChoices(
'apify_persist_state_interval_millis',
'crawlee_persist_state_interval_millis',
)
),
] = timedelta(minutes=1)
"""Interval at which `PersistState` events are emitted. The event ensures the state persistence during
the crawler run. This option is utilized by the `EventManager`."""
system_info_interval: Annotated[
timedelta_ms,
Field(
validation_alias=AliasChoices(
'apify_system_info_interval_millis',
'crawlee_system_info_interval_millis',
)
),
] = timedelta(seconds=1)
"""Interval at which `SystemInfo` events are emitted. The event represents the current status of the system.
This option is utilized by the `LocalEventManager`."""
max_used_cpu_ratio: Annotated[
float,
Field(
validation_alias=AliasChoices(
'apify_max_used_cpu_ratio',
'crawlee_max_used_cpu_ratio',
)
),
] = 0.95
"""The maximum CPU usage ratio. If the CPU usage exceeds this value, the system is considered overloaded.
This option is used by the `Snapshotter`."""
max_used_memory_ratio: Annotated[
float,
Field(
validation_alias=AliasChoices(
'apify_max_used_memory_ratio',
'crawlee_max_used_memory_ratio',
)
),
] = 0.9
"""The maximum memory usage ratio. If the memory usage exceeds this ratio, it is considered overloaded.
This option is used by the `Snapshotter`."""
max_event_loop_delay: Annotated[
timedelta_ms,
Field(
validation_alias=AliasChoices(
'apify_max_event_loop_delay_millis',
'crawlee_max_event_loop_delay_millis',
)
),
] = timedelta(milliseconds=50)
"""The maximum event loop delay. If the event loop delay exceeds this value, it is considered overloaded.
This option is used by the `Snapshotter`."""
max_client_errors: Annotated[
int,
Field(
validation_alias=AliasChoices(
'apify_max_client_errors',
'crawlee_max_client_errors',
)
),
] = 1
"""The maximum number of client errors (HTTP 429) allowed before the system is considered overloaded.
This option is used by the `Snapshotter`."""
memory_mbytes: Annotated[
int | None,
Field(
validation_alias=AliasChoices(
'actor_memory_mbytes',
'apify_memory_mbytes',
'crawlee_memory_mbytes',
)
),
] = None
"""The maximum used memory in megabytes. This option is utilized by the `Snapshotter`."""
available_memory_ratio: Annotated[
float,
Field(
validation_alias=AliasChoices(
'apify_available_memory_ratio',
'crawlee_available_memory_ratio',
)
),
] = 0.25
"""The maximum proportion of system memory to use. If `memory_mbytes` is not provided, this ratio is used to
calculate the maximum memory. This option is utilized by the `Snapshotter`."""
storage_dir: Annotated[
str,
Field(
validation_alias=AliasChoices(
'apify_local_storage_dir',
'crawlee_storage_dir',
),
),
] = './storage'
"""The path to the storage directory. This option is utilized by the `MemoryStorageClient`."""
headless: Annotated[
bool,
Field(
validation_alias=AliasChoices(
'apify_headless',
'crawlee_headless',
)
),
] = True
"""Whether to run the browser in headless mode. Currently primarily for Playwright-based features. This option
is passed directly to Playwright's `browser_type.launch` method as `headless`. For more details,
refer to the Playwright documentation:
https://playwright.dev/docs/api/class-browsertype#browser-type-launch.
"""
@classmethod
def get_global_configuration(cls) -> Self:
"""Retrieve the global instance of the configuration.
Mostly for the backwards compatibility. It is recommended to use the `service_locator.get_configuration()`
instead.
"""
from crawlee import service_locator
config = service_locator.get_configuration()
if not isinstance(config, cls):
raise TypeError(f'Requested global configuration object of type {cls}, but {config.__class__} was found')
return config