@hazeljs/cron 0.2.0-beta.8 → 0.2.0-beta.80
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 +192 -21
- package/README.md +7 -6
- package/dist/cron.service.js +1 -1
- package/dist/cron.test.d.ts +2 -0
- package/dist/cron.test.d.ts.map +1 -0
- package/dist/cron.test.js +401 -0
- package/package.json +11 -5
package/LICENSE
CHANGED
|
@@ -1,21 +1,192 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
Copyright 2024 HazelJS Team
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
22
|
+
|
|
23
|
+
1. Definitions.
|
|
24
|
+
|
|
25
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
26
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
27
|
+
|
|
28
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
29
|
+
the copyright owner that is granting the License.
|
|
30
|
+
|
|
31
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
32
|
+
other entities that control, are controlled by, or are under common
|
|
33
|
+
control with that entity. For the purposes of this definition,
|
|
34
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
35
|
+
direction or management of such entity, whether by contract or
|
|
36
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
37
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
38
|
+
|
|
39
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
40
|
+
exercising permissions granted by this License.
|
|
41
|
+
|
|
42
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
43
|
+
including but not limited to software source code, documentation
|
|
44
|
+
source, and configuration files.
|
|
45
|
+
|
|
46
|
+
"Object" form shall mean any form resulting from mechanical
|
|
47
|
+
transformation or translation of a Source form, including but
|
|
48
|
+
not limited to compiled object code, generated documentation,
|
|
49
|
+
and conversions to other media types.
|
|
50
|
+
|
|
51
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
52
|
+
Object form, made available under the License, as indicated by a
|
|
53
|
+
copyright notice that is included in or attached to the work
|
|
54
|
+
(an example is provided in the Appendix below).
|
|
55
|
+
|
|
56
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
57
|
+
form, that is based on (or derived from) the Work and for which the
|
|
58
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
59
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
60
|
+
of this License, Derivative Works shall not include works that remain
|
|
61
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
62
|
+
the Work and Derivative Works thereof.
|
|
63
|
+
|
|
64
|
+
"Contribution" shall mean any work of authorship, including
|
|
65
|
+
the original version of the Work and any modifications or additions
|
|
66
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
67
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
68
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
69
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
70
|
+
means any form of electronic, verbal, or written communication sent
|
|
71
|
+
to the Licensor or its representatives, including but not limited to
|
|
72
|
+
communication on electronic mailing lists, source code control systems,
|
|
73
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
74
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
75
|
+
excluding communication that is conspicuously marked or otherwise
|
|
76
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
77
|
+
|
|
78
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
79
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
80
|
+
subsequently incorporated within the Work.
|
|
81
|
+
|
|
82
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
83
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
84
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
85
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
86
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
87
|
+
Work and such Derivative Works in Source or Object form.
|
|
88
|
+
|
|
89
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
90
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
91
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
92
|
+
(except as stated in this section) patent license to make, have made,
|
|
93
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
94
|
+
where such license applies only to those patent claims licensable
|
|
95
|
+
by such Contributor that are necessarily infringed by their
|
|
96
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
97
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
98
|
+
institute patent litigation against any entity (including a
|
|
99
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
100
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
101
|
+
or contributory patent infringement, then any patent licenses
|
|
102
|
+
granted to You under this License for that Work shall terminate
|
|
103
|
+
as of the date such litigation is filed.
|
|
104
|
+
|
|
105
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
106
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
107
|
+
modifications, and in Source or Object form, provided that You
|
|
108
|
+
meet the following conditions:
|
|
109
|
+
|
|
110
|
+
(a) You must give any other recipients of the Work or
|
|
111
|
+
Derivative Works a copy of this License; and
|
|
112
|
+
|
|
113
|
+
(b) You must cause any modified files to carry prominent notices
|
|
114
|
+
stating that You changed the files; and
|
|
115
|
+
|
|
116
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
117
|
+
that You distribute, all copyright, patent, trademark, and
|
|
118
|
+
attribution notices from the Source form of the Work,
|
|
119
|
+
excluding those notices that do not pertain to any part of
|
|
120
|
+
the Derivative Works; and
|
|
121
|
+
|
|
122
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
123
|
+
distribution, then any Derivative Works that You distribute must
|
|
124
|
+
include a readable copy of the attribution notices contained
|
|
125
|
+
within such NOTICE file, excluding those notices that do not
|
|
126
|
+
pertain to any part of the Derivative Works, in at least one
|
|
127
|
+
of the following places: within a NOTICE text file distributed
|
|
128
|
+
as part of the Derivative Works; within the Source form or
|
|
129
|
+
documentation, if provided along with the Derivative Works; or,
|
|
130
|
+
within a display generated by the Derivative Works, if and
|
|
131
|
+
wherever such third-party notices normally appear. The contents
|
|
132
|
+
of the NOTICE file are for informational purposes only and
|
|
133
|
+
do not modify the License. You may add Your own attribution
|
|
134
|
+
notices within Derivative Works that You distribute, alongside
|
|
135
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
136
|
+
that such additional attribution notices cannot be construed
|
|
137
|
+
as modifying the License.
|
|
138
|
+
|
|
139
|
+
You may add Your own copyright statement to Your modifications and
|
|
140
|
+
may provide additional or different license terms and conditions
|
|
141
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
142
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
143
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
144
|
+
the conditions stated in this License.
|
|
145
|
+
|
|
146
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
147
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
148
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
149
|
+
this License, without any additional terms or conditions.
|
|
150
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
151
|
+
the terms of any separate license agreement you may have executed
|
|
152
|
+
with Licensor regarding such Contributions.
|
|
153
|
+
|
|
154
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
155
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
156
|
+
except as required for reasonable and customary use in describing the
|
|
157
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
158
|
+
|
|
159
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
160
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
161
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
162
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
163
|
+
implied, including, without limitation, any warranties or conditions
|
|
164
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
165
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
166
|
+
appropriateness of using or redistributing the Work and assume any
|
|
167
|
+
risks associated with Your exercise of permissions under this License.
|
|
168
|
+
|
|
169
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
170
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
171
|
+
unless required by applicable law (such as deliberate and grossly
|
|
172
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
173
|
+
liable to You for damages, including any direct, indirect, special,
|
|
174
|
+
incidental, or consequential damages of any character arising as a
|
|
175
|
+
result of this License or out of the use or inability to use the
|
|
176
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
177
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
178
|
+
other commercial damages or losses), even if such Contributor
|
|
179
|
+
has been advised of the possibility of such damages.
|
|
180
|
+
|
|
181
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
182
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
183
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
184
|
+
or other liability obligations and/or rights consistent with this
|
|
185
|
+
License. However, in accepting such obligations, You may act only
|
|
186
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
187
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
188
|
+
defend, and hold each Contributor harmless for any liability
|
|
189
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
190
|
+
of your accepting any such warranty or additional liability.
|
|
191
|
+
|
|
192
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
# @hazeljs/cron
|
|
2
2
|
|
|
3
|
-
**
|
|
3
|
+
**Scheduled tasks as decorators. No `setInterval` hacks.**
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
`@Cron('0 9 * * *')` for daily at 9am. `@Interval(60000)` for every minute. Real cron expressions, timezone support, overlap prevention. Background jobs that actually run when you expect.
|
|
6
6
|
|
|
7
7
|
[](https://www.npmjs.com/package/@hazeljs/cron)
|
|
8
|
-
[](https://www.npmjs.com/package/@hazeljs/cron)
|
|
9
|
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
|
9
10
|
|
|
10
11
|
## Features
|
|
11
12
|
|
|
@@ -431,11 +432,11 @@ Contributions are welcome! Please read our [Contributing Guide](../../CONTRIBUTI
|
|
|
431
432
|
|
|
432
433
|
## License
|
|
433
434
|
|
|
434
|
-
|
|
435
|
+
Apache 2.0 © [HazelJS](https://hazeljs.com)
|
|
435
436
|
|
|
436
437
|
## Links
|
|
437
438
|
|
|
438
439
|
- [Documentation](https://hazeljs.com/docs/packages/cron)
|
|
439
440
|
- [GitHub](https://github.com/hazel-js/hazeljs)
|
|
440
|
-
- [Issues](https://github.com/
|
|
441
|
-
- [Discord](https://discord.
|
|
441
|
+
- [Issues](https://github.com/hazel-js/hazeljs/issues)
|
|
442
|
+
- [Discord](https://discord.com/channels/1448263814238965833/1448263814859456575)
|
package/dist/cron.service.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cron.test.d.ts","sourceRoot":"","sources":["../src/cron.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/// <reference types="jest" />
|
|
3
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
4
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
5
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
6
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
7
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
8
|
+
};
|
|
9
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
10
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
jest.mock('@hazeljs/core', () => ({
|
|
14
|
+
__esModule: true,
|
|
15
|
+
Service: () => () => undefined,
|
|
16
|
+
HazelModule: () => () => undefined,
|
|
17
|
+
Container: { getInstance: jest.fn() },
|
|
18
|
+
logger: { info: jest.fn(), debug: jest.fn(), warn: jest.fn(), error: jest.fn() },
|
|
19
|
+
default: { info: jest.fn(), debug: jest.fn(), warn: jest.fn(), error: jest.fn() },
|
|
20
|
+
}));
|
|
21
|
+
const core_1 = require("@hazeljs/core");
|
|
22
|
+
const cron_service_1 = require("./cron.service");
|
|
23
|
+
const cron_module_1 = require("./cron.module");
|
|
24
|
+
const cron_decorator_1 = require("./cron.decorator");
|
|
25
|
+
const cron_types_1 = require("./cron.types");
|
|
26
|
+
// Use a minute-level cron expression that won't fire during tests
|
|
27
|
+
const EVERY_MINUTE = '* * * * *';
|
|
28
|
+
describe('CronService', () => {
|
|
29
|
+
let service;
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
service = new cron_service_1.CronService();
|
|
32
|
+
});
|
|
33
|
+
afterEach(() => {
|
|
34
|
+
service.clearAll();
|
|
35
|
+
});
|
|
36
|
+
describe('registerJob()', () => {
|
|
37
|
+
it('registers a job and increments count', () => {
|
|
38
|
+
service.registerJob('job1', EVERY_MINUTE, jest.fn());
|
|
39
|
+
expect(service.getJobCount()).toBe(1);
|
|
40
|
+
});
|
|
41
|
+
it('replaces an existing job with the same name', () => {
|
|
42
|
+
service.registerJob('job1', EVERY_MINUTE, jest.fn());
|
|
43
|
+
service.registerJob('job1', EVERY_MINUTE, jest.fn());
|
|
44
|
+
expect(service.getJobCount()).toBe(1);
|
|
45
|
+
});
|
|
46
|
+
it('does not auto-start when enabled is false', () => {
|
|
47
|
+
service.registerJob('job1', EVERY_MINUTE, jest.fn(), {
|
|
48
|
+
cronTime: EVERY_MINUTE,
|
|
49
|
+
enabled: false,
|
|
50
|
+
});
|
|
51
|
+
const status = service.getJobStatus('job1');
|
|
52
|
+
expect(status).toBeDefined();
|
|
53
|
+
expect(status?.enabled).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
it('throws for invalid cron expression', () => {
|
|
56
|
+
expect(() => service.registerJob('bad', 'not-a-cron', jest.fn())).toThrow('Invalid cron expression');
|
|
57
|
+
});
|
|
58
|
+
it('registers multiple independent jobs', () => {
|
|
59
|
+
service.registerJob('job1', EVERY_MINUTE, jest.fn());
|
|
60
|
+
service.registerJob('job2', EVERY_MINUTE, jest.fn());
|
|
61
|
+
service.registerJob('job3', EVERY_MINUTE, jest.fn());
|
|
62
|
+
expect(service.getJobCount()).toBe(3);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
describe('deleteJob()', () => {
|
|
66
|
+
it('returns true and removes the job', () => {
|
|
67
|
+
service.registerJob('job1', EVERY_MINUTE, jest.fn());
|
|
68
|
+
expect(service.deleteJob('job1')).toBe(true);
|
|
69
|
+
expect(service.getJobCount()).toBe(0);
|
|
70
|
+
});
|
|
71
|
+
it('returns false for unknown job', () => {
|
|
72
|
+
expect(service.deleteJob('unknown')).toBe(false);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
describe('startJob() / stopJob()', () => {
|
|
76
|
+
it('startJob returns true for existing job', () => {
|
|
77
|
+
service.registerJob('job1', EVERY_MINUTE, jest.fn(), {
|
|
78
|
+
cronTime: EVERY_MINUTE,
|
|
79
|
+
enabled: false,
|
|
80
|
+
});
|
|
81
|
+
expect(service.startJob('job1')).toBe(true);
|
|
82
|
+
});
|
|
83
|
+
it('startJob returns false for unknown job', () => {
|
|
84
|
+
expect(service.startJob('unknown')).toBe(false);
|
|
85
|
+
});
|
|
86
|
+
it('stopJob returns true for existing job', () => {
|
|
87
|
+
service.registerJob('job1', EVERY_MINUTE, jest.fn());
|
|
88
|
+
expect(service.stopJob('job1')).toBe(true);
|
|
89
|
+
});
|
|
90
|
+
it('stopJob returns false for unknown job', () => {
|
|
91
|
+
expect(service.stopJob('unknown')).toBe(false);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
describe('enableJob() / disableJob()', () => {
|
|
95
|
+
it('enableJob returns true for existing job', () => {
|
|
96
|
+
service.registerJob('job1', EVERY_MINUTE, jest.fn(), {
|
|
97
|
+
cronTime: EVERY_MINUTE,
|
|
98
|
+
enabled: false,
|
|
99
|
+
});
|
|
100
|
+
expect(service.enableJob('job1')).toBe(true);
|
|
101
|
+
});
|
|
102
|
+
it('enableJob returns false for unknown job', () => {
|
|
103
|
+
expect(service.enableJob('unknown')).toBe(false);
|
|
104
|
+
});
|
|
105
|
+
it('disableJob returns true for existing job', () => {
|
|
106
|
+
service.registerJob('job1', EVERY_MINUTE, jest.fn());
|
|
107
|
+
expect(service.disableJob('job1')).toBe(true);
|
|
108
|
+
});
|
|
109
|
+
it('disableJob returns false for unknown job', () => {
|
|
110
|
+
expect(service.disableJob('unknown')).toBe(false);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
describe('getJobStatus()', () => {
|
|
114
|
+
it('returns status for registered job', () => {
|
|
115
|
+
service.registerJob('job1', EVERY_MINUTE, jest.fn());
|
|
116
|
+
const status = service.getJobStatus('job1');
|
|
117
|
+
expect(status).toBeDefined();
|
|
118
|
+
expect(status?.name).toBe('job1');
|
|
119
|
+
expect(status?.runCount).toBe(0);
|
|
120
|
+
expect(status?.enabled).toBe(true);
|
|
121
|
+
});
|
|
122
|
+
it('returns undefined for unknown job', () => {
|
|
123
|
+
expect(service.getJobStatus('unknown')).toBeUndefined();
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
describe('getAllJobStatuses()', () => {
|
|
127
|
+
it('returns empty array when no jobs', () => {
|
|
128
|
+
expect(service.getAllJobStatuses()).toEqual([]);
|
|
129
|
+
});
|
|
130
|
+
it('returns all job statuses', () => {
|
|
131
|
+
service.registerJob('j1', EVERY_MINUTE, jest.fn());
|
|
132
|
+
service.registerJob('j2', EVERY_MINUTE, jest.fn());
|
|
133
|
+
const statuses = service.getAllJobStatuses();
|
|
134
|
+
expect(statuses).toHaveLength(2);
|
|
135
|
+
expect(statuses.map((s) => s.name)).toContain('j1');
|
|
136
|
+
expect(statuses.map((s) => s.name)).toContain('j2');
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
describe('stopAll() / startAll() / clearAll()', () => {
|
|
140
|
+
it('stopAll stops all registered jobs', () => {
|
|
141
|
+
service.registerJob('j1', EVERY_MINUTE, jest.fn());
|
|
142
|
+
service.registerJob('j2', EVERY_MINUTE, jest.fn());
|
|
143
|
+
expect(() => service.stopAll()).not.toThrow();
|
|
144
|
+
});
|
|
145
|
+
it('startAll starts all registered jobs', () => {
|
|
146
|
+
service.registerJob('j1', EVERY_MINUTE, jest.fn(), {
|
|
147
|
+
cronTime: EVERY_MINUTE,
|
|
148
|
+
enabled: false,
|
|
149
|
+
});
|
|
150
|
+
expect(() => service.startAll()).not.toThrow();
|
|
151
|
+
});
|
|
152
|
+
it('clearAll removes all jobs', () => {
|
|
153
|
+
service.registerJob('j1', EVERY_MINUTE, jest.fn());
|
|
154
|
+
service.registerJob('j2', EVERY_MINUTE, jest.fn());
|
|
155
|
+
service.clearAll();
|
|
156
|
+
expect(service.getJobCount()).toBe(0);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
describe('getJobCount()', () => {
|
|
160
|
+
it('returns 0 when no jobs registered', () => {
|
|
161
|
+
expect(service.getJobCount()).toBe(0);
|
|
162
|
+
});
|
|
163
|
+
it('returns correct count after adding/removing jobs', () => {
|
|
164
|
+
service.registerJob('j1', EVERY_MINUTE, jest.fn());
|
|
165
|
+
service.registerJob('j2', EVERY_MINUTE, jest.fn());
|
|
166
|
+
expect(service.getJobCount()).toBe(2);
|
|
167
|
+
service.deleteJob('j1');
|
|
168
|
+
expect(service.getJobCount()).toBe(1);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
describe('job options: runOnInit', () => {
|
|
172
|
+
it('executes callback immediately when runOnInit is true', async () => {
|
|
173
|
+
const callback = jest.fn().mockResolvedValue(undefined);
|
|
174
|
+
service.registerJob('runNow', EVERY_MINUTE, callback, {
|
|
175
|
+
cronTime: EVERY_MINUTE,
|
|
176
|
+
runOnInit: true,
|
|
177
|
+
});
|
|
178
|
+
// Allow the async execute() to run
|
|
179
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
180
|
+
expect(callback).toHaveBeenCalled();
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
describe('job options: onComplete / onError', () => {
|
|
184
|
+
it('calls onComplete after successful execution (via runOnInit)', async () => {
|
|
185
|
+
const onComplete = jest.fn();
|
|
186
|
+
service.registerJob('complete-job', EVERY_MINUTE, jest.fn().mockResolvedValue(undefined), {
|
|
187
|
+
cronTime: EVERY_MINUTE,
|
|
188
|
+
runOnInit: true,
|
|
189
|
+
onComplete,
|
|
190
|
+
});
|
|
191
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
192
|
+
expect(onComplete).toHaveBeenCalled();
|
|
193
|
+
});
|
|
194
|
+
it('calls onError when callback throws (via runOnInit)', async () => {
|
|
195
|
+
const onError = jest.fn();
|
|
196
|
+
service.registerJob('error-job', EVERY_MINUTE, jest.fn().mockRejectedValue(new Error('fail')), {
|
|
197
|
+
cronTime: EVERY_MINUTE,
|
|
198
|
+
runOnInit: true,
|
|
199
|
+
onError,
|
|
200
|
+
});
|
|
201
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
202
|
+
expect(onError).toHaveBeenCalledWith(expect.any(Error));
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
describe('startJob on disabled job', () => {
|
|
206
|
+
it('warns but does not crash when starting a disabled job', () => {
|
|
207
|
+
service.registerJob('disabled-job', EVERY_MINUTE, jest.fn(), {
|
|
208
|
+
cronTime: EVERY_MINUTE,
|
|
209
|
+
enabled: false,
|
|
210
|
+
});
|
|
211
|
+
// startJob calls job.start() which logs a warn since job is disabled
|
|
212
|
+
expect(() => service.startJob('disabled-job')).not.toThrow();
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
describe('starting an already-started job', () => {
|
|
216
|
+
it('does not throw when starting a job that is already running', () => {
|
|
217
|
+
service.registerJob('already-running', EVERY_MINUTE, jest.fn());
|
|
218
|
+
// job is already started by registerJob
|
|
219
|
+
expect(() => service.startJob('already-running')).not.toThrow();
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
describe('Cron decorator', () => {
|
|
224
|
+
it('attaches metadata to the class', () => {
|
|
225
|
+
class MyService {
|
|
226
|
+
doWork() {
|
|
227
|
+
// noop
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
__decorate([
|
|
231
|
+
(0, cron_decorator_1.Cron)({ cronTime: EVERY_MINUTE, name: 'myJob' }),
|
|
232
|
+
__metadata("design:type", Function),
|
|
233
|
+
__metadata("design:paramtypes", []),
|
|
234
|
+
__metadata("design:returntype", void 0)
|
|
235
|
+
], MyService.prototype, "doWork", null);
|
|
236
|
+
const metadata = Reflect.getMetadata(cron_decorator_1.CRON_METADATA_KEY, MyService);
|
|
237
|
+
expect(metadata).toHaveLength(1);
|
|
238
|
+
expect(metadata[0].methodName).toBe('doWork');
|
|
239
|
+
expect(metadata[0].options.name).toBe('myJob');
|
|
240
|
+
expect(metadata[0].options.cronTime).toBe(EVERY_MINUTE);
|
|
241
|
+
});
|
|
242
|
+
it('uses default name based on class and method when name is not provided', () => {
|
|
243
|
+
class AnotherService {
|
|
244
|
+
handleTask() {
|
|
245
|
+
// noop
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
__decorate([
|
|
249
|
+
(0, cron_decorator_1.Cron)({ cronTime: EVERY_MINUTE }),
|
|
250
|
+
__metadata("design:type", Function),
|
|
251
|
+
__metadata("design:paramtypes", []),
|
|
252
|
+
__metadata("design:returntype", void 0)
|
|
253
|
+
], AnotherService.prototype, "handleTask", null);
|
|
254
|
+
const metadata = Reflect.getMetadata(cron_decorator_1.CRON_METADATA_KEY, AnotherService);
|
|
255
|
+
expect(metadata[0].options.name).toBe('AnotherService.handleTask');
|
|
256
|
+
});
|
|
257
|
+
it('accumulates metadata for multiple methods', () => {
|
|
258
|
+
class MultiService {
|
|
259
|
+
firstJob() {
|
|
260
|
+
// noop
|
|
261
|
+
}
|
|
262
|
+
secondJob() {
|
|
263
|
+
// noop
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
__decorate([
|
|
267
|
+
(0, cron_decorator_1.Cron)({ cronTime: EVERY_MINUTE, name: 'first' }),
|
|
268
|
+
__metadata("design:type", Function),
|
|
269
|
+
__metadata("design:paramtypes", []),
|
|
270
|
+
__metadata("design:returntype", void 0)
|
|
271
|
+
], MultiService.prototype, "firstJob", null);
|
|
272
|
+
__decorate([
|
|
273
|
+
(0, cron_decorator_1.Cron)({ cronTime: EVERY_MINUTE, name: 'second' }),
|
|
274
|
+
__metadata("design:type", Function),
|
|
275
|
+
__metadata("design:paramtypes", []),
|
|
276
|
+
__metadata("design:returntype", void 0)
|
|
277
|
+
], MultiService.prototype, "secondJob", null);
|
|
278
|
+
const metadata = Reflect.getMetadata(cron_decorator_1.CRON_METADATA_KEY, MultiService);
|
|
279
|
+
expect(metadata).toHaveLength(2);
|
|
280
|
+
const names = metadata.map((m) => m.options.name);
|
|
281
|
+
expect(names).toContain('first');
|
|
282
|
+
expect(names).toContain('second');
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
describe('getCronMetadata()', () => {
|
|
286
|
+
it('returns empty array for class with no @Cron decorators', () => {
|
|
287
|
+
class Plain {
|
|
288
|
+
doThing() {
|
|
289
|
+
// noop
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
const instance = new Plain();
|
|
293
|
+
expect((0, cron_decorator_1.getCronMetadata)(instance)).toEqual([]);
|
|
294
|
+
});
|
|
295
|
+
it('returns metadata for class with @Cron decorators', () => {
|
|
296
|
+
class Scheduled {
|
|
297
|
+
job() {
|
|
298
|
+
// noop
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
__decorate([
|
|
302
|
+
(0, cron_decorator_1.Cron)({ cronTime: EVERY_MINUTE }),
|
|
303
|
+
__metadata("design:type", Function),
|
|
304
|
+
__metadata("design:paramtypes", []),
|
|
305
|
+
__metadata("design:returntype", void 0)
|
|
306
|
+
], Scheduled.prototype, "job", null);
|
|
307
|
+
const instance = new Scheduled();
|
|
308
|
+
const meta = (0, cron_decorator_1.getCronMetadata)(instance);
|
|
309
|
+
expect(meta).toHaveLength(1);
|
|
310
|
+
expect(meta[0].methodName).toBe('job');
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
describe('CronModule', () => {
|
|
314
|
+
const EVERY_MINUTE = '* * * * *';
|
|
315
|
+
describe('forRoot()', () => {
|
|
316
|
+
it('returns module configuration with defaults', () => {
|
|
317
|
+
const result = cron_module_1.CronModule.forRoot();
|
|
318
|
+
expect(result.module).toBe(cron_module_1.CronModule);
|
|
319
|
+
expect(result.providers).toContain(cron_service_1.CronService);
|
|
320
|
+
expect(result.exports).toContain(cron_service_1.CronService);
|
|
321
|
+
expect(result.global).toBe(true);
|
|
322
|
+
});
|
|
323
|
+
it('respects isGlobal: false', () => {
|
|
324
|
+
const result = cron_module_1.CronModule.forRoot({ isGlobal: false });
|
|
325
|
+
expect(result.global).toBe(false);
|
|
326
|
+
});
|
|
327
|
+
it('respects isGlobal: true explicitly', () => {
|
|
328
|
+
const result = cron_module_1.CronModule.forRoot({ isGlobal: true });
|
|
329
|
+
expect(result.global).toBe(true);
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
describe('registerJobsFromProvider()', () => {
|
|
333
|
+
it('registers cron jobs from a provider with @Cron decorated methods', () => {
|
|
334
|
+
const cronService = new cron_service_1.CronService();
|
|
335
|
+
core_1.Container.getInstance.mockReturnValue({
|
|
336
|
+
resolve: jest.fn().mockReturnValue(cronService),
|
|
337
|
+
});
|
|
338
|
+
class TaskService {
|
|
339
|
+
doTask() {
|
|
340
|
+
// noop
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
__decorate([
|
|
344
|
+
(0, cron_decorator_1.Cron)({ cronTime: EVERY_MINUTE, name: 'myTask' }),
|
|
345
|
+
__metadata("design:type", Function),
|
|
346
|
+
__metadata("design:paramtypes", []),
|
|
347
|
+
__metadata("design:returntype", void 0)
|
|
348
|
+
], TaskService.prototype, "doTask", null);
|
|
349
|
+
const instance = new TaskService();
|
|
350
|
+
cron_module_1.CronModule.registerJobsFromProvider(instance);
|
|
351
|
+
expect(cronService.getJobCount()).toBeGreaterThan(0);
|
|
352
|
+
cronService.clearAll();
|
|
353
|
+
});
|
|
354
|
+
it('does nothing when CronService is not in container', () => {
|
|
355
|
+
core_1.Container.getInstance.mockReturnValue({
|
|
356
|
+
resolve: jest.fn().mockReturnValue(null),
|
|
357
|
+
});
|
|
358
|
+
class NoopService {
|
|
359
|
+
}
|
|
360
|
+
expect(() => cron_module_1.CronModule.registerJobsFromProvider(new NoopService())).not.toThrow();
|
|
361
|
+
});
|
|
362
|
+
it('does nothing when provider has no @Cron metadata', () => {
|
|
363
|
+
const cronService = new cron_service_1.CronService();
|
|
364
|
+
core_1.Container.getInstance.mockReturnValue({
|
|
365
|
+
resolve: jest.fn().mockReturnValue(cronService),
|
|
366
|
+
});
|
|
367
|
+
class EmptyService {
|
|
368
|
+
plainMethod() {
|
|
369
|
+
// noop
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
cron_module_1.CronModule.registerJobsFromProvider(new EmptyService());
|
|
373
|
+
expect(cronService.getJobCount()).toBe(0);
|
|
374
|
+
});
|
|
375
|
+
it('handles errors gracefully', () => {
|
|
376
|
+
core_1.Container.getInstance.mockImplementation(() => {
|
|
377
|
+
throw new Error('container not ready');
|
|
378
|
+
});
|
|
379
|
+
class TaskService {
|
|
380
|
+
}
|
|
381
|
+
expect(() => cron_module_1.CronModule.registerJobsFromProvider(new TaskService())).not.toThrow();
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
describe('CronExpression constants', () => {
|
|
386
|
+
it('EVERY_SECOND is a valid 6-field expression', () => {
|
|
387
|
+
expect(cron_types_1.CronExpression.EVERY_SECOND).toBe('* * * * * *');
|
|
388
|
+
});
|
|
389
|
+
it('EVERY_MINUTE is a valid expression', () => {
|
|
390
|
+
expect(cron_types_1.CronExpression.EVERY_MINUTE).toBe('0 * * * * *');
|
|
391
|
+
});
|
|
392
|
+
it('EVERY_HOUR is a valid expression', () => {
|
|
393
|
+
expect(cron_types_1.CronExpression.EVERY_HOUR).toBe('0 0 * * * *');
|
|
394
|
+
});
|
|
395
|
+
it('EVERY_DAY_AT_MIDNIGHT is a valid expression', () => {
|
|
396
|
+
expect(cron_types_1.CronExpression.EVERY_DAY_AT_MIDNIGHT).toBe('0 0 0 * * *');
|
|
397
|
+
});
|
|
398
|
+
it('EVERY_YEAR is a valid expression', () => {
|
|
399
|
+
expect(cron_types_1.CronExpression.EVERY_YEAR).toBe('0 0 0 1 1 *');
|
|
400
|
+
});
|
|
401
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hazeljs/cron",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.80",
|
|
4
4
|
"description": "Cron job scheduling module for HazelJS framework",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,7 +15,6 @@
|
|
|
15
15
|
"clean": "rm -rf dist"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@hazeljs/core": "^0.2.0-beta.8",
|
|
19
18
|
"node-cron": "^3.0.3"
|
|
20
19
|
},
|
|
21
20
|
"devDependencies": {
|
|
@@ -42,7 +41,14 @@
|
|
|
42
41
|
"scheduler",
|
|
43
42
|
"jobs"
|
|
44
43
|
],
|
|
45
|
-
"author": "Muhammad Arslan <
|
|
46
|
-
"license": "
|
|
47
|
-
"
|
|
44
|
+
"author": "Muhammad Arslan <muhammad.arslan@hazeljs.com>",
|
|
45
|
+
"license": "Apache-2.0",
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/hazeljs/hazel-js/issues"
|
|
48
|
+
},
|
|
49
|
+
"homepage": "https://hazeljs.com",
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"@hazeljs/core": ">=0.2.0-beta.0"
|
|
52
|
+
},
|
|
53
|
+
"gitHead": "083332d9cc6b634265382e7d1e0439236d30fedd"
|
|
48
54
|
}
|