@crawlee/core 4.0.0-beta.1 → 4.0.0-beta.100

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 (269) hide show
  1. package/README.md +17 -13
  2. package/autoscaling/autoscaled_pool.d.ts +69 -156
  3. package/autoscaling/autoscaled_pool.js +97 -252
  4. package/autoscaling/client_load_signal.d.ts +59 -0
  5. package/autoscaling/client_load_signal.js +73 -0
  6. package/autoscaling/concurrency_system.d.ts +283 -0
  7. package/autoscaling/concurrency_system.js +350 -0
  8. package/autoscaling/cpu_load_signal.d.ts +44 -0
  9. package/autoscaling/cpu_load_signal.js +46 -0
  10. package/autoscaling/event_loop_load_signal.d.ts +54 -0
  11. package/autoscaling/event_loop_load_signal.js +60 -0
  12. package/autoscaling/index.d.ts +6 -1
  13. package/autoscaling/index.js +6 -1
  14. package/autoscaling/load_signal.d.ts +99 -0
  15. package/autoscaling/load_signal.js +103 -0
  16. package/autoscaling/memory_load_signal.d.ts +56 -0
  17. package/autoscaling/memory_load_signal.js +106 -0
  18. package/autoscaling/snapshotter.d.ts +61 -163
  19. package/autoscaling/snapshotter.js +45 -270
  20. package/autoscaling/system_status.d.ts +63 -83
  21. package/autoscaling/system_status.js +90 -120
  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 -230
  27. package/configuration.js +170 -226
  28. package/cookie_utils.d.ts +4 -3
  29. package/cookie_utils.js +22 -13
  30. package/crawlers/context_pipeline.d.ts +70 -0
  31. package/crawlers/context_pipeline.js +122 -0
  32. package/crawlers/crawler_commons.d.ts +96 -40
  33. package/crawlers/crawler_commons.js +15 -24
  34. package/crawlers/error_snapshotter.d.ts +3 -3
  35. package/crawlers/error_snapshotter.js +2 -3
  36. package/crawlers/error_tracker.d.ts +2 -2
  37. package/crawlers/error_tracker.js +0 -1
  38. package/crawlers/index.d.ts +1 -3
  39. package/crawlers/index.js +1 -3
  40. package/crawlers/internals/types.d.ts +7 -0
  41. package/crawlers/internals/types.js +1 -0
  42. package/crawlers/statistics.d.ts +28 -23
  43. package/crawlers/statistics.js +38 -34
  44. package/debug.d.ts +36 -0
  45. package/debug.js +70 -0
  46. package/enqueue_links/enqueue_links.d.ts +59 -68
  47. package/enqueue_links/enqueue_links.js +57 -62
  48. package/enqueue_links/index.d.ts +0 -1
  49. package/enqueue_links/index.js +0 -1
  50. package/enqueue_links/shared.d.ts +39 -26
  51. package/enqueue_links/shared.js +89 -67
  52. package/errors.d.ts +53 -4
  53. package/errors.js +70 -5
  54. package/events/event_manager.d.ts +34 -8
  55. package/events/event_manager.js +8 -10
  56. package/events/index.d.ts +0 -1
  57. package/events/index.js +0 -1
  58. package/events/local_event_manager.d.ts +15 -5
  59. package/events/local_event_manager.js +37 -40
  60. package/index.d.ts +6 -4
  61. package/index.js +5 -3
  62. package/iterables.d.ts +79 -0
  63. package/iterables.js +134 -0
  64. package/log.d.ts +82 -3
  65. package/log.js +102 -1
  66. package/memory-storage/consts.d.ts +4 -0
  67. package/memory-storage/consts.js +4 -0
  68. package/memory-storage/index.d.ts +1 -0
  69. package/memory-storage/index.js +1 -0
  70. package/memory-storage/memory-storage.d.ts +46 -0
  71. package/memory-storage/memory-storage.js +136 -0
  72. package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
  73. package/memory-storage/resource-clients/common/base-client.js +6 -0
  74. package/memory-storage/resource-clients/dataset.d.ts +40 -0
  75. package/memory-storage/resource-clients/dataset.js +113 -0
  76. package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
  77. package/memory-storage/resource-clients/key-value-store.js +203 -0
  78. package/memory-storage/resource-clients/request-queue.d.ts +96 -0
  79. package/memory-storage/resource-clients/request-queue.js +421 -0
  80. package/memory-storage/utils.d.ts +16 -0
  81. package/memory-storage/utils.js +41 -0
  82. package/owned_or_injected.d.ts +60 -0
  83. package/owned_or_injected.js +98 -0
  84. package/package.json +13 -12
  85. package/proxy_configuration.d.ts +29 -152
  86. package/proxy_configuration.js +21 -173
  87. package/recoverable_state.d.ts +120 -0
  88. package/recoverable_state.js +143 -0
  89. package/request.d.ts +85 -17
  90. package/request.js +107 -36
  91. package/router.d.ts +194 -19
  92. package/router.js +177 -32
  93. package/serialization.d.ts +0 -1
  94. package/serialization.js +1 -2
  95. package/service_locator.d.ts +156 -0
  96. package/service_locator.js +244 -0
  97. package/session_pool/consts.d.ts +1 -2
  98. package/session_pool/consts.js +1 -2
  99. package/session_pool/errors.d.ts +0 -1
  100. package/session_pool/errors.js +0 -1
  101. package/session_pool/fingerprint.d.ts +9 -0
  102. package/session_pool/fingerprint.js +30 -0
  103. package/session_pool/index.d.ts +0 -2
  104. package/session_pool/index.js +0 -2
  105. package/session_pool/session.d.ts +37 -75
  106. package/session_pool/session.js +49 -102
  107. package/session_pool/session_pool.d.ts +85 -90
  108. package/session_pool/session_pool.js +131 -120
  109. package/storages/access_checking.d.ts +1 -2
  110. package/storages/access_checking.js +5 -2
  111. package/storages/dataset.d.ts +103 -54
  112. package/storages/dataset.js +174 -132
  113. package/storages/index.d.ts +8 -7
  114. package/storages/index.js +6 -7
  115. package/storages/key_value_store.d.ts +167 -39
  116. package/storages/key_value_store.js +274 -127
  117. package/storages/key_value_store_codec.d.ts +32 -0
  118. package/storages/key_value_store_codec.js +113 -0
  119. package/storages/request_dedup_cache.d.ts +23 -0
  120. package/storages/request_dedup_cache.js +48 -0
  121. package/storages/request_list.d.ts +54 -98
  122. package/storages/request_list.js +99 -75
  123. package/storages/request_loader.d.ts +96 -0
  124. package/storages/request_loader.js +1 -0
  125. package/storages/request_manager.d.ts +33 -0
  126. package/storages/request_manager.js +1 -0
  127. package/storages/request_manager_tandem.d.ts +106 -0
  128. package/storages/request_manager_tandem.js +197 -0
  129. package/storages/request_queue.d.ts +287 -47
  130. package/storages/request_queue.js +620 -215
  131. package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +38 -46
  132. package/storages/{sitemap_request_list.js → sitemap_request_loader.js} +58 -65
  133. package/storages/storage_instance_manager.d.ts +88 -0
  134. package/storages/storage_instance_manager.js +256 -0
  135. package/storages/storage_stats.d.ts +48 -0
  136. package/storages/storage_stats.js +29 -0
  137. package/storages/utils.d.ts +54 -9
  138. package/storages/utils.js +64 -13
  139. package/system-info/cpu-info.d.ts +67 -0
  140. package/system-info/cpu-info.js +216 -0
  141. package/system-info/memory-info.d.ts +31 -0
  142. package/system-info/memory-info.js +115 -0
  143. package/system-info/ps-tree.d.ts +17 -0
  144. package/system-info/ps-tree.js +144 -0
  145. package/system-info/runtime.d.ts +14 -0
  146. package/system-info/runtime.js +80 -0
  147. package/typedefs.d.ts +0 -6
  148. package/typedefs.js +0 -1
  149. package/validators.d.ts +8 -1
  150. package/validators.js +10 -3
  151. package/autoscaling/autoscaled_pool.d.ts.map +0 -1
  152. package/autoscaling/autoscaled_pool.js.map +0 -1
  153. package/autoscaling/index.d.ts.map +0 -1
  154. package/autoscaling/index.js.map +0 -1
  155. package/autoscaling/snapshotter.d.ts.map +0 -1
  156. package/autoscaling/snapshotter.js.map +0 -1
  157. package/autoscaling/system_status.d.ts.map +0 -1
  158. package/autoscaling/system_status.js.map +0 -1
  159. package/configuration.d.ts.map +0 -1
  160. package/configuration.js.map +0 -1
  161. package/cookie_utils.d.ts.map +0 -1
  162. package/cookie_utils.js.map +0 -1
  163. package/crawlers/crawler_commons.d.ts.map +0 -1
  164. package/crawlers/crawler_commons.js.map +0 -1
  165. package/crawlers/crawler_extension.d.ts +0 -12
  166. package/crawlers/crawler_extension.d.ts.map +0 -1
  167. package/crawlers/crawler_extension.js +0 -14
  168. package/crawlers/crawler_extension.js.map +0 -1
  169. package/crawlers/crawler_utils.d.ts +0 -10
  170. package/crawlers/crawler_utils.d.ts.map +0 -1
  171. package/crawlers/crawler_utils.js +0 -12
  172. package/crawlers/crawler_utils.js.map +0 -1
  173. package/crawlers/error_snapshotter.d.ts.map +0 -1
  174. package/crawlers/error_snapshotter.js.map +0 -1
  175. package/crawlers/error_tracker.d.ts.map +0 -1
  176. package/crawlers/error_tracker.js.map +0 -1
  177. package/crawlers/index.d.ts.map +0 -1
  178. package/crawlers/index.js.map +0 -1
  179. package/crawlers/statistics.d.ts.map +0 -1
  180. package/crawlers/statistics.js.map +0 -1
  181. package/enqueue_links/enqueue_links.d.ts.map +0 -1
  182. package/enqueue_links/enqueue_links.js.map +0 -1
  183. package/enqueue_links/index.d.ts.map +0 -1
  184. package/enqueue_links/index.js.map +0 -1
  185. package/enqueue_links/shared.d.ts.map +0 -1
  186. package/enqueue_links/shared.js.map +0 -1
  187. package/errors.d.ts.map +0 -1
  188. package/errors.js.map +0 -1
  189. package/events/event_manager.d.ts.map +0 -1
  190. package/events/event_manager.js.map +0 -1
  191. package/events/index.d.ts.map +0 -1
  192. package/events/index.js.map +0 -1
  193. package/events/local_event_manager.d.ts.map +0 -1
  194. package/events/local_event_manager.js.map +0 -1
  195. package/http_clients/base-http-client.d.ts +0 -134
  196. package/http_clients/base-http-client.d.ts.map +0 -1
  197. package/http_clients/base-http-client.js +0 -33
  198. package/http_clients/base-http-client.js.map +0 -1
  199. package/http_clients/form-data-like.d.ts +0 -67
  200. package/http_clients/form-data-like.d.ts.map +0 -1
  201. package/http_clients/form-data-like.js +0 -5
  202. package/http_clients/form-data-like.js.map +0 -1
  203. package/http_clients/got-scraping-http-client.d.ts +0 -15
  204. package/http_clients/got-scraping-http-client.d.ts.map +0 -1
  205. package/http_clients/got-scraping-http-client.js +0 -69
  206. package/http_clients/got-scraping-http-client.js.map +0 -1
  207. package/http_clients/index.d.ts +0 -3
  208. package/http_clients/index.d.ts.map +0 -1
  209. package/http_clients/index.js +0 -3
  210. package/http_clients/index.js.map +0 -1
  211. package/index.d.ts.map +0 -1
  212. package/index.js.map +0 -1
  213. package/log.d.ts.map +0 -1
  214. package/log.js.map +0 -1
  215. package/proxy_configuration.d.ts.map +0 -1
  216. package/proxy_configuration.js.map +0 -1
  217. package/request.d.ts.map +0 -1
  218. package/request.js.map +0 -1
  219. package/router.d.ts.map +0 -1
  220. package/router.js.map +0 -1
  221. package/serialization.d.ts.map +0 -1
  222. package/serialization.js.map +0 -1
  223. package/session_pool/consts.d.ts.map +0 -1
  224. package/session_pool/consts.js.map +0 -1
  225. package/session_pool/errors.d.ts.map +0 -1
  226. package/session_pool/errors.js.map +0 -1
  227. package/session_pool/events.d.ts +0 -3
  228. package/session_pool/events.d.ts.map +0 -1
  229. package/session_pool/events.js +0 -3
  230. package/session_pool/events.js.map +0 -1
  231. package/session_pool/index.d.ts.map +0 -1
  232. package/session_pool/index.js.map +0 -1
  233. package/session_pool/session.d.ts.map +0 -1
  234. package/session_pool/session.js.map +0 -1
  235. package/session_pool/session_pool.d.ts.map +0 -1
  236. package/session_pool/session_pool.js.map +0 -1
  237. package/storages/access_checking.d.ts.map +0 -1
  238. package/storages/access_checking.js.map +0 -1
  239. package/storages/dataset.d.ts.map +0 -1
  240. package/storages/dataset.js.map +0 -1
  241. package/storages/index.d.ts.map +0 -1
  242. package/storages/index.js.map +0 -1
  243. package/storages/key_value_store.d.ts.map +0 -1
  244. package/storages/key_value_store.js.map +0 -1
  245. package/storages/request_list.d.ts.map +0 -1
  246. package/storages/request_list.js.map +0 -1
  247. package/storages/request_provider.d.ts +0 -307
  248. package/storages/request_provider.d.ts.map +0 -1
  249. package/storages/request_provider.js +0 -555
  250. package/storages/request_provider.js.map +0 -1
  251. package/storages/request_queue.d.ts.map +0 -1
  252. package/storages/request_queue.js.map +0 -1
  253. package/storages/request_queue_v2.d.ts +0 -87
  254. package/storages/request_queue_v2.d.ts.map +0 -1
  255. package/storages/request_queue_v2.js +0 -438
  256. package/storages/request_queue_v2.js.map +0 -1
  257. package/storages/sitemap_request_list.d.ts.map +0 -1
  258. package/storages/sitemap_request_list.js.map +0 -1
  259. package/storages/storage_manager.d.ts +0 -58
  260. package/storages/storage_manager.d.ts.map +0 -1
  261. package/storages/storage_manager.js +0 -105
  262. package/storages/storage_manager.js.map +0 -1
  263. package/storages/utils.d.ts.map +0 -1
  264. package/storages/utils.js.map +0 -1
  265. package/tsconfig.build.tsbuildinfo +0 -1
  266. package/typedefs.d.ts.map +0 -1
  267. package/typedefs.js.map +0 -1
  268. package/validators.d.ts.map +0 -1
  269. package/validators.js.map +0 -1
package/README.md CHANGED
@@ -1,19 +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://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>
14
- <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>
15
- <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>
16
- <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>
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
+ </p>
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" /></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>
17
21
  </p>
18
22
 
19
23
  Crawlee covers your crawling and scraping end-to-end and **helps you build reliable scrapers. Fast.**
@@ -24,7 +28,7 @@ Crawlee is available as the [`crawlee`](https://www.npmjs.com/package/crawlee) N
24
28
 
25
29
  > 👉 **View full documentation, guides and examples on the [Crawlee project website](https://crawlee.dev)** 👈
26
30
 
27
- > Crawlee for Python is open for early adopters. 🐍 [👉 Checkout the source code 👈](https://github.com/apify/crawlee-python).
31
+ > Do you prefer 🐍 Python instead of JavaScript? [👉 Checkout Crawlee for Python 👈](https://github.com/apify/crawlee-python).
28
32
 
29
33
  ## Installation
30
34
 
@@ -85,7 +89,7 @@ By default, Crawlee stores data to `./storage` in the current working directory.
85
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:
86
90
 
87
91
  ```bash
88
- npm install crawlee@3.12.3-beta.13
92
+ npm install crawlee@next
89
93
  ```
90
94
 
91
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:
@@ -94,9 +98,9 @@ If you also use the [Apify SDK](https://github.com/apify/apify-sdk-js), you need
94
98
  {
95
99
  "overrides": {
96
100
  "apify": {
97
- "@crawlee/core": "3.12.3-beta.13",
98
- "@crawlee/types": "3.12.3-beta.13",
99
- "@crawlee/utils": "3.12.3-beta.13"
101
+ "@crawlee/core": "$crawlee",
102
+ "@crawlee/types": "$crawlee",
103
+ "@crawlee/utils": "$crawlee"
100
104
  }
101
105
  }
102
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,61 @@ 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
+ /** @internal */
24
+ export interface AutoscaledPoolOptions extends TaskLoopPredicates {
26
25
  /**
27
- * The minimum number of tasks running in parallel.
26
+ * The governor that decides whether there is free compute for one more task. Typically a
27
+ * {@link ConcurrencySystem}, but any {@link IConcurrencySystem} works. Share a single instance across
28
+ * multiple pools (and therefore multiple crawlers) to cap their *combined* concurrency against one budget.
28
29
  *
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`.
30
+ * All concurrency/scaling/snapshotter configuration lives on the governor the pool only owns the task loop and
31
+ * its cadence.
43
32
  */
44
- desiredConcurrency?: number;
33
+ concurrencySystem: IConcurrencySystem;
45
34
  /**
46
- * Minimum level of desired concurrency to reach before more scaling up is allowed.
47
- * @default 0.90
35
+ * Who this pool is, presented to the governor on every capacity query and booking so that a shared one can tell
36
+ * several pools apart. Worth naming meaningfully — a governor that allocates per consumer reports this `id`.
48
37
  */
49
- desiredConcurrencyRatio?: number;
38
+ consumer: ConcurrencyConsumer;
50
39
  /**
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
40
+ * A function that performs an asynchronous resource-intensive task.
41
+ * The function must either be labeled `async` or return a promise.
60
42
  */
61
- scaleDownStepRatio?: number;
43
+ runTaskFunction?: () => Promise<unknown>;
62
44
  /**
63
45
  * Indicates how often the pool should call the `runTaskFunction()` to start a new task, in seconds.
64
46
  * This has no effect on starting new tasks immediately after a task completes.
65
47
  * @default 0.5
66
48
  */
67
49
  maybeRunIntervalSecs?: number;
68
- /**
69
- * Specifies a period in which the instance logs its state, in seconds.
70
- * Set to `null` to disable periodic logging.
71
- * @default 60
72
- */
73
- loggingIntervalSecs?: number | null;
74
- /**
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
79
- */
80
- autoscaleIntervalSecs?: number;
81
50
  /**
82
51
  * Timeout in which the `runTaskFunction` needs to finish, given in seconds.
83
52
  * @default 0
84
53
  */
85
54
  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;
55
+ log?: CrawleeLogger;
103
56
  }
104
57
  /**
105
58
  * 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.
59
+ * The pool only starts new tasks while its {@link IConcurrencySystem|concurrency system} reports free capacity
60
+ * that governor is what monitors CPU, memory and event loop load and autoscales the concurrency budget.
113
61
  *
114
62
  * 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}.
63
+ * {@link AutoscaledPoolOptions.runTaskFunction|`runTaskFunction`},
64
+ * {@link TaskLoopPredicates.isTaskReadyFunction|`isTaskReadyFunction`} and
65
+ * {@link TaskLoopPredicates.isFinishedFunction|`isFinishedFunction`}.
118
66
  *
119
67
  * 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.
68
+ * The pool periodically queries `isTaskReadyFunction` for more tasks, managing optimal concurrency, until the function
69
+ * resolves to `false`. The pool then queries `isFinishedFunction`. If it resolves to `true`, the run finishes after all
70
+ * running tasks complete. If it resolves to `false`, it assumes there will be more tasks available later and keeps
71
+ * periodically querying for tasks.
124
72
  * If any of the tasks throws then the {@link AutoscaledPool.run} function rejects the promise with an error.
125
73
  *
126
74
  * The pool evaluates whether it should start a new task every time one of the tasks finishes
@@ -129,8 +77,12 @@ export interface AutoscaledPoolOptions {
129
77
  * **Example usage:**
130
78
  *
131
79
  * ```javascript
80
+ * const concurrencySystem = new ConcurrencySystem({ maxConcurrency: 50 });
81
+ * await concurrencySystem.start();
82
+ *
132
83
  * const pool = new AutoscaledPool({
133
- * maxConcurrency: 50,
84
+ * concurrencySystem,
85
+ * consumer: { id: 'my-pool' },
134
86
  * runTaskFunction: async () => {
135
87
  * // Run some resource-intensive asynchronous operation here.
136
88
  * },
@@ -145,77 +97,57 @@ export interface AutoscaledPoolOptions {
145
97
  * }
146
98
  * });
147
99
  *
148
- * await pool.run();
100
+ * try {
101
+ * await pool.run();
102
+ * } finally {
103
+ * await concurrencySystem.stop();
104
+ * }
149
105
  * ```
150
- * @category Scaling
106
+ *
107
+ * @internal
151
108
  */
152
109
  export declare class AutoscaledPool {
153
- private readonly config;
154
110
  private readonly log;
155
- private readonly desiredConcurrencyRatio;
156
- private readonly scaleUpStepRatio;
157
- private readonly scaleDownStepRatio;
158
111
  private readonly maybeRunIntervalMillis;
159
- private readonly loggingIntervalMillis;
160
- private readonly autoscaleIntervalMillis;
161
112
  private readonly taskTimeoutMillis;
162
113
  private readonly runTaskFunction;
163
114
  private readonly isFinishedFunction;
164
115
  private readonly isTaskReadyFunction;
165
- private readonly maxTasksPerMinute;
166
- private _minConcurrency;
167
- private _maxConcurrency;
168
- private _desiredConcurrency;
169
- private _currentConcurrency;
116
+ private readonly concurrencySystem;
117
+ private readonly consumer;
170
118
  private isStopped;
171
- private lastLoggingTime?;
172
119
  private resolve;
173
120
  private reject;
174
- private snapshotter;
175
- private systemStatus;
176
- private autoscaleInterval;
177
121
  private maybeRunInterval;
178
122
  private queryingIsTaskReady;
179
123
  private queryingIsFinished;
180
- private tasksDonePerSecondInterval?;
181
- private _tasksPerMinute;
182
- constructor(options: AutoscaledPoolOptions, config?: Configuration);
183
124
  /**
184
- * Gets the minimum number of tasks running in parallel.
125
+ * This pool's *own* in-flight task count, as opposed to {@link AutoscaledPool.currentConcurrency}, which is the
126
+ * (possibly shared) governor's total. `pause()` and `maybeFinish()` care only about this pool draining.
185
127
  */
186
- get minConcurrency(): number;
128
+ private ownConcurrency;
129
+ constructor(options: AutoscaledPoolOptions);
187
130
  /**
188
- * Sets the minimum number of tasks running in parallel.
131
+ * The governor backing this pool, as supplied to the constructor exposed as the read-only
132
+ * {@link IConcurrencySystem} contract.
189
133
  *
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.
134
+ * This and the two getters below are telemetry only: concurrency is configured and tuned on the concrete
135
+ * {@link ConcurrencySystem} its owner holds, never through the pool.
205
136
  */
137
+ get system(): IConcurrencySystem;
138
+ /** The estimated number of parallel tasks the governor can currently support. */
206
139
  get desiredConcurrency(): number;
207
140
  /**
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.
141
+ * The number of parallel tasks currently booked against the governor. When it is shared, this counts every
142
+ * borrowing pool's tasks, not just this one's.
214
143
  */
215
144
  get currentConcurrency(): number;
216
145
  /**
217
146
  * Runs the auto-scaled pool. Returns a promise that gets resolved or rejected once
218
147
  * all the tasks are finished or one of them fails.
148
+ *
149
+ * Throws if the {@link IConcurrencySystem|concurrency system} it borrows was never started — the pool assumes
150
+ * a running governor and cannot start one it does not own.
219
151
  */
220
152
  run(): Promise<void>;
221
153
  /**
@@ -240,6 +172,10 @@ export declare class AutoscaledPool {
240
172
  *
241
173
  * The promise returned from the {@link AutoscaledPool.run} function will not resolve
242
174
  * when `.pause()` is invoked (unlike abort, which resolves it).
175
+ *
176
+ * > *NOTE:* Pausing the pool does not suspend the (possibly shared) {@link ConcurrencySystem} — its
177
+ * autoscaling and resource monitoring keep running, since other pools borrowing it may still be active. To silence
178
+ * it during a long pause, its owner can `stop()` and `start()` it again.
243
179
  */
244
180
  pause(timeoutSecs?: number): Promise<void>;
245
181
  /**
@@ -262,39 +198,16 @@ export declare class AutoscaledPool {
262
198
  *
263
199
  * It doesn't allow multiple concurrent runs of this method.
264
200
  */
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;
201
+ private maybeRunTask;
286
202
  /**
287
203
  * If there are no running tasks and this.isFinishedFunction() returns true then closes
288
204
  * the pool and resolves the pool's promise returned by the run() method.
289
205
  *
290
206
  * It doesn't allow multiple concurrent runs of this method.
291
207
  */
292
- protected _maybeFinish(): Promise<void>;
208
+ private maybeFinish;
293
209
  /**
294
210
  * Cleans up resources.
295
211
  */
296
- protected _destroy(): Promise<void>;
297
- protected _incrementTasksDonePerSecond(intervalCallback: () => void): void;
298
- protected get _isOverMaxRequestLimit(): boolean;
212
+ private destroy;
299
213
  }
300
- //# sourceMappingURL=autoscaled_pool.d.ts.map