@cap-js-community/event-queue 0.1.49
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.
- package/LICENSE +201 -0
- package/README.md +160 -0
- package/cds-plugin.js +11 -0
- package/db/Event.cds +24 -0
- package/db/Lock.cds +10 -0
- package/db/index.cds +2 -0
- package/index.cds +1 -0
- package/package.json +55 -0
- package/src/EventQueueError.js +141 -0
- package/src/EventQueueProcessorBase.js +922 -0
- package/src/config.js +196 -0
- package/src/constants.js +11 -0
- package/src/dbHandler.js +40 -0
- package/src/index.js +20 -0
- package/src/initialize.js +216 -0
- package/src/processEventQueue.js +297 -0
- package/src/publishEvent.js +25 -0
- package/src/redisPubSub.js +143 -0
- package/src/runner.js +237 -0
- package/src/shared/PerformanceTracer.js +74 -0
- package/src/shared/WorkerQueue.js +78 -0
- package/src/shared/cdsHelper.js +136 -0
- package/src/shared/common.js +115 -0
- package/src/shared/distributedLock.js +191 -0
- package/src/shared/env.js +9 -0
- package/src/shared/redis.js +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# @cap-js-community/event-queue
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@cap-js-community/event-queue)
|
|
4
|
+
[](https://www.npmjs.com/package/@cap-js-community/event-queue)
|
|
5
|
+
[](https://api.reuse.software/info/github.com/cap-js-community/event-queue)
|
|
6
|
+
[](https://github.com/cap-js-community/event-queue/commits/main)
|
|
7
|
+
|
|
8
|
+
The Event-Queue is a framework built on top of CAP Node.js, specifically designed to enable efficient and streamlined
|
|
9
|
+
asynchronous event processing in a multi-tenancy environment. With a strong emphasis on load balancing, this package
|
|
10
|
+
ensures optimal distribution of workload across all available application instances. By offering managed tenant-specific
|
|
11
|
+
transactions, similar to CAP handlers, the Event-Queue framework simplifies event and asynchronous processing, thereby
|
|
12
|
+
enhancing the overall performance of your application.
|
|
13
|
+
|
|
14
|
+
## Getting started
|
|
15
|
+
|
|
16
|
+
- Run `npm add @cap-community/event-queue` in `@sap/cds` project
|
|
17
|
+
- Initialize the event queue for example in the CAP server.js
|
|
18
|
+
- Enhance or create `./srv/server.js`:
|
|
19
|
+
|
|
20
|
+
### By Code
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
const cds = require("@sap/cds");
|
|
24
|
+
const eventQueue = require("@sap/cds-event-queue");
|
|
25
|
+
|
|
26
|
+
cds.on("bootstrap", () => {
|
|
27
|
+
eventQueue.initialize({
|
|
28
|
+
configFilePath: "./srv/eventConfig.yml",
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
module.exports = cds.server;
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### As cds-plugin
|
|
36
|
+
|
|
37
|
+
Extend the cds section of your package.json. Reference to the cds-plugin section in the capire documentation about the
|
|
38
|
+
cds-plugin concept.
|
|
39
|
+
https://cap.cloud.sap/docs/releases/march23#new-cds-plugin-technique
|
|
40
|
+
|
|
41
|
+
```json
|
|
42
|
+
{
|
|
43
|
+
"cds": {
|
|
44
|
+
"eventQueue": {
|
|
45
|
+
"plugin": true,
|
|
46
|
+
"configFilePath": "./srv/eventQueueConfig.yml"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Features
|
|
53
|
+
|
|
54
|
+
- load balancing of event processing throughout app instances
|
|
55
|
+
- managed transactions for event processing
|
|
56
|
+
- async processing of processing intensive tasks for better UI responsiveness
|
|
57
|
+
- push/pull mechanism for reducing delay between publish an event and processing
|
|
58
|
+
- cluster published events during processing (e.g. for combining multiple E-Mail events to one E-Mail)
|
|
59
|
+
|
|
60
|
+
## Initialize event queue configuration
|
|
61
|
+
|
|
62
|
+
| Property | Description |
|
|
63
|
+
| ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
64
|
+
| configFilePath | Filepath as a string for the event configuration file. The base path is the root directory of the project. |
|
|
65
|
+
| registerAsEventProcessor | Allows enabling/disabling the registration of the app instance as an event processor. The interval is based on the `runInterval` parameter. Based on this interval, all events for all tenants will be processed. The default value is `true`. |
|
|
66
|
+
| runInterval | The interval specified in seconds, indicating how often events are processed for all tenants. If `processEventsAfterPublish` is true, in most situations, only erroneous events are processed during this run. All other events are automatically processed after they have been published (see the parameter `processEventsAfterPublish`). |
|
|
67
|
+
| processEventsAfterPublish | Allows enabling/disabling the automatic processing of events after they have been published. The behavior depends on whether a Redis service is bound to the app. With Redis, the processing will happen on any available app instance. If Redis is not bound, the processing will happen on the same instance where the event was published. |
|
|
68
|
+
| tableNameEventQueue | Allows 'Bring your own table'. This is the name of the event queue table. The required fields for this table can be found in the db-folder. |
|
|
69
|
+
| tableNameEventLock | Allows 'Bring your own table'. This is the name of the event lock table. The required fields for this table can be found in the db-folder. |
|
|
70
|
+
| skipCsnCheck | Specifies whether to skip the CSN check. This might be useful for testing purposes. |
|
|
71
|
+
| parallelTenantProcessing | Specifies the limit as an integer on how many tenants are processed on a given app-instance in parallel. The default is 5. |
|
|
72
|
+
|
|
73
|
+
## Persistence
|
|
74
|
+
|
|
75
|
+
This library needs two tables two work as designed. The event tables contains the data and state about the events for
|
|
76
|
+
processing.
|
|
77
|
+
The second table is for keeping track of semantic locks. This table is used if no redis-instance is available.
|
|
78
|
+
There are two options for getting the required tables into the project.
|
|
79
|
+
|
|
80
|
+
### Use provided tables
|
|
81
|
+
|
|
82
|
+
Use the tables provided by this library. For that add the following to `package.json` of the project:
|
|
83
|
+
|
|
84
|
+
```json
|
|
85
|
+
{
|
|
86
|
+
"cds": {
|
|
87
|
+
"requires": {
|
|
88
|
+
"cds-event-queue": {
|
|
89
|
+
"model": "@sap/cds-event-queue"
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Bring your own persistence
|
|
97
|
+
|
|
98
|
+
The table names can be specified during the initialization of the event queue. It's important that the provided tables
|
|
99
|
+
have the same named fields and types as the default tables.
|
|
100
|
+
|
|
101
|
+
Own tables can have additional fields but need to meet the minimal requirements. During initialization, the CDS CSN for
|
|
102
|
+
the provided table names is checked to ensure they meet the requirements.
|
|
103
|
+
If you want to skip this check, you can achieve it by using skipCsnCheck (refer to the "Initialize Event Queue
|
|
104
|
+
Configuration" section).
|
|
105
|
+
|
|
106
|
+
```json
|
|
107
|
+
{
|
|
108
|
+
"cds": {
|
|
109
|
+
"eventQueue": {
|
|
110
|
+
"plugin": true,
|
|
111
|
+
"configFilePath": "./srv/eventQueueConfig.yml",
|
|
112
|
+
"tableNameEventQueue": "sap.custom.EventQueue",
|
|
113
|
+
"tableNameEventLock": "sap.custom.EventLock"
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Configure your events
|
|
120
|
+
|
|
121
|
+
Events are configured in a configuration yml file. In this file all relevant information about how events should be
|
|
122
|
+
processed can be maintained.
|
|
123
|
+
|
|
124
|
+
```yaml
|
|
125
|
+
events:
|
|
126
|
+
- type: Notifications
|
|
127
|
+
subType: Email
|
|
128
|
+
impl: ./test/asset/EventQueueTest
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Event Configurations
|
|
132
|
+
|
|
133
|
+
| Property | Description |
|
|
134
|
+
| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
135
|
+
| retryAttempts | For infinite retries, maintain -1 as 'retryAttempts'. Default retry attempts is 3. |
|
|
136
|
+
| parallelEventProcessing | How many events of the same type and subType are parallel processed after clustering. Default value is 1 and limit is 10. |
|
|
137
|
+
| eventOutdatedCheck | Checks if the db record for the event has been modified since the selection and right before the processing of the event. Default is true. |
|
|
138
|
+
| commitOnEventLevel | After processing an event, the associated transaction is committed and the associated status is committed with the same transaction. This should be used if events should be processed atomically. Default is false. |
|
|
139
|
+
| selectMaxChunkSize | Number of events which are selected at once. Default is 100. If it should be checked if there are more open events available, set the parameter checkForNextChunk to true. |
|
|
140
|
+
| checkForNextChunk | Determines if after processing a chunk (the size depends on the value of selectMaxChunkSize), a next chunk is being processed if there are more open events and the processing time has not already exceeded 5 minutes. Default is false. |
|
|
141
|
+
|
|
142
|
+
## Support, Feedback, Contributing
|
|
143
|
+
|
|
144
|
+
This project is open to feature requests/suggestions, bug reports etc.
|
|
145
|
+
via [GitHub issues](https://github.com/cap-js-community/<your-project>/issues). Contribution and feedback are encouraged
|
|
146
|
+
and always welcome. For more information about how to contribute, the project structure, as well as additional
|
|
147
|
+
contribution information, see our [Contribution Guidelines](CONTRIBUTING.md).
|
|
148
|
+
|
|
149
|
+
## Code of Conduct
|
|
150
|
+
|
|
151
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for
|
|
152
|
+
everyone. By participating in this project, you agree to abide by its [Code of Conduct](CODE_OF_CONDUCT.md) at all
|
|
153
|
+
times.
|
|
154
|
+
|
|
155
|
+
## Licensing
|
|
156
|
+
|
|
157
|
+
Copyright 2023 SAP SE or an SAP affiliate company and `@cap-js-community/event-queue contributors`. Please see
|
|
158
|
+
our [LICENSE](LICENSE) for copyright and license information. Detailed information including third-party components and
|
|
159
|
+
their licensing/copyright information is
|
|
160
|
+
available [via the REUSE tool](https://api.reuse.software/info/github.com/cap-js-community/<your-project>).
|
package/cds-plugin.js
ADDED
package/db/Event.cds
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
namespace sap.eventqueue;
|
|
2
|
+
|
|
3
|
+
using cuid from '@sap/cds/common';
|
|
4
|
+
|
|
5
|
+
@sap.value.list: 'fixed-values'
|
|
6
|
+
type Status: Integer enum {
|
|
7
|
+
Open = 0;
|
|
8
|
+
InProgress = 1;
|
|
9
|
+
Done = 2;
|
|
10
|
+
Error = 3;
|
|
11
|
+
Exceeded = 4;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
entity Event: cuid {
|
|
15
|
+
type: String not null;
|
|
16
|
+
subType: String not null;
|
|
17
|
+
referenceEntity: String;
|
|
18
|
+
referenceEntityKey: UUID;
|
|
19
|
+
status: Status default 0 not null;
|
|
20
|
+
payload: LargeString;
|
|
21
|
+
attempts: Integer default 0 not null;
|
|
22
|
+
lastAttemptTimestamp: Timestamp;
|
|
23
|
+
createdAt: Timestamp @cds.on.insert : $now;
|
|
24
|
+
}
|
package/db/Lock.cds
ADDED
package/db/index.cds
ADDED
package/index.cds
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
using from './db';
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cap-js-community/event-queue",
|
|
3
|
+
"version": "0.1.49",
|
|
4
|
+
"description": "event queue for cds",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"src",
|
|
8
|
+
"db",
|
|
9
|
+
"cds-plugin.js",
|
|
10
|
+
"index.cds"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test:unit": "jest --testPathIgnorePatterns=\"/test-integration/\"",
|
|
14
|
+
"test:integration": "jest --testPathIgnorePatterns=\"/test/\" --runInBand --forceExit",
|
|
15
|
+
"test:all": "npm run test:unit && npm run test:integration",
|
|
16
|
+
"test:ci": "npm run test:prepare && npm run test:all",
|
|
17
|
+
"test:all:coverage": "jest --runInBand --forceExit --collect-coverage",
|
|
18
|
+
"test:prepare": "npm run build:ci --prefix=./test-integration/_env",
|
|
19
|
+
"lint": "npm run eslint && npm run prettier",
|
|
20
|
+
"lint:ci": "npm run eslint:ci && npm run prettier:ci",
|
|
21
|
+
"eslint": "eslint --fix .",
|
|
22
|
+
"eslint:ci": "eslint .",
|
|
23
|
+
"prettier": "prettier --write --loglevel error .",
|
|
24
|
+
"prettier:ci": "prettier --check .",
|
|
25
|
+
"prepareRelease": "npm prune --production",
|
|
26
|
+
"docs": "cd docs && bundle exec jekyll serve",
|
|
27
|
+
"docs:install": "cd docs && npx shx rm -rf vendor Gemfile.lock && bundle install",
|
|
28
|
+
"upgrade-lock": "npx shx rm -rf package-lock.json node_modules && npm i --package-lock"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=16"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"uuid": "9.0.0",
|
|
35
|
+
"redis": "4.6.7",
|
|
36
|
+
"verror": "1.10.1",
|
|
37
|
+
"yaml": "2.3.1"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@sap/eslint-plugin-cds": "2.6.3",
|
|
41
|
+
"@sap/cds-dk": "7.0.0",
|
|
42
|
+
"hdb": "0.19.5",
|
|
43
|
+
"eslint-plugin-node": "11.1.0",
|
|
44
|
+
"sqlite3": "5.1.6",
|
|
45
|
+
"express": "4.18.2",
|
|
46
|
+
"@sap/cds": "7.0.0",
|
|
47
|
+
"eslint": "^8.39.0",
|
|
48
|
+
"eslint-config-prettier": "^8.8.0",
|
|
49
|
+
"eslint-plugin-jest": "^27.2.1",
|
|
50
|
+
"jest": "^29.5.0",
|
|
51
|
+
"prettier": "^2.8.8"
|
|
52
|
+
},
|
|
53
|
+
"author": "Maximilian Gruenfelder <maximilian.gruenfelder@sap.com>",
|
|
54
|
+
"license": "Apache-2.0"
|
|
55
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const VError = require("verror");
|
|
4
|
+
|
|
5
|
+
const ERROR_CODES = {
|
|
6
|
+
WRONG_TX_USAGE: "WRONG_TX_USAGE",
|
|
7
|
+
UNKNOWN_EVENT_TYPE: "UNKNOWN_EVENT_TYPE",
|
|
8
|
+
NOT_INITIALIZED: "NOT_INITIALIZED",
|
|
9
|
+
REDIS_CREATE_CLIENT: "REDIS_CREATE_CLIENT",
|
|
10
|
+
REDIS_LOCAL_NO_RECONNECT: "REDIS_LOCAL_NO_RECONNECT",
|
|
11
|
+
MISSING_TABLE_DEFINITION: "MISSING_TABLE_DEFINITION",
|
|
12
|
+
MISSING_ELEMENT_IN_TABLE: "MISSING_ELEMENT_IN_TABLE",
|
|
13
|
+
TYPE_MISMATCH_TABLE: "TYPE_MISMATCH_TABLE",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const ERROR_CODES_META = {
|
|
17
|
+
[ERROR_CODES.WRONG_TX_USAGE]: {
|
|
18
|
+
message:
|
|
19
|
+
"Usage of this.tx|this.context is not allowed if parallel event processing is enabled",
|
|
20
|
+
},
|
|
21
|
+
[ERROR_CODES.UNKNOWN_EVENT_TYPE]: {
|
|
22
|
+
message:
|
|
23
|
+
"The event type and subType configuration is not configured! Maintain the combination in the config file.",
|
|
24
|
+
},
|
|
25
|
+
[ERROR_CODES.NOT_INITIALIZED]: {
|
|
26
|
+
message:
|
|
27
|
+
"The event-queue is not initialized yet. The initialization needs to be completed before the package is used.",
|
|
28
|
+
},
|
|
29
|
+
[ERROR_CODES.REDIS_CREATE_CLIENT]: {
|
|
30
|
+
message: "error during create client with redis-cache service",
|
|
31
|
+
},
|
|
32
|
+
[ERROR_CODES.REDIS_LOCAL_NO_RECONNECT]: {
|
|
33
|
+
message: "disabled reconnect, because we are not running on cloud foundry",
|
|
34
|
+
},
|
|
35
|
+
[ERROR_CODES.MISSING_TABLE_DEFINITION]: {
|
|
36
|
+
message:
|
|
37
|
+
"Could not find table in csn. Make sure the provided table name is correct and the table is known by CDS.",
|
|
38
|
+
},
|
|
39
|
+
[ERROR_CODES.MISSING_ELEMENT_IN_TABLE]: {
|
|
40
|
+
message:
|
|
41
|
+
"The provided table doesn't match the required structure. At least the following element is missing.",
|
|
42
|
+
},
|
|
43
|
+
[ERROR_CODES.TYPE_MISMATCH_TABLE]: {
|
|
44
|
+
message:
|
|
45
|
+
"At least one field in the provided table doesn't have the expected data type.",
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
class EventQueueError extends VError {
|
|
50
|
+
constructor(...args) {
|
|
51
|
+
super(...args);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static wrongTxUsage(type, subType) {
|
|
55
|
+
const { message } = ERROR_CODES_META[ERROR_CODES.WRONG_TX_USAGE];
|
|
56
|
+
return new EventQueueError(
|
|
57
|
+
{
|
|
58
|
+
name: ERROR_CODES.WRONG_TX_USAGE,
|
|
59
|
+
info: { type, subType },
|
|
60
|
+
},
|
|
61
|
+
message
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static unknownEventType(type, subType) {
|
|
66
|
+
const { message } = ERROR_CODES_META[ERROR_CODES.UNKNOWN_EVENT_TYPE];
|
|
67
|
+
return new EventQueueError(
|
|
68
|
+
{
|
|
69
|
+
name: ERROR_CODES.UNKNOWN_EVENT_TYPE,
|
|
70
|
+
info: { type, subType },
|
|
71
|
+
},
|
|
72
|
+
message
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static notInitialized() {
|
|
77
|
+
const { message } = ERROR_CODES_META[ERROR_CODES.NOT_INITIALIZED];
|
|
78
|
+
return new EventQueueError(
|
|
79
|
+
{
|
|
80
|
+
name: ERROR_CODES.NOT_INITIALIZED,
|
|
81
|
+
},
|
|
82
|
+
message
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static redisConnectionFailure(err) {
|
|
87
|
+
const { message } = ERROR_CODES_META[ERROR_CODES.REDIS_CREATE_CLIENT];
|
|
88
|
+
return new EventQueueError(
|
|
89
|
+
{
|
|
90
|
+
name: ERROR_CODES.REDIS_CREATE_CLIENT,
|
|
91
|
+
cause: err,
|
|
92
|
+
},
|
|
93
|
+
message
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
static redisNoReconnect() {
|
|
98
|
+
const { message } = ERROR_CODES_META[ERROR_CODES.REDIS_LOCAL_NO_RECONNECT];
|
|
99
|
+
return new EventQueueError(
|
|
100
|
+
{
|
|
101
|
+
name: ERROR_CODES.REDIS_LOCAL_NO_RECONNECT,
|
|
102
|
+
},
|
|
103
|
+
message
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
static missingTableInCsn(tableName) {
|
|
108
|
+
const { message } = ERROR_CODES_META[ERROR_CODES.MISSING_TABLE_DEFINITION];
|
|
109
|
+
return new EventQueueError(
|
|
110
|
+
{
|
|
111
|
+
name: ERROR_CODES.MISSING_TABLE_DEFINITION,
|
|
112
|
+
info: { tableName },
|
|
113
|
+
},
|
|
114
|
+
message
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
static missingElementInTable(tableName, elementName) {
|
|
119
|
+
const { message } = ERROR_CODES_META[ERROR_CODES.MISSING_ELEMENT_IN_TABLE];
|
|
120
|
+
return new EventQueueError(
|
|
121
|
+
{
|
|
122
|
+
name: ERROR_CODES.MISSING_ELEMENT_IN_TABLE,
|
|
123
|
+
info: { tableName, elementName },
|
|
124
|
+
},
|
|
125
|
+
message
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
static typeMismatchInTable(tableName, elementName) {
|
|
130
|
+
const { message } = ERROR_CODES_META[ERROR_CODES.TYPE_MISMATCH_TABLE];
|
|
131
|
+
return new EventQueueError(
|
|
132
|
+
{
|
|
133
|
+
name: ERROR_CODES.TYPE_MISMATCH_TABLE,
|
|
134
|
+
info: { tableName, elementName },
|
|
135
|
+
},
|
|
136
|
+
message
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
module.exports = EventQueueError;
|