pp-command-bus 1.5.0 → 2.0.0

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 (193) hide show
  1. package/README.md +400 -1219
  2. package/dist/command-bus/command-bus.spec.js +138 -359
  3. package/dist/command-bus/command-bus.spec.js.map +1 -1
  4. package/dist/command-bus/command.d.ts +3 -4
  5. package/dist/command-bus/command.js +3 -32
  6. package/dist/command-bus/command.js.map +1 -1
  7. package/dist/command-bus/config/command-bus-config.d.ts +75 -21
  8. package/dist/command-bus/config/command-bus-config.js +99 -58
  9. package/dist/command-bus/config/command-bus-config.js.map +1 -1
  10. package/dist/command-bus/config/command-bus-config.spec.js +174 -100
  11. package/dist/command-bus/config/command-bus-config.spec.js.map +1 -1
  12. package/dist/command-bus/index.d.ts +39 -52
  13. package/dist/command-bus/index.js +133 -126
  14. package/dist/command-bus/index.js.map +1 -1
  15. package/dist/command-bus/logging/command-logger.d.ts +2 -0
  16. package/dist/command-bus/logging/command-logger.js +7 -0
  17. package/dist/command-bus/logging/command-logger.js.map +1 -1
  18. package/dist/command-bus/logging/command-logger.spec.js +36 -0
  19. package/dist/command-bus/logging/command-logger.spec.js.map +1 -1
  20. package/dist/command-bus/serialization/index.d.ts +6 -0
  21. package/dist/command-bus/serialization/index.js +9 -0
  22. package/dist/command-bus/serialization/index.js.map +1 -0
  23. package/dist/command-bus/serialization/msgpack-serializer.d.ts +26 -0
  24. package/dist/command-bus/serialization/msgpack-serializer.js +70 -0
  25. package/dist/command-bus/serialization/msgpack-serializer.js.map +1 -0
  26. package/dist/command-bus/serialization/msgpack-serializer.spec.js +223 -0
  27. package/dist/command-bus/serialization/msgpack-serializer.spec.js.map +1 -0
  28. package/dist/command-bus/serialization/serializer.interface.d.ts +21 -0
  29. package/dist/command-bus/serialization/serializer.interface.js +3 -0
  30. package/dist/command-bus/serialization/serializer.interface.js.map +1 -0
  31. package/dist/command-bus/transport/consumer-loop.d.ts +45 -0
  32. package/dist/command-bus/transport/consumer-loop.js +90 -0
  33. package/dist/command-bus/transport/consumer-loop.js.map +1 -0
  34. package/dist/command-bus/transport/consumer-loop.spec.js +216 -0
  35. package/dist/command-bus/transport/consumer-loop.spec.js.map +1 -0
  36. package/dist/command-bus/transport/index.d.ts +21 -0
  37. package/dist/command-bus/transport/index.js +23 -0
  38. package/dist/command-bus/transport/index.js.map +1 -0
  39. package/dist/command-bus/transport/message-processor.d.ts +59 -0
  40. package/dist/command-bus/transport/message-processor.js +111 -0
  41. package/dist/command-bus/transport/message-processor.js.map +1 -0
  42. package/dist/command-bus/transport/message-processor.spec.js +185 -0
  43. package/dist/command-bus/transport/message-processor.spec.js.map +1 -0
  44. package/dist/command-bus/transport/pending-recovery.d.ts +54 -0
  45. package/dist/command-bus/transport/pending-recovery.js +139 -0
  46. package/dist/command-bus/transport/pending-recovery.js.map +1 -0
  47. package/dist/command-bus/transport/pending-recovery.spec.js +176 -0
  48. package/dist/command-bus/transport/pending-recovery.spec.js.map +1 -0
  49. package/dist/command-bus/transport/redis-codec.d.ts +24 -0
  50. package/dist/command-bus/transport/redis-codec.js +33 -0
  51. package/dist/command-bus/transport/redis-codec.js.map +1 -0
  52. package/dist/command-bus/transport/redis-codec.spec.js +53 -0
  53. package/dist/command-bus/transport/redis-codec.spec.js.map +1 -0
  54. package/dist/command-bus/transport/redis-streams-transport.d.ts +91 -0
  55. package/dist/command-bus/transport/redis-streams-transport.js +134 -0
  56. package/dist/command-bus/transport/redis-streams-transport.js.map +1 -0
  57. package/dist/command-bus/transport/redis-streams-transport.spec.js +420 -0
  58. package/dist/command-bus/transport/redis-streams-transport.spec.js.map +1 -0
  59. package/dist/command-bus/transport/rpc-handler.d.ts +39 -0
  60. package/dist/command-bus/transport/rpc-handler.js +87 -0
  61. package/dist/command-bus/transport/rpc-handler.js.map +1 -0
  62. package/dist/command-bus/transport/rpc-handler.spec.js +157 -0
  63. package/dist/command-bus/transport/rpc-handler.spec.js.map +1 -0
  64. package/dist/command-bus/transport/stream-consumer.d.ts +89 -0
  65. package/dist/command-bus/transport/stream-consumer.js +181 -0
  66. package/dist/command-bus/transport/stream-consumer.js.map +1 -0
  67. package/dist/command-bus/transport/stream-consumer.spec.js +284 -0
  68. package/dist/command-bus/transport/stream-consumer.spec.js.map +1 -0
  69. package/dist/command-bus/transport/stream-producer.d.ts +23 -0
  70. package/dist/command-bus/transport/stream-producer.js +70 -0
  71. package/dist/command-bus/transport/stream-producer.js.map +1 -0
  72. package/dist/command-bus/transport/stream-producer.spec.js +125 -0
  73. package/dist/command-bus/transport/stream-producer.spec.js.map +1 -0
  74. package/dist/command-bus/transport/transport.interface.d.ts +87 -0
  75. package/dist/command-bus/transport/transport.interface.js +3 -0
  76. package/dist/command-bus/transport/transport.interface.js.map +1 -0
  77. package/dist/command-bus/types/index.d.ts +0 -84
  78. package/dist/examples/rpc.demo.js +1 -1
  79. package/dist/examples/rpc.demo.js.map +1 -1
  80. package/dist/index.d.ts +8 -5
  81. package/dist/index.js +6 -4
  82. package/dist/index.js.map +1 -1
  83. package/dist/pp-command-bus-2.0.0.tgz +0 -0
  84. package/dist/shared/redis/connection-pool.d.ts +54 -0
  85. package/dist/shared/redis/connection-pool.js +117 -0
  86. package/dist/shared/redis/connection-pool.js.map +1 -0
  87. package/dist/shared/redis/connection-pool.spec.js +114 -0
  88. package/dist/shared/redis/connection-pool.spec.js.map +1 -0
  89. package/dist/shared/redis/index.d.ts +5 -3
  90. package/dist/shared/redis/index.js +6 -4
  91. package/dist/shared/redis/index.js.map +1 -1
  92. package/dist/shared/redis/rpc-connection-pool.d.ts +61 -0
  93. package/dist/shared/redis/rpc-connection-pool.js +154 -0
  94. package/dist/shared/redis/rpc-connection-pool.js.map +1 -0
  95. package/dist/shared/redis/rpc-connection-pool.spec.d.ts +1 -0
  96. package/dist/shared/redis/rpc-connection-pool.spec.js +173 -0
  97. package/dist/shared/redis/rpc-connection-pool.spec.js.map +1 -0
  98. package/dist/shared/types.d.ts +0 -4
  99. package/dist/shared/utils/error-utils.d.ts +8 -0
  100. package/dist/shared/utils/error-utils.js +14 -0
  101. package/dist/shared/utils/error-utils.js.map +1 -0
  102. package/package.json +12 -12
  103. package/dist/command-bus/config/auto-config-optimizer.d.ts +0 -35
  104. package/dist/command-bus/config/auto-config-optimizer.js +0 -52
  105. package/dist/command-bus/config/auto-config-optimizer.js.map +0 -1
  106. package/dist/command-bus/config/auto-config-optimizer.spec.js +0 -42
  107. package/dist/command-bus/config/auto-config-optimizer.spec.js.map +0 -1
  108. package/dist/command-bus/job/index.d.ts +0 -6
  109. package/dist/command-bus/job/index.js +0 -15
  110. package/dist/command-bus/job/index.js.map +0 -1
  111. package/dist/command-bus/job/job-options-builder.d.ts +0 -21
  112. package/dist/command-bus/job/job-options-builder.js +0 -58
  113. package/dist/command-bus/job/job-options-builder.js.map +0 -1
  114. package/dist/command-bus/job/job-options-builder.spec.js +0 -156
  115. package/dist/command-bus/job/job-options-builder.spec.js.map +0 -1
  116. package/dist/command-bus/job/job-processor.d.ts +0 -39
  117. package/dist/command-bus/job/job-processor.js +0 -203
  118. package/dist/command-bus/job/job-processor.js.map +0 -1
  119. package/dist/command-bus/job/job-processor.spec.js +0 -436
  120. package/dist/command-bus/job/job-processor.spec.js.map +0 -1
  121. package/dist/command-bus/queue/index.d.ts +0 -5
  122. package/dist/command-bus/queue/index.js +0 -13
  123. package/dist/command-bus/queue/index.js.map +0 -1
  124. package/dist/command-bus/queue/queue-manager.d.ts +0 -56
  125. package/dist/command-bus/queue/queue-manager.js +0 -163
  126. package/dist/command-bus/queue/queue-manager.js.map +0 -1
  127. package/dist/command-bus/queue/queue-manager.spec.js +0 -371
  128. package/dist/command-bus/queue/queue-manager.spec.js.map +0 -1
  129. package/dist/command-bus/rpc/index.d.ts +0 -11
  130. package/dist/command-bus/rpc/index.js +0 -19
  131. package/dist/command-bus/rpc/index.js.map +0 -1
  132. package/dist/command-bus/rpc/payload-compression.service.d.ts +0 -50
  133. package/dist/command-bus/rpc/payload-compression.service.js +0 -215
  134. package/dist/command-bus/rpc/payload-compression.service.js.map +0 -1
  135. package/dist/command-bus/rpc/payload-compression.service.spec.js +0 -376
  136. package/dist/command-bus/rpc/payload-compression.service.spec.js.map +0 -1
  137. package/dist/command-bus/rpc/rpc-coordinator.d.ts +0 -96
  138. package/dist/command-bus/rpc/rpc-coordinator.js +0 -500
  139. package/dist/command-bus/rpc/rpc-coordinator.js.map +0 -1
  140. package/dist/command-bus/rpc/rpc-coordinator.spec.js +0 -621
  141. package/dist/command-bus/rpc/rpc-coordinator.spec.js.map +0 -1
  142. package/dist/command-bus/rpc/rpc-job-cancellation.service.d.ts +0 -82
  143. package/dist/command-bus/rpc/rpc-job-cancellation.service.js +0 -180
  144. package/dist/command-bus/rpc/rpc-job-cancellation.service.js.map +0 -1
  145. package/dist/command-bus/rpc/rpc-job-cancellation.service.spec.js +0 -286
  146. package/dist/command-bus/rpc/rpc-job-cancellation.service.spec.js.map +0 -1
  147. package/dist/command-bus/worker/index.d.ts +0 -10
  148. package/dist/command-bus/worker/index.js +0 -19
  149. package/dist/command-bus/worker/index.js.map +0 -1
  150. package/dist/command-bus/worker/worker-benchmark.d.ts +0 -71
  151. package/dist/command-bus/worker/worker-benchmark.js +0 -202
  152. package/dist/command-bus/worker/worker-benchmark.js.map +0 -1
  153. package/dist/command-bus/worker/worker-benchmark.spec.js +0 -310
  154. package/dist/command-bus/worker/worker-benchmark.spec.js.map +0 -1
  155. package/dist/command-bus/worker/worker-metrics-collector.d.ts +0 -98
  156. package/dist/command-bus/worker/worker-metrics-collector.js +0 -242
  157. package/dist/command-bus/worker/worker-metrics-collector.js.map +0 -1
  158. package/dist/command-bus/worker/worker-orchestrator.d.ts +0 -70
  159. package/dist/command-bus/worker/worker-orchestrator.js +0 -339
  160. package/dist/command-bus/worker/worker-orchestrator.js.map +0 -1
  161. package/dist/command-bus/worker/worker-orchestrator.spec.js +0 -712
  162. package/dist/command-bus/worker/worker-orchestrator.spec.js.map +0 -1
  163. package/dist/examples/auto-config.demo.d.ts +0 -9
  164. package/dist/examples/auto-config.demo.js +0 -106
  165. package/dist/examples/auto-config.demo.js.map +0 -1
  166. package/dist/examples/rpc-compression.demo.d.ts +0 -5
  167. package/dist/examples/rpc-compression.demo.js +0 -358
  168. package/dist/examples/rpc-compression.demo.js.map +0 -1
  169. package/dist/examples/rpc-resilience.demo.d.ts +0 -15
  170. package/dist/examples/rpc-resilience.demo.js +0 -233
  171. package/dist/examples/rpc-resilience.demo.js.map +0 -1
  172. package/dist/pp-command-bus-1.5.0.tgz +0 -0
  173. package/dist/shared/config/base-config.d.ts +0 -54
  174. package/dist/shared/config/base-config.js +0 -114
  175. package/dist/shared/config/base-config.js.map +0 -1
  176. package/dist/shared/config/base-config.spec.js +0 -204
  177. package/dist/shared/config/base-config.spec.js.map +0 -1
  178. package/dist/shared/config/index.d.ts +0 -1
  179. package/dist/shared/config/index.js +0 -9
  180. package/dist/shared/config/index.js.map +0 -1
  181. package/dist/shared/redis/redis-connection-factory.d.ts +0 -66
  182. package/dist/shared/redis/redis-connection-factory.js +0 -113
  183. package/dist/shared/redis/redis-connection-factory.js.map +0 -1
  184. /package/dist/command-bus/{config/auto-config-optimizer.spec.d.ts → serialization/msgpack-serializer.spec.d.ts} +0 -0
  185. /package/dist/command-bus/{job/job-options-builder.spec.d.ts → transport/consumer-loop.spec.d.ts} +0 -0
  186. /package/dist/command-bus/{job/job-processor.spec.d.ts → transport/message-processor.spec.d.ts} +0 -0
  187. /package/dist/command-bus/{queue/queue-manager.spec.d.ts → transport/pending-recovery.spec.d.ts} +0 -0
  188. /package/dist/command-bus/{rpc/payload-compression.service.spec.d.ts → transport/redis-codec.spec.d.ts} +0 -0
  189. /package/dist/command-bus/{rpc/rpc-coordinator.spec.d.ts → transport/redis-streams-transport.spec.d.ts} +0 -0
  190. /package/dist/command-bus/{rpc/rpc-job-cancellation.service.spec.d.ts → transport/rpc-handler.spec.d.ts} +0 -0
  191. /package/dist/command-bus/{worker/worker-benchmark.spec.d.ts → transport/stream-consumer.spec.d.ts} +0 -0
  192. /package/dist/command-bus/{worker/worker-orchestrator.spec.d.ts → transport/stream-producer.spec.d.ts} +0 -0
  193. /package/dist/shared/{config/base-config.spec.d.ts → redis/connection-pool.spec.d.ts} +0 -0
@@ -1,98 +0,0 @@
1
- import type { ILogger } from '../../shared/types';
2
- /**
3
- * Rekomendacja zmiany concurrency
4
- */
5
- interface ConcurrencyRecommendation {
6
- shouldAdjust: boolean;
7
- direction: 'increase' | 'decrease' | 'none';
8
- reason: string;
9
- currentMetrics: {
10
- activeCount: number;
11
- completedCount: number;
12
- avgProcessingTimeMs: number;
13
- };
14
- }
15
- /**
16
- * Zbiera metryki workera w trybie event-driven
17
- * Resetuje dane co 1 minutę (okno profilowania)
18
- * Nie używa setInterval - bazuje na eventach jobów
19
- */
20
- export default class WorkerMetricsCollector {
21
- private readonly commandName;
22
- private readonly logger;
23
- private readonly onConcurrencyRecommendation?;
24
- /**
25
- * Długość okna profilowania w ms (1 minuta)
26
- */
27
- private readonly PROFILING_WINDOW_MS;
28
- /**
29
- * Timestamp rozpoczęcia aktualnego okna profilowania
30
- */
31
- private profilingWindowStartTime;
32
- /**
33
- * Licznik ukończonych jobów w aktualnym oknie
34
- */
35
- private completedCount;
36
- /**
37
- * Licznik nieudanych jobów w aktualnym oknie
38
- */
39
- private failedCount;
40
- /**
41
- * Licznik aktywnie przetwarzanych jobów (lokalny counter)
42
- */
43
- private activeCount;
44
- /**
45
- * Czasy przetwarzania jobów w aktualnym oknie (ms)
46
- */
47
- private processingTimes;
48
- /**
49
- * Historia średnich wartości activeCount dla analizy trendów
50
- * Zbierana przy każdym evencie completed/failed
51
- */
52
- private activeCountHistory;
53
- private readonly maxActiveCountHistory;
54
- constructor(commandName: string, logger: ILogger, onConcurrencyRecommendation?: ((recommendation: ConcurrencyRecommendation) => void) | undefined);
55
- /**
56
- * Wywołane gdy job zostaje aktywowany (rozpoczęty)
57
- * @param jobId - ID joba
58
- */
59
- onJobActive(jobId: string): void;
60
- /**
61
- * Wywołane gdy job zostaje ukończony pomyślnie
62
- * @param jobId - ID joba
63
- * @param processingTimeMs - Czas przetwarzania w ms
64
- */
65
- onJobCompleted(jobId: string, processingTimeMs: number): void;
66
- /**
67
- * Wywołane gdy job kończy się niepowodzeniem
68
- * @param jobId - ID joba
69
- */
70
- onJobFailed(jobId: string): void;
71
- /**
72
- * Sprawdza czy minęło okno profilowania (1 minuta)
73
- * Jeśli tak, loguje podsumowanie, analizuje i resetuje
74
- */
75
- private checkWindowReset;
76
- /**
77
- * Wyświetla podsumowanie okna, analizuje metryki i resetuje liczniki
78
- */
79
- private displayMetricsAndReset;
80
- /**
81
- * Oblicza statystyki dla aktualnego okna profilowania
82
- */
83
- private calculateWindowStats;
84
- /**
85
- * Analizuje metryki i rekomenduje zmianę concurrency
86
- * Bazuje na activeCount zamiast waiting (event-driven)
87
- */
88
- private analyzeAndRecommendConcurrency;
89
- /**
90
- * Zapisuje snapshot activeCount do historii dla analizy trendów
91
- */
92
- private recordActiveCountSnapshot;
93
- /**
94
- * Resetuje okno profilowania - czyści wszystkie zebrane metryki
95
- */
96
- private resetProfilingWindow;
97
- }
98
- export {};
@@ -1,242 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- /**
4
- * Zbiera metryki workera w trybie event-driven
5
- * Resetuje dane co 1 minutę (okno profilowania)
6
- * Nie używa setInterval - bazuje na eventach jobów
7
- */
8
- class WorkerMetricsCollector {
9
- constructor(commandName, logger, onConcurrencyRecommendation) {
10
- this.commandName = commandName;
11
- this.logger = logger;
12
- this.onConcurrencyRecommendation = onConcurrencyRecommendation;
13
- /**
14
- * Długość okna profilowania w ms (1 minuta)
15
- */
16
- this.PROFILING_WINDOW_MS = 60000;
17
- /**
18
- * Timestamp rozpoczęcia aktualnego okna profilowania
19
- */
20
- this.profilingWindowStartTime = 0;
21
- /**
22
- * Licznik ukończonych jobów w aktualnym oknie
23
- */
24
- this.completedCount = 0;
25
- /**
26
- * Licznik nieudanych jobów w aktualnym oknie
27
- */
28
- this.failedCount = 0;
29
- /**
30
- * Licznik aktywnie przetwarzanych jobów (lokalny counter)
31
- */
32
- this.activeCount = 0;
33
- /**
34
- * Czasy przetwarzania jobów w aktualnym oknie (ms)
35
- */
36
- this.processingTimes = [];
37
- /**
38
- * Historia średnich wartości activeCount dla analizy trendów
39
- * Zbierana przy każdym evencie completed/failed
40
- */
41
- this.activeCountHistory = [];
42
- this.maxActiveCountHistory = 30;
43
- // Zainicjuj okno profilowania
44
- this.profilingWindowStartTime = Date.now();
45
- }
46
- /**
47
- * Wywołane gdy job zostaje aktywowany (rozpoczęty)
48
- * @param jobId - ID joba
49
- */
50
- onJobActive(jobId) {
51
- this.activeCount++;
52
- this.logger.debug('Job aktywowany - metryki', {
53
- commandName: this.commandName,
54
- jobId,
55
- activeCount: this.activeCount,
56
- timestamp: new Date().toISOString(),
57
- });
58
- // Sprawdź czy minęło okno profilowania
59
- this.checkWindowReset();
60
- }
61
- /**
62
- * Wywołane gdy job zostaje ukończony pomyślnie
63
- * @param jobId - ID joba
64
- * @param processingTimeMs - Czas przetwarzania w ms
65
- */
66
- onJobCompleted(jobId, processingTimeMs) {
67
- this.activeCount--;
68
- this.completedCount++;
69
- this.processingTimes.push(processingTimeMs);
70
- // Zapisz snapshot activeCount dla analizy trendów
71
- this.recordActiveCountSnapshot();
72
- this.logger.debug('Job ukończony - metryki', {
73
- commandName: this.commandName,
74
- jobId,
75
- processingTimeMs,
76
- activeCount: this.activeCount,
77
- completedCount: this.completedCount,
78
- timestamp: new Date().toISOString(),
79
- });
80
- // Sprawdź czy minęło okno profilowania
81
- this.checkWindowReset();
82
- }
83
- /**
84
- * Wywołane gdy job kończy się niepowodzeniem
85
- * @param jobId - ID joba
86
- */
87
- onJobFailed(jobId) {
88
- this.activeCount--;
89
- this.failedCount++;
90
- // Zapisz snapshot activeCount dla analizy trendów
91
- this.recordActiveCountSnapshot();
92
- this.logger.debug('Job nieudany - metryki', {
93
- commandName: this.commandName,
94
- jobId,
95
- activeCount: this.activeCount,
96
- failedCount: this.failedCount,
97
- timestamp: new Date().toISOString(),
98
- });
99
- // Sprawdź czy minęło okno profilowania
100
- this.checkWindowReset();
101
- }
102
- /**
103
- * Sprawdza czy minęło okno profilowania (1 minuta)
104
- * Jeśli tak, loguje podsumowanie, analizuje i resetuje
105
- */
106
- checkWindowReset() {
107
- const windowElapsedMs = Date.now() - this.profilingWindowStartTime;
108
- if (windowElapsedMs >= this.PROFILING_WINDOW_MS) {
109
- this.displayMetricsAndReset();
110
- }
111
- }
112
- /**
113
- * Wyświetla podsumowanie okna, analizuje metryki i resetuje liczniki
114
- */
115
- displayMetricsAndReset() {
116
- const windowStats = this.calculateWindowStats();
117
- this.logger.log(`Podsumowanie okna profilowania ${this.commandName}`, windowStats);
118
- // Jeśli mamy wystarczająco danych (przynajmniej 5 jobów), wygeneruj rekomendację
119
- if (this.completedCount >= 5 && this.activeCountHistory.length >= 5) {
120
- const recommendation = this.analyzeAndRecommendConcurrency();
121
- this.logger.log(`Rekomendacja concurrency (koniec okna) ${this.commandName}`, recommendation);
122
- if (recommendation.shouldAdjust && this.onConcurrencyRecommendation) {
123
- this.onConcurrencyRecommendation(recommendation);
124
- }
125
- }
126
- else {
127
- this.logger.debug('Za mało danych dla rekomendacji concurrency', {
128
- commandName: this.commandName,
129
- completedCount: this.completedCount,
130
- activeCountHistorySize: this.activeCountHistory.length,
131
- timestamp: new Date().toISOString(),
132
- });
133
- }
134
- // Reset okna profilowania
135
- this.resetProfilingWindow();
136
- }
137
- /**
138
- * Oblicza statystyki dla aktualnego okna profilowania
139
- */
140
- calculateWindowStats() {
141
- const windowDurationMs = Date.now() - this.profilingWindowStartTime;
142
- const windowDurationSec = windowDurationMs / 1000;
143
- // Oblicz średni czas przetwarzania
144
- const avgProcessingTimeMs = this.processingTimes.length > 0
145
- ? this.processingTimes.reduce((sum, time) => sum + time, 0) / this.processingTimes.length
146
- : 0;
147
- // Oblicz przepustowość
148
- const jobsPerSecond = windowDurationSec > 0 ? this.completedCount / windowDurationSec : 0;
149
- const jobsPerMinute = jobsPerSecond * 60;
150
- return {
151
- commandName: this.commandName,
152
- windowDurationMs: Math.round(windowDurationMs),
153
- completedJobs: this.completedCount,
154
- failedJobs: this.failedCount,
155
- currentActiveJobs: this.activeCount,
156
- avgProcessingTimeMs: Math.round(avgProcessingTimeMs),
157
- jobsPerSecond: Number(jobsPerSecond.toFixed(1)),
158
- jobsPerMinute: Math.round(jobsPerMinute),
159
- timestamp: new Date().toISOString(),
160
- };
161
- }
162
- /**
163
- * Analizuje metryki i rekomenduje zmianę concurrency
164
- * Bazuje na activeCount zamiast waiting (event-driven)
165
- */
166
- analyzeAndRecommendConcurrency() {
167
- const currentMetrics = {
168
- activeCount: this.activeCount,
169
- completedCount: this.completedCount,
170
- avgProcessingTimeMs: Math.round(this.processingTimes.reduce((sum, time) => sum + time, 0) / this.processingTimes.length),
171
- };
172
- // Oblicz średnią activeCount z historii
173
- const avgActiveCount = this.activeCountHistory.reduce((sum, count) => sum + count, 0) /
174
- this.activeCountHistory.length;
175
- // Analiza trendu activeCount
176
- // Jeśli activeCount konsekwentnie wysoki → worker jest pod obciążeniem → zwiększ concurrency
177
- // Jeśli activeCount konsekwentnie niski → worker ma nadmiar → zmniejsz concurrency
178
- const highActiveThreshold = 0.8; // 80% historii z wysokim activeCount
179
- const lowActiveThreshold = 0.3; // 30% historii z niskim activeCount
180
- const highActiveRatio = this.activeCountHistory.filter((count) => count >= avgActiveCount * 0.9).length /
181
- this.activeCountHistory.length;
182
- const lowActiveRatio = this.activeCountHistory.filter((count) => count <= avgActiveCount * 0.3).length /
183
- this.activeCountHistory.length;
184
- // Warunki zwiększenia concurrency
185
- if (highActiveRatio >= highActiveThreshold && avgActiveCount >= 5) {
186
- return {
187
- shouldAdjust: true,
188
- direction: 'increase',
189
- reason: `Wysoki activeCount: średnia=${avgActiveCount.toFixed(1)}, ratio=${(highActiveRatio * 100).toFixed(0)}%`,
190
- currentMetrics,
191
- };
192
- }
193
- // Warunki zmniejszenia concurrency
194
- if (lowActiveRatio >= lowActiveThreshold && avgActiveCount >= 1) {
195
- return {
196
- shouldAdjust: true,
197
- direction: 'decrease',
198
- reason: `Niski activeCount: średnia=${avgActiveCount.toFixed(1)}, ratio=${(lowActiveRatio * 100).toFixed(0)}%`,
199
- currentMetrics,
200
- };
201
- }
202
- // Stabilna sytuacja - brak rekomendacji
203
- return {
204
- shouldAdjust: false,
205
- direction: 'none',
206
- reason: `Stabilne activeCount: średnia=${avgActiveCount.toFixed(1)}`,
207
- currentMetrics,
208
- };
209
- }
210
- /**
211
- * Zapisuje snapshot activeCount do historii dla analizy trendów
212
- */
213
- recordActiveCountSnapshot() {
214
- this.activeCountHistory.push(this.activeCount);
215
- // Ogranicz rozmiar historii
216
- if (this.activeCountHistory.length > this.maxActiveCountHistory) {
217
- this.activeCountHistory.shift();
218
- }
219
- }
220
- /**
221
- * Resetuje okno profilowania - czyści wszystkie zebrane metryki
222
- */
223
- resetProfilingWindow() {
224
- this.logger.log(`Reset okna profilowania ${this.commandName}`, {
225
- commandName: this.commandName,
226
- windowDurationMs: Date.now() - this.profilingWindowStartTime,
227
- completedJobs: this.completedCount,
228
- failedJobs: this.failedCount,
229
- timestamp: new Date().toISOString(),
230
- });
231
- // Reset liczników
232
- this.completedCount = 0;
233
- this.failedCount = 0;
234
- this.processingTimes = [];
235
- this.activeCountHistory = [];
236
- // activeCount NIE resetujemy - to bieżąca liczba aktywnych jobów
237
- // Rozpocznij nowe okno
238
- this.profilingWindowStartTime = Date.now();
239
- }
240
- }
241
- exports.default = WorkerMetricsCollector;
242
- //# sourceMappingURL=worker-metrics-collector.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"worker-metrics-collector.js","sourceRoot":"","sources":["../../../src/command-bus/worker/worker-metrics-collector.ts"],"names":[],"mappings":";;AAgBA;;;;GAIG;AACH,MAAqB,sBAAsB;IAsCzC,YACmB,WAAmB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;OAEG;IACK,yBAAyB;QAC/B,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAE/C,4BAA4B;QAC5B,IAAI,IAAI,CAAC,kBAAkB,CAAC,MAAM,GAAG,IAAI,CAAC,qBAAqB,EAAE,CAAC;YAChE,IAAI,CAAC,kBAAkB,CAAC,KAAK,EAAE,CAAC;QAClC,CAAC;IACH,CAAC;IAED;;OAEG;IACK,oBAAoB;QAC1B,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,2BAA2B,IAAI,CAAC,WAAW,EAAE,EAAE;YAC7D,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,gBAAgB,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,wBAAwB;YAC5D,aAAa,EAAE,IAAI,CAAC,cAAc;YAClC,UAAU,EAAE,IAAI,CAAC,WAAW;YAC5B,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;SACpC,CAAC,CAAC;QAEH,kBAAkB;QAClB,IAAI,CAAC,cAAc,GAAG,CAAC,CAAC;QACxB,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC;QACrB,IAAI,CAAC,eAAe,GAAG,EAAE,CAAC;QAC1B,IAAI,CAAC,kBAAkB,GAAG,EAAE,CAAC;QAC7B,iEAAiE;QAEjE,uBAAuB;QACvB,IAAI,CAAC,wBAAwB,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAC7C,CAAC;CACF;AArSD,yCAqSC"}
@@ -1,70 +0,0 @@
1
- import { Worker } from 'bullmq';
2
- import type { Redis } from 'ioredis';
3
- import type { ILogger } from '../../shared/types';
4
- import type JobProcessor from '../job/job-processor';
5
- /**
6
- * Zarządza workerami BullMQ dla obsługi komend
7
- */
8
- export default class WorkerOrchestrator {
9
- private readonly redisConnection;
10
- private readonly jobProcessor;
11
- private readonly defaultConcurrency;
12
- private readonly maxAttempts;
13
- private readonly logger;
14
- /**
15
- * Workery BullMQ dla obsługi komend
16
- */
17
- private workers;
18
- /**
19
- * Collectory metryk dla każdego workera
20
- */
21
- private metricsCollectors;
22
- /**
23
- * Timestamp ostatniej zmiany concurrency per-worker (dla cooldown 30s)
24
- */
25
- private lastConcurrencyChange;
26
- /**
27
- * Cooldown między zmianami concurrency (30s)
28
- */
29
- private readonly concurrencyCooldownMs;
30
- /**
31
- * Współczynnik zwiększenia concurrency (+20%)
32
- */
33
- private readonly CONCURRENCY_INCREASE_FACTOR;
34
- /**
35
- * Współczynnik zmniejszenia concurrency (-20%)
36
- */
37
- private readonly CONCURRENCY_DECREASE_FACTOR;
38
- constructor(redisConnection: Redis, jobProcessor: JobProcessor, defaultConcurrency: number, maxAttempts: number, logger: ILogger);
39
- /**
40
- * Rejestruje handler dla komendy i tworzy workera
41
- * @param commandName - Nazwa komendy
42
- */
43
- registerWorker(commandName: string): void;
44
- /**
45
- * Konfiguruje event handlery dla workera
46
- */
47
- private setupWorkerEventHandlers;
48
- /**
49
- * Pobiera workera dla danego typu komendy
50
- * @param commandName - Nazwa komendy
51
- */
52
- getWorker(commandName: string): Worker | undefined;
53
- /**
54
- * Zamyka wszystkie workery i czyści collectory metryk
55
- */
56
- closeAll(): Promise<void>;
57
- /**
58
- * Dynamicznie dostosowuje concurrency workera na podstawie metryk
59
- * Zmiana: +/-20% za każdym razem, z cooldown 30s
60
- * Limity: min 10, max 2000
61
- */
62
- adjustConcurrency(commandName: string, direction: 'increase' | 'decrease', reason: string): void;
63
- /**
64
- * Zwraca statystyki workerów (dla diagnostyki)
65
- */
66
- getWorkerStats(): {
67
- activeWorkersCount: number;
68
- workerNames: string[];
69
- };
70
- }