@crawlee/core 4.0.0-beta.15 → 4.0.0-beta.150

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (287) hide show
  1. package/README.md +14 -14
  2. package/autoscaling/autoscaled_pool.d.ts +73 -177
  3. package/autoscaling/autoscaled_pool.js +182 -329
  4. package/autoscaling/concurrency_system.d.ts +268 -0
  5. package/autoscaling/concurrency_system.js +364 -0
  6. package/autoscaling/cpu_load_signal.d.ts +43 -0
  7. package/autoscaling/cpu_load_signal.js +47 -0
  8. package/autoscaling/event_loop_load_signal.d.ts +51 -0
  9. package/autoscaling/event_loop_load_signal.js +60 -0
  10. package/autoscaling/index.d.ts +6 -1
  11. package/autoscaling/index.js +6 -1
  12. package/autoscaling/load_signal.d.ts +98 -0
  13. package/autoscaling/load_signal.js +103 -0
  14. package/autoscaling/memory_load_signal.d.ts +47 -0
  15. package/autoscaling/memory_load_signal.js +106 -0
  16. package/autoscaling/snapshotter.d.ts +58 -163
  17. package/autoscaling/snapshotter.js +45 -263
  18. package/autoscaling/storage_backend_load_signal.d.ts +56 -0
  19. package/autoscaling/storage_backend_load_signal.js +73 -0
  20. package/autoscaling/system_status.d.ts +67 -89
  21. package/autoscaling/system_status.js +92 -122
  22. package/autoscaling/weighted_avg.d.ts +5 -0
  23. package/autoscaling/weighted_avg.js +14 -0
  24. package/byte_utils.d.ts +17 -0
  25. package/byte_utils.js +42 -0
  26. package/configuration.d.ts +96 -223
  27. package/configuration.js +170 -222
  28. package/cookie_utils.d.ts +3 -2
  29. package/cookie_utils.js +18 -7
  30. package/crawlers/context_pipeline.d.ts +10 -1
  31. package/crawlers/context_pipeline.js +31 -8
  32. package/crawlers/crawler_commons.d.ts +90 -126
  33. package/crawlers/crawler_commons.js +1 -108
  34. package/crawlers/error_snapshotter.d.ts +2 -5
  35. package/crawlers/error_snapshotter.js +7 -8
  36. package/crawlers/error_tracker.d.ts +0 -1
  37. package/crawlers/error_tracker.js +0 -1
  38. package/crawlers/index.d.ts +1 -3
  39. package/crawlers/index.js +0 -3
  40. package/crawlers/internals/types.d.ts +0 -1
  41. package/crawlers/internals/types.js +0 -1
  42. package/crawlers/statistics.d.ts +187 -64
  43. package/crawlers/statistics.js +354 -165
  44. package/debug.d.ts +36 -0
  45. package/debug.js +70 -0
  46. package/enqueue_links/enqueue_links.d.ts +61 -154
  47. package/enqueue_links/enqueue_links.js +40 -232
  48. package/enqueue_links/index.d.ts +0 -1
  49. package/enqueue_links/index.js +0 -1
  50. package/enqueue_links/shared.d.ts +55 -28
  51. package/enqueue_links/shared.js +116 -69
  52. package/errors.d.ts +72 -4
  53. package/errors.js +89 -5
  54. package/events/event_manager.d.ts +35 -9
  55. package/events/event_manager.js +12 -13
  56. package/events/index.d.ts +0 -1
  57. package/events/index.js +0 -1
  58. package/events/local_event_manager.d.ts +15 -3
  59. package/events/local_event_manager.js +39 -13
  60. package/http.d.ts +9 -0
  61. package/http.js +28 -0
  62. package/index.d.ts +7 -5
  63. package/index.js +8 -4
  64. package/iterables.d.ts +79 -0
  65. package/iterables.js +134 -0
  66. package/log.d.ts +77 -1
  67. package/log.js +104 -1
  68. package/memory-storage/consts.d.ts +4 -0
  69. package/memory-storage/consts.js +4 -0
  70. package/memory-storage/index.d.ts +1 -0
  71. package/memory-storage/index.js +1 -0
  72. package/memory-storage/memory-storage.d.ts +44 -0
  73. package/memory-storage/memory-storage.js +160 -0
  74. package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
  75. package/memory-storage/resource-clients/common/base-client.js +6 -0
  76. package/memory-storage/resource-clients/dataset.d.ts +40 -0
  77. package/memory-storage/resource-clients/dataset.js +106 -0
  78. package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
  79. package/memory-storage/resource-clients/key-value-store.js +199 -0
  80. package/memory-storage/resource-clients/request-queue.d.ts +77 -0
  81. package/memory-storage/resource-clients/request-queue.js +407 -0
  82. package/memory-storage/utils.d.ts +16 -0
  83. package/memory-storage/utils.js +41 -0
  84. package/owned_or_injected.d.ts +58 -0
  85. package/owned_or_injected.js +98 -0
  86. package/package.json +13 -12
  87. package/proxy_configuration.d.ts +22 -128
  88. package/proxy_configuration.js +32 -144
  89. package/recoverable_state.d.ts +83 -51
  90. package/recoverable_state.js +163 -72
  91. package/request.d.ts +57 -16
  92. package/request.js +130 -69
  93. package/router.d.ts +193 -21
  94. package/router.js +188 -43
  95. package/serialization.d.ts +0 -1
  96. package/serialization.js +15 -15
  97. package/service_locator.d.ts +165 -0
  98. package/service_locator.js +253 -0
  99. package/session_pool/consts.d.ts +1 -2
  100. package/session_pool/consts.js +1 -2
  101. package/session_pool/errors.d.ts +0 -1
  102. package/session_pool/errors.js +0 -1
  103. package/session_pool/fingerprint.d.ts +9 -0
  104. package/session_pool/fingerprint.js +30 -0
  105. package/session_pool/index.d.ts +0 -2
  106. package/session_pool/index.js +0 -2
  107. package/session_pool/session.d.ts +39 -89
  108. package/session_pool/session.js +102 -159
  109. package/session_pool/session_pool.d.ts +67 -91
  110. package/session_pool/session_pool.js +196 -187
  111. package/storages/batched_adds.d.ts +37 -0
  112. package/storages/batched_adds.js +73 -0
  113. package/storages/dataset.d.ts +109 -56
  114. package/storages/dataset.js +284 -149
  115. package/storages/index.d.ts +9 -9
  116. package/storages/index.js +7 -9
  117. package/storages/key_value_store.d.ts +183 -48
  118. package/storages/key_value_store.js +444 -171
  119. package/storages/key_value_store_codec.d.ts +32 -0
  120. package/storages/key_value_store_codec.js +113 -0
  121. package/storages/request_dedup_cache.d.ts +22 -0
  122. package/storages/request_dedup_cache.js +48 -0
  123. package/storages/request_list.d.ts +53 -115
  124. package/storages/request_list.js +194 -167
  125. package/storages/request_loader.d.ts +133 -0
  126. package/storages/request_loader.js +36 -0
  127. package/storages/request_manager.d.ts +109 -0
  128. package/storages/request_manager.js +1 -0
  129. package/storages/request_manager_tandem.d.ts +55 -23
  130. package/storages/request_manager_tandem.js +139 -57
  131. package/storages/request_queue.d.ts +286 -48
  132. package/storages/request_queue.js +757 -218
  133. package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +43 -91
  134. package/storages/sitemap_request_loader.js +439 -0
  135. package/storages/storage_instance_manager.d.ts +87 -0
  136. package/storages/storage_instance_manager.js +256 -0
  137. package/storages/storage_stats.d.ts +48 -0
  138. package/storages/storage_stats.js +29 -0
  139. package/storages/throttling_request_manager.d.ts +222 -0
  140. package/storages/throttling_request_manager.js +780 -0
  141. package/storages/transaction.d.ts +252 -0
  142. package/storages/transaction.js +251 -0
  143. package/storages/utils.d.ts +59 -11
  144. package/storages/utils.js +75 -15
  145. package/system-info/cpu-info.d.ts +67 -0
  146. package/system-info/cpu-info.js +216 -0
  147. package/system-info/memory-info.d.ts +31 -0
  148. package/system-info/memory-info.js +115 -0
  149. package/system-info/ps-tree.d.ts +17 -0
  150. package/system-info/ps-tree.js +144 -0
  151. package/system-info/runtime.d.ts +14 -0
  152. package/system-info/runtime.js +80 -0
  153. package/typedefs.d.ts +0 -6
  154. package/typedefs.js +0 -1
  155. package/url.d.ts +9 -0
  156. package/url.js +11 -0
  157. package/validators.d.ts +22 -18
  158. package/validators.js +13 -18
  159. package/autoscaling/autoscaled_pool.d.ts.map +0 -1
  160. package/autoscaling/autoscaled_pool.js.map +0 -1
  161. package/autoscaling/index.d.ts.map +0 -1
  162. package/autoscaling/index.js.map +0 -1
  163. package/autoscaling/snapshotter.d.ts.map +0 -1
  164. package/autoscaling/snapshotter.js.map +0 -1
  165. package/autoscaling/system_status.d.ts.map +0 -1
  166. package/autoscaling/system_status.js.map +0 -1
  167. package/configuration.d.ts.map +0 -1
  168. package/configuration.js.map +0 -1
  169. package/cookie_utils.d.ts.map +0 -1
  170. package/cookie_utils.js.map +0 -1
  171. package/crawlers/context_pipeline.d.ts.map +0 -1
  172. package/crawlers/context_pipeline.js.map +0 -1
  173. package/crawlers/crawler_commons.d.ts.map +0 -1
  174. package/crawlers/crawler_commons.js.map +0 -1
  175. package/crawlers/crawler_utils.d.ts +0 -10
  176. package/crawlers/crawler_utils.d.ts.map +0 -1
  177. package/crawlers/crawler_utils.js +0 -12
  178. package/crawlers/crawler_utils.js.map +0 -1
  179. package/crawlers/error_snapshotter.d.ts.map +0 -1
  180. package/crawlers/error_snapshotter.js.map +0 -1
  181. package/crawlers/error_tracker.d.ts.map +0 -1
  182. package/crawlers/error_tracker.js.map +0 -1
  183. package/crawlers/index.d.ts.map +0 -1
  184. package/crawlers/index.js.map +0 -1
  185. package/crawlers/internals/types.d.ts.map +0 -1
  186. package/crawlers/internals/types.js.map +0 -1
  187. package/crawlers/statistics.d.ts.map +0 -1
  188. package/crawlers/statistics.js.map +0 -1
  189. package/enqueue_links/enqueue_links.d.ts.map +0 -1
  190. package/enqueue_links/enqueue_links.js.map +0 -1
  191. package/enqueue_links/index.d.ts.map +0 -1
  192. package/enqueue_links/index.js.map +0 -1
  193. package/enqueue_links/shared.d.ts.map +0 -1
  194. package/enqueue_links/shared.js.map +0 -1
  195. package/errors.d.ts.map +0 -1
  196. package/errors.js.map +0 -1
  197. package/events/event_manager.d.ts.map +0 -1
  198. package/events/event_manager.js.map +0 -1
  199. package/events/index.d.ts.map +0 -1
  200. package/events/index.js.map +0 -1
  201. package/events/local_event_manager.d.ts.map +0 -1
  202. package/events/local_event_manager.js.map +0 -1
  203. package/http_clients/base-http-client.d.ts +0 -140
  204. package/http_clients/base-http-client.d.ts.map +0 -1
  205. package/http_clients/base-http-client.js +0 -40
  206. package/http_clients/base-http-client.js.map +0 -1
  207. package/http_clients/form-data-like.d.ts +0 -67
  208. package/http_clients/form-data-like.d.ts.map +0 -1
  209. package/http_clients/form-data-like.js +0 -5
  210. package/http_clients/form-data-like.js.map +0 -1
  211. package/http_clients/got-scraping-http-client.d.ts +0 -20
  212. package/http_clients/got-scraping-http-client.d.ts.map +0 -1
  213. package/http_clients/got-scraping-http-client.js +0 -85
  214. package/http_clients/got-scraping-http-client.js.map +0 -1
  215. package/http_clients/index.d.ts +0 -3
  216. package/http_clients/index.d.ts.map +0 -1
  217. package/http_clients/index.js +0 -3
  218. package/http_clients/index.js.map +0 -1
  219. package/index.d.ts.map +0 -1
  220. package/index.js.map +0 -1
  221. package/log.d.ts.map +0 -1
  222. package/log.js.map +0 -1
  223. package/proxy_configuration.d.ts.map +0 -1
  224. package/proxy_configuration.js.map +0 -1
  225. package/recoverable_state.d.ts.map +0 -1
  226. package/recoverable_state.js.map +0 -1
  227. package/request.d.ts.map +0 -1
  228. package/request.js.map +0 -1
  229. package/router.d.ts.map +0 -1
  230. package/router.js.map +0 -1
  231. package/serialization.d.ts.map +0 -1
  232. package/serialization.js.map +0 -1
  233. package/session_pool/consts.d.ts.map +0 -1
  234. package/session_pool/consts.js.map +0 -1
  235. package/session_pool/errors.d.ts.map +0 -1
  236. package/session_pool/errors.js.map +0 -1
  237. package/session_pool/events.d.ts +0 -3
  238. package/session_pool/events.d.ts.map +0 -1
  239. package/session_pool/events.js +0 -3
  240. package/session_pool/events.js.map +0 -1
  241. package/session_pool/index.d.ts.map +0 -1
  242. package/session_pool/index.js.map +0 -1
  243. package/session_pool/session.d.ts.map +0 -1
  244. package/session_pool/session.js.map +0 -1
  245. package/session_pool/session_pool.d.ts.map +0 -1
  246. package/session_pool/session_pool.js.map +0 -1
  247. package/storages/access_checking.d.ts +0 -13
  248. package/storages/access_checking.d.ts.map +0 -1
  249. package/storages/access_checking.js +0 -14
  250. package/storages/access_checking.js.map +0 -1
  251. package/storages/dataset.d.ts.map +0 -1
  252. package/storages/dataset.js.map +0 -1
  253. package/storages/index.d.ts.map +0 -1
  254. package/storages/index.js.map +0 -1
  255. package/storages/key_value_store.d.ts.map +0 -1
  256. package/storages/key_value_store.js.map +0 -1
  257. package/storages/request_list.d.ts.map +0 -1
  258. package/storages/request_list.js.map +0 -1
  259. package/storages/request_list_adapter.d.ts +0 -58
  260. package/storages/request_list_adapter.d.ts.map +0 -1
  261. package/storages/request_list_adapter.js +0 -81
  262. package/storages/request_list_adapter.js.map +0 -1
  263. package/storages/request_manager_tandem.d.ts.map +0 -1
  264. package/storages/request_manager_tandem.js.map +0 -1
  265. package/storages/request_provider.d.ts +0 -371
  266. package/storages/request_provider.d.ts.map +0 -1
  267. package/storages/request_provider.js +0 -585
  268. package/storages/request_provider.js.map +0 -1
  269. package/storages/request_queue.d.ts.map +0 -1
  270. package/storages/request_queue.js.map +0 -1
  271. package/storages/request_queue_v2.d.ts +0 -87
  272. package/storages/request_queue_v2.d.ts.map +0 -1
  273. package/storages/request_queue_v2.js +0 -438
  274. package/storages/request_queue_v2.js.map +0 -1
  275. package/storages/sitemap_request_list.d.ts.map +0 -1
  276. package/storages/sitemap_request_list.js +0 -430
  277. package/storages/sitemap_request_list.js.map +0 -1
  278. package/storages/storage_manager.d.ts +0 -58
  279. package/storages/storage_manager.d.ts.map +0 -1
  280. package/storages/storage_manager.js +0 -105
  281. package/storages/storage_manager.js.map +0 -1
  282. package/storages/utils.d.ts.map +0 -1
  283. package/storages/utils.js.map +0 -1
  284. package/typedefs.d.ts.map +0 -1
  285. package/typedefs.js.map +0 -1
  286. package/validators.d.ts.map +0 -1
  287. package/validators.js.map +0 -1
package/README.md CHANGED
@@ -1,23 +1,23 @@
1
1
  <h1 align="center">
2
2
  <a href="https://crawlee.dev">
3
3
  <picture>
4
- <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-dark.svg?sanitize=true">
5
- <img alt="Crawlee" src="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-light.svg?sanitize=true" width="500">
4
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-dark.svg?sanitize=true" />
5
+ <img alt="Crawlee" src="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-light.svg?sanitize=true" width="500" />
6
6
  </picture>
7
7
  </a>
8
- <br>
8
+ <br />
9
9
  <small>A web scraping and browser automation library</small>
10
10
  </h1>
11
11
 
12
- <p align=center>
13
- <a href="https://trendshift.io/repositories/5179" target="_blank"><img src="https://trendshift.io/api/badge/repositories/5179" alt="apify%2Fcrawlee | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
12
+ <p align="center">
13
+ <a href="https://trendshift.io/repositories/5179" target="_blank"><img src="https://trendshift.io/api/badge/repositories/5179" alt="apify%2Fcrawlee | Trendshift" width="250" height="55"/></a>
14
14
  </p>
15
15
 
16
- <p align=center>
17
- <a href="https://www.npmjs.com/package/@crawlee/core" rel="nofollow"><img src="https://img.shields.io/npm/v/@crawlee/core.svg" alt="NPM latest version" data-canonical-src="https://img.shields.io/npm/v/@crawlee/core/next.svg" style="max-width: 100%;"></a>
18
- <a href="https://www.npmjs.com/package/@crawlee/core" rel="nofollow"><img src="https://img.shields.io/npm/dm/@crawlee/core.svg" alt="Downloads" data-canonical-src="https://img.shields.io/npm/dm/@crawlee/core.svg" style="max-width: 100%;"></a>
19
- <a href="https://discord.gg/jyEM2PRvMU" rel="nofollow"><img src="https://img.shields.io/discord/801163717915574323?label=discord" alt="Chat on discord" data-canonical-src="https://img.shields.io/discord/801163717915574323?label=discord" style="max-width: 100%;"></a>
20
- <a href="https://github.com/apify/crawlee/actions/workflows/test-ci.yml"><img src="https://github.com/apify/crawlee/actions/workflows/test-ci.yml/badge.svg?branch=master" alt="Build Status" style="max-width: 100%;"></a>
16
+ <p align="center">
17
+ <a href="https://www.npmjs.com/package/@crawlee/core" rel="nofollow"><img src="https://img.shields.io/npm/v/@crawlee/core.svg" alt="NPM latest version" data-canonical-src="https://img.shields.io/npm/v/@crawlee/core/next.svg" /></a>
18
+ <a href="https://www.npmjs.com/package/@crawlee/core" rel="nofollow"><img src="https://img.shields.io/npm/dm/@crawlee/core.svg" alt="Downloads" data-canonical-src="https://img.shields.io/npm/dm/@crawlee/core.svg" /></a>
19
+ <a href="https://discord.gg/jyEM2PRvMU" rel="nofollow"><img src="https://img.shields.io/discord/801163717915574323?label=discord" alt="Chat on discord" data-canonical-src="https://img.shields.io/discord/801163717915574323?label=discord" /></a>
20
+ <a href="https://github.com/apify/crawlee/actions/workflows/test-ci.yml"><img src="https://github.com/apify/crawlee/actions/workflows/test-ci.yml/badge.svg?branch=master" alt="Build Status" /></a>
21
21
  </p>
22
22
 
23
23
  Crawlee covers your crawling and scraping end-to-end and **helps you build reliable scrapers. Fast.**
@@ -89,7 +89,7 @@ By default, Crawlee stores data to `./storage` in the current working directory.
89
89
  We provide automated beta builds for every merged code change in Crawlee. You can find them in the npm [list of releases](https://www.npmjs.com/package/crawlee?activeTab=versions). If you want to test new features or bug fixes before we release them, feel free to install a beta build like this:
90
90
 
91
91
  ```bash
92
- npm install crawlee@3.12.3-beta.13
92
+ npm install crawlee@next
93
93
  ```
94
94
 
95
95
  If you also use the [Apify SDK](https://github.com/apify/apify-sdk-js), you need to specify dependency overrides in your `package.json` file so that you don't end up with multiple versions of Crawlee installed:
@@ -98,9 +98,9 @@ If you also use the [Apify SDK](https://github.com/apify/apify-sdk-js), you need
98
98
  {
99
99
  "overrides": {
100
100
  "apify": {
101
- "@crawlee/core": "3.12.3-beta.13",
102
- "@crawlee/types": "3.12.3-beta.13",
103
- "@crawlee/utils": "3.12.3-beta.13"
101
+ "@crawlee/core": "$crawlee",
102
+ "@crawlee/types": "$crawlee",
103
+ "@crawlee/utils": "$crawlee"
104
104
  }
105
105
  }
106
106
  }
@@ -1,13 +1,10 @@
1
- import type { Log } from '@apify/log';
2
- import { Configuration } from '../configuration.js';
3
- import type { SnapshotterOptions } from './snapshotter.js';
4
- import type { SystemInfo, SystemStatusOptions } from './system_status.js';
5
- export interface AutoscaledPoolOptions {
6
- /**
7
- * A function that performs an asynchronous resource-intensive task.
8
- * The function must either be labeled `async` or return a promise.
9
- */
10
- runTaskFunction?: () => Promise<unknown>;
1
+ import type { ConcurrencyConsumer, IConcurrencySystem } from './concurrency_system.js';
2
+ import type { CrawleeLogger } from '../log.js';
3
+ /**
4
+ * The two predicates that steer a task loop: *is there work ready?* and *are we done?* These are the parts of the loop
5
+ * a caller legitimately overrides, as opposed to the task itself (`runTaskFunction`), which the loop's driver owns.
6
+ */
7
+ export interface TaskLoopPredicates {
11
8
  /**
12
9
  * A function that indicates whether `runTaskFunction` should be called.
13
10
  * This function is called every time there is free capacity for a new task and it should
@@ -17,110 +14,62 @@ export interface AutoscaledPoolOptions {
17
14
  isTaskReadyFunction?: () => Promise<boolean>;
18
15
  /**
19
16
  * A function that is called only when there are no tasks to be processed.
20
- * If it resolves to `true` then the pool's run finishes. Being called only
17
+ * If it resolves to `true` then the run finishes. Being called only
21
18
  * when there are no tasks being processed means that as long as `isTaskReadyFunction()`
22
19
  * keeps resolving to `true`, `isFinishedFunction()` will never be called.
23
- * To abort a run, use the {@link AutoscaledPool.abort} method.
24
20
  */
25
21
  isFinishedFunction?: () => Promise<boolean>;
22
+ }
23
+ export interface TaskLoopOptions extends TaskLoopPredicates {
26
24
  /**
27
- * The minimum number of tasks running in parallel.
28
- *
29
- * *WARNING:* If you set this value too high with respect to the available system memory and CPU, your code might run extremely slow or crash.
30
- * If you're not sure, just keep the default value and the concurrency will scale up automatically.
31
- * @default 1
32
- */
33
- minConcurrency?: number;
34
- /**
35
- * The maximum number of tasks running in parallel.
36
- * @default 200
37
- */
38
- maxConcurrency?: number;
39
- /**
40
- * The desired number of tasks that should be running parallel on the start of the pool,
41
- * if there is a large enough supply of them.
42
- * By default, it is `minConcurrency`.
43
- */
44
- desiredConcurrency?: number;
45
- /**
46
- * Minimum level of desired concurrency to reach before more scaling up is allowed.
47
- * @default 0.90
48
- */
49
- desiredConcurrencyRatio?: number;
50
- /**
51
- * Defines the fractional amount of desired concurrency to be added with each scaling up.
52
- * The minimum scaling step is one.
53
- * @default 0.05
54
- */
55
- scaleUpStepRatio?: number;
56
- /**
57
- * Defines the amount of desired concurrency to be subtracted with each scaling down.
58
- * The minimum scaling step is one.
59
- * @default 0.05
60
- */
61
- scaleDownStepRatio?: number;
62
- /**
63
- * Indicates how often the pool should call the `runTaskFunction()` to start a new task, in seconds.
64
- * This has no effect on starting new tasks immediately after a task completes.
25
+ * How often the pool should check if a new task is ready, in seconds.
65
26
  * @default 0.5
66
27
  */
67
28
  maybeRunIntervalSecs?: number;
29
+ }
30
+ /** @internal */
31
+ export interface AutoscaledPoolOptions extends TaskLoopOptions {
68
32
  /**
69
- * Specifies a period in which the instance logs its state, in seconds.
70
- * Set to `null` to disable periodic logging.
71
- * @default 60
33
+ * The governor that decides whether there is free compute for one more task. Typically a
34
+ * {@link ConcurrencySystem}, but any {@link IConcurrencySystem} works. Share a single instance across
35
+ * multiple pools (and therefore multiple crawlers) to cap their *combined* concurrency against one budget.
36
+ *
37
+ * All concurrency/scaling/snapshotter configuration lives on the governor — the pool only owns the task loop and
38
+ * its cadence.
72
39
  */
73
- loggingIntervalSecs?: number | null;
40
+ concurrencySystem: IConcurrencySystem;
74
41
  /**
75
- * Defines in seconds how often the pool should attempt to adjust the desired concurrency
76
- * based on the latest system status. Setting it lower than 1 might have a severe impact on performance.
77
- * We suggest using a value from 5 to 20.
78
- * @default 10
42
+ * Who this pool is, presented to the governor on every capacity query and booking so that a shared one can tell
43
+ * several pools apart. Worth naming meaningfully a governor that allocates per consumer reports this `id`.
79
44
  */
80
- autoscaleIntervalSecs?: number;
45
+ consumer: ConcurrencyConsumer;
46
+ /**
47
+ * A function that performs an asynchronous resource-intensive task.
48
+ * The function must either be labeled `async` or return a promise.
49
+ */
50
+ runTaskFunction?: () => Promise<unknown>;
81
51
  /**
82
52
  * Timeout in which the `runTaskFunction` needs to finish, given in seconds.
83
53
  * @default 0
84
54
  */
85
55
  taskTimeoutSecs?: number;
86
- /**
87
- * Options to be passed down to the {@link Snapshotter} constructor. This is useful for fine-tuning
88
- * the snapshot intervals and history.
89
- */
90
- snapshotterOptions?: SnapshotterOptions;
91
- /**
92
- * Options to be passed down to the {@link SystemStatus} constructor. This is useful for fine-tuning
93
- * the system status reports. If a custom snapshotter is set in the options, it will be used
94
- * by the pool.
95
- */
96
- systemStatusOptions?: SystemStatusOptions;
97
- /**
98
- * The maximum number of tasks per minute the pool can run.
99
- * By default, this is set to `Infinity`, but you can pass any positive, non-zero integer.
100
- */
101
- maxTasksPerMinute?: number;
102
- log?: Log;
56
+ log?: CrawleeLogger;
103
57
  }
104
58
  /**
105
59
  * Manages a pool of asynchronous resource-intensive tasks that are executed in parallel.
106
- * The pool only starts new tasks if there is enough free CPU and memory available
107
- * and the Javascript event loop is not blocked.
108
- *
109
- * The information about the CPU and memory usage is obtained by the {@link Snapshotter} class,
110
- * which makes regular snapshots of system resources that may be either local
111
- * or from the Apify cloud infrastructure in case the process is running on the Apify platform.
112
- * Meaningful data gathered from these snapshots is provided to `AutoscaledPool` by the {@link SystemStatus} class.
60
+ * The pool only starts new tasks while its {@link IConcurrencySystem|concurrency system} reports free capacity
61
+ * that governor is what monitors CPU, memory and event loop load and autoscales the concurrency budget.
113
62
  *
114
63
  * Before running the pool, you need to implement the following three functions:
115
- * {@link AutoscaledPoolOptions.runTaskFunction},
116
- * {@link AutoscaledPoolOptions.isTaskReadyFunction} and
117
- * {@link AutoscaledPoolOptions.isFinishedFunction}.
64
+ * {@link AutoscaledPoolOptions.runTaskFunction|`runTaskFunction`},
65
+ * {@link TaskLoopPredicates.isTaskReadyFunction|`isTaskReadyFunction`} and
66
+ * {@link TaskLoopPredicates.isFinishedFunction|`isFinishedFunction`}.
118
67
  *
119
68
  * The auto-scaled pool is started by calling the {@link AutoscaledPool.run} function.
120
- * The pool periodically queries the {@link AutoscaledPoolOptions.isTaskReadyFunction} function
121
- * for more tasks, managing optimal concurrency, until the function resolves to `false`. The pool then queries
122
- * the {@link AutoscaledPoolOptions.isFinishedFunction}. If it resolves to `true`, the run finishes after all running tasks complete.
123
- * If it resolves to `false`, it assumes there will be more tasks available later and keeps periodically querying for tasks.
69
+ * The pool periodically queries `isTaskReadyFunction` for more tasks, managing optimal concurrency, until the function
70
+ * resolves to `false`. The pool then queries `isFinishedFunction`. If it resolves to `true`, the run finishes after all
71
+ * running tasks complete. If it resolves to `false`, it assumes there will be more tasks available later and keeps
72
+ * periodically querying for tasks.
124
73
  * If any of the tasks throws then the {@link AutoscaledPool.run} function rejects the promise with an error.
125
74
  *
126
75
  * The pool evaluates whether it should start a new task every time one of the tasks finishes
@@ -129,8 +78,12 @@ export interface AutoscaledPoolOptions {
129
78
  * **Example usage:**
130
79
  *
131
80
  * ```javascript
81
+ * const concurrencySystem = new ConcurrencySystem({ maxConcurrency: 50 });
82
+ * await concurrencySystem.start();
83
+ *
132
84
  * const pool = new AutoscaledPool({
133
- * maxConcurrency: 50,
85
+ * concurrencySystem,
86
+ * consumer: { id: 'my-pool' },
134
87
  * runTaskFunction: async () => {
135
88
  * // Run some resource-intensive asynchronous operation here.
136
89
  * },
@@ -145,77 +98,39 @@ export interface AutoscaledPoolOptions {
145
98
  * }
146
99
  * });
147
100
  *
148
- * await pool.run();
101
+ * try {
102
+ * await pool.run();
103
+ * } finally {
104
+ * await concurrencySystem.stop();
105
+ * }
149
106
  * ```
150
- * @category Scaling
107
+ *
108
+ * @internal
151
109
  */
152
110
  export declare class AutoscaledPool {
153
- private readonly config;
154
- private readonly log;
155
- private readonly desiredConcurrencyRatio;
156
- private readonly scaleUpStepRatio;
157
- private readonly scaleDownStepRatio;
158
- private readonly maybeRunIntervalMillis;
159
- private readonly loggingIntervalMillis;
160
- private readonly autoscaleIntervalMillis;
161
- private readonly taskTimeoutMillis;
162
- private readonly runTaskFunction;
163
- private readonly isFinishedFunction;
164
- private readonly isTaskReadyFunction;
165
- private readonly maxTasksPerMinute;
166
- private _minConcurrency;
167
- private _maxConcurrency;
168
- private _desiredConcurrency;
169
- private _currentConcurrency;
170
- private isStopped;
171
- private lastLoggingTime?;
172
- private resolve;
173
- private reject;
174
- private snapshotter;
175
- private systemStatus;
176
- private autoscaleInterval;
177
- private maybeRunInterval;
178
- private queryingIsTaskReady;
179
- private queryingIsFinished;
180
- private tasksDonePerSecondInterval?;
181
- private _tasksPerMinute;
182
- constructor(options: AutoscaledPoolOptions, config?: Configuration);
183
- /**
184
- * Gets the minimum number of tasks running in parallel.
185
- */
186
- get minConcurrency(): number;
111
+ #private;
112
+ constructor(options: AutoscaledPoolOptions);
187
113
  /**
188
- * Sets the minimum number of tasks running in parallel.
114
+ * The governor backing this pool, as supplied to the constructor exposed as the read-only
115
+ * {@link IConcurrencySystem} contract.
189
116
  *
190
- * *WARNING:* If you set this value too high with respect to the available system memory and CPU, your code might run extremely slow or crash.
191
- * If you're not sure, just keep the default value and the concurrency will scale up automatically.
192
- */
193
- set minConcurrency(value: number);
194
- /**
195
- * Gets the maximum number of tasks running in parallel.
196
- */
197
- get maxConcurrency(): number;
198
- /**
199
- * Sets the maximum number of tasks running in parallel.
200
- */
201
- set maxConcurrency(value: number);
202
- /**
203
- * Gets the desired concurrency for the pool,
204
- * which is an estimated number of parallel tasks that the system can currently support.
117
+ * This and the two getters below are telemetry only: concurrency is configured and tuned on the concrete
118
+ * {@link ConcurrencySystem} its owner holds, never through the pool.
205
119
  */
120
+ get system(): IConcurrencySystem;
121
+ /** The estimated number of parallel tasks the governor can currently support. */
206
122
  get desiredConcurrency(): number;
207
123
  /**
208
- * Sets the desired concurrency for the pool, i.e. the number of tasks that should be running
209
- * in parallel if there's large enough supply of tasks.
210
- */
211
- set desiredConcurrency(value: number);
212
- /**
213
- * Gets the number of parallel tasks currently running in the pool.
124
+ * The number of parallel tasks currently booked against the governor. When it is shared, this counts every
125
+ * borrowing pool's tasks, not just this one's.
214
126
  */
215
127
  get currentConcurrency(): number;
216
128
  /**
217
129
  * Runs the auto-scaled pool. Returns a promise that gets resolved or rejected once
218
130
  * all the tasks are finished or one of them fails.
131
+ *
132
+ * Throws if the {@link IConcurrencySystem|concurrency system} it borrows was never started — the pool assumes
133
+ * a running governor and cannot start one it does not own.
219
134
  */
220
135
  run(): Promise<void>;
221
136
  /**
@@ -240,6 +155,10 @@ export declare class AutoscaledPool {
240
155
  *
241
156
  * The promise returned from the {@link AutoscaledPool.run} function will not resolve
242
157
  * when `.pause()` is invoked (unlike abort, which resolves it).
158
+ *
159
+ * > *NOTE:* Pausing the pool does not suspend the (possibly shared) {@link ConcurrencySystem} — its
160
+ * autoscaling and resource monitoring keep running, since other pools borrowing it may still be active. To silence
161
+ * it during a long pause, its owner can `stop()` and `start()` it again.
243
162
  */
244
163
  pause(timeoutSecs?: number): Promise<void>;
245
164
  /**
@@ -258,43 +177,20 @@ export declare class AutoscaledPool {
258
177
  * Starts a new task
259
178
  * if the number of running tasks (current concurrency) is lower than desired concurrency
260
179
  * and the system is not currently overloaded
261
- * and this.isTaskReadyFunction() returns true.
180
+ * and `isTaskReadyFunction()` returns true.
262
181
  *
263
182
  * It doesn't allow multiple concurrent runs of this method.
264
183
  */
265
- protected _maybeRunTask(intervalCallback?: () => void): Promise<void>;
266
- /**
267
- * Gets called every autoScaleIntervalSecs and evaluates the current system status.
268
- * If the system IS NOT overloaded and the settings allow it, it scales up.
269
- * If the system IS overloaded and the settings allow it, it scales down.
270
- */
271
- protected _autoscale(intervalCallback: () => void): void;
272
- /**
273
- * Scales the pool up by increasing
274
- * the desired concurrency by the scaleUpStepRatio.
275
- *
276
- * @param systemStatus for logging
277
- */
278
- protected _scaleUp(systemStatus: SystemInfo): void;
279
- /**
280
- * Scales the pool down by decreasing
281
- * the desired concurrency by the scaleDownStepRatio.
282
- *
283
- * @param systemStatus for logging
284
- */
285
- protected _scaleDown(systemStatus: SystemInfo): void;
184
+ private maybeRunTask;
286
185
  /**
287
- * If there are no running tasks and this.isFinishedFunction() returns true then closes
186
+ * If there are no running tasks and `isFinishedFunction()` returns true then closes
288
187
  * the pool and resolves the pool's promise returned by the run() method.
289
188
  *
290
189
  * It doesn't allow multiple concurrent runs of this method.
291
190
  */
292
- protected _maybeFinish(): Promise<void>;
191
+ private maybeFinish;
293
192
  /**
294
193
  * Cleans up resources.
295
194
  */
296
- protected _destroy(): Promise<void>;
297
- protected _incrementTasksDonePerSecond(intervalCallback: () => void): void;
298
- protected get _isOverMaxRequestLimit(): boolean;
195
+ private destroy;
299
196
  }
300
- //# sourceMappingURL=autoscaled_pool.d.ts.map