Skip to content

Commit c08ce5f

Browse files
authored
Merge pull request #5 from denysovvl/cron-patterns
Added cron patterns as functions and constatns
2 parents 0eb9fbb + b9fe796 commit c08ce5f

File tree

2 files changed

+229
-23
lines changed

2 files changed

+229
-23
lines changed

src/schedule/index.ts

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
enum Days {
2+
SUNDAY = 0,
3+
MONDAY = 1,
4+
TUESDAY = 2,
5+
WEDNESAY = 3,
6+
THURSDAY = 4,
7+
FRIDAY = 5,
8+
SATURDAY = 6
9+
}
10+
11+
enum ConstantExpressions {
12+
EVERY_SECOND = '* * * * * *',
13+
EVERY_5_SECONDS = '*/5 * * * * *',
14+
EVERY_10_SECONDS = '*/10 * * * * *',
15+
EVERY_30_SECONDS = '*/30 * * * * *',
16+
EVERY_MINUTE = '*/1 * * * *',
17+
EVERY_5_MINUTES = '0 */5 * * * *',
18+
EVERY_10_MINUTES = '0 */10 * * * *',
19+
EVERY_30_MINUTES = '0 */30 * * * *',
20+
EVERY_HOUR = '0 0-23/1 * * *',
21+
EVERY_2_HOURS = '0 0-23/2 * * *',
22+
EVERY_3_HOURS = '0 0-23/3 * * *',
23+
EVERY_4_HOURS = '0 0-23/4 * * *',
24+
EVERY_5_HOURS = '0 0-23/5 * * *',
25+
EVERY_6_HOURS = '0 0-23/6 * * *',
26+
EVERY_7_HOURS = '0 0-23/7 * * *',
27+
EVERY_8_HOURS = '0 0-23/8 * * *',
28+
EVERY_9_HOURS = '0 0-23/9 * * *',
29+
EVERY_10_HOURS = '0 0-23/10 * * *',
30+
EVERY_11_HOURS = '0 0-23/11 * * *',
31+
EVERY_12_HOURS = '0 0-23/12 * * *',
32+
EVERY_DAY_AT_1AM = '0 01 * * *',
33+
EVERY_DAY_AT_2AM = '0 02 * * *',
34+
EVERY_DAY_AT_3AM = '0 03 * * *',
35+
EVERY_DAY_AT_4AM = '0 04 * * *',
36+
EVERY_DAY_AT_5AM = '0 05 * * *',
37+
EVERY_DAY_AT_6AM = '0 06 * * *',
38+
EVERY_DAY_AT_7AM = '0 07 * * *',
39+
EVERY_DAY_AT_8AM = '0 08 * * *',
40+
EVERY_DAY_AT_9AM = '0 09 * * *',
41+
EVERY_DAY_AT_10AM = '0 10 * * *',
42+
EVERY_DAY_AT_11AM = '0 11 * * *',
43+
EVERY_DAY_AT_NOON = '0 12 * * *',
44+
EVERY_DAY_AT_1PM = '0 13 * * *',
45+
EVERY_DAY_AT_2PM = '0 14 * * *',
46+
EVERY_DAY_AT_3PM = '0 15 * * *',
47+
EVERY_DAY_AT_4PM = '0 16 * * *',
48+
EVERY_DAY_AT_5PM = '0 17 * * *',
49+
EVERY_DAY_AT_6PM = '0 18 * * *',
50+
EVERY_DAY_AT_7PM = '0 19 * * *',
51+
EVERY_DAY_AT_8PM = '0 20 * * *',
52+
EVERY_DAY_AT_9PM = '0 21 * * *',
53+
EVERY_DAY_AT_10PM = '0 22 * * *',
54+
EVERY_DAY_AT_11PM = '0 23 * * *',
55+
EVERY_DAY_AT_MIDNIGHT = '0 0 * * *',
56+
EVERY_WEEK = '0 0 * * 0',
57+
EVERY_WEEKDAY = '0 0 * * 1-5',
58+
EVERY_WEEKEND = '0 0 * * 6,0',
59+
EVERY_1ST_DAY_OF_MONTH_AT_MIDNIGHT = '0 0 1 * *',
60+
EVERY_1ST_DAY_OF_MONTH_AT_NOON = '0 12 1 * *',
61+
EVERY_2ND_HOUR = '0 */2 * * *',
62+
EVERY_2ND_HOUR_FROM_1AM_THROUGH_11PM = '0 1-23/2 * * *',
63+
EVERY_2ND_MONTH = '0 0 1 */2 *',
64+
EVERY_QUARTER = '0 0 1 */3 *',
65+
EVERY_6_MONTHS = '0 0 1 */6 *',
66+
EVERY_YEAR = '0 0 1 1 *',
67+
EVERY_30_MINUTES_BETWEEN_9AM_AND_5PM = '0 */30 9-17 * * *',
68+
EVERY_30_MINUTES_BETWEEN_9AM_AND_6PM = '0 */30 9-18 * * *',
69+
EVERY_30_MINUTES_BETWEEN_10AM_AND_7PM = '0 */30 10-19 * * *',
70+
}
71+
72+
const FunctionExpressions = {
73+
/**
74+
* Run the task every S seconds
75+
* @param {number} seconds
76+
*/
77+
everySenconds(seconds = 1) { return `*/${seconds} * * * * *` },
78+
/**
79+
* Run the task every M minutes
80+
* @param {number} minutes
81+
*/
82+
everyMinutes(minutes = 1) { return `0 */${minutes} * * * *` },
83+
/**
84+
* Run the task every H hours
85+
* @param {number} hours
86+
*/
87+
everyHours(hours = 1) { return `0 0-23/${hours} * * *` },
88+
/**
89+
* Run the task every H hours at M minutes
90+
* @param {number} hours
91+
* @param {number} minutes
92+
*/
93+
everyHoursAt(hours: number, minutes = 0) { return `${minutes} 0-23/${hours} * * *` },
94+
/**
95+
* Run the task every day at HH:MM
96+
* @param {string} [time='08:00'] - The time in 'HH:mm' format (24-hour clock).
97+
*/
98+
everyDayAt(time = '00:00') {
99+
const [hours, minutes] = time.split(':');
100+
return `${minutes} ${hours} 0 * *`;
101+
},
102+
/**
103+
* Run the task at a specific time on a given day of the week.
104+
* @param {number} day - The day of the week (0 for Sunday, 1 for Monday, ..., 6 for Saturday).
105+
* @param {string} [time='14:00'] - The time in 'HH:mm' format (24-hour clock).
106+
*/
107+
everyWeekOn(day: number, time = '00:00') {
108+
const [hours, minutes] = time.split(':');
109+
return `${minutes} ${hours} * * ${day}`;
110+
},
111+
/**
112+
* Run the task every day from monday to friday at given time
113+
* @param {string} [time='14:00'] - The time in 'HH:mm' format (24-hour clock).
114+
*/
115+
everyWeekdayAt(time = '00:00') {
116+
const [hours, minutes] = time.split(':');
117+
return `${minutes} ${hours} * * 1-5`
118+
},
119+
/**
120+
* Run the task on saturday and sunday at given time
121+
* @param {string} [time='14:00'] - The time in 'HH:mm' format (24-hour clock).
122+
*/
123+
everyWeekendAt(time = '00:00') {
124+
const [hours, minutes] = time.split(':');
125+
return `${minutes} ${hours} * * 6,0`
126+
},
127+
128+
/**
129+
* Run the task every second
130+
* Alias: EVERY_SECOND
131+
*/
132+
everySecond() { return ConstantExpressions.EVERY_SECOND },
133+
/**
134+
* Run the task every minute
135+
* Alias: EVERY_MINUTE
136+
*/
137+
everyMinute() { return ConstantExpressions.EVERY_MINUTE },
138+
/**
139+
* Run the task every hour
140+
* Alias: EVERY_HOUR
141+
*/
142+
hourly() { return ConstantExpressions.EVERY_HOUR },
143+
/**
144+
* Run the task every day at 00:00
145+
* Alias: EVERY_DAY_AT_MIDNIGHT
146+
*/
147+
daily() { return ConstantExpressions.EVERY_DAY_AT_MIDNIGHT },
148+
/**
149+
* Run the task every day from monday to friday at 00:00
150+
* Alias: EVERY_WEEKDAY
151+
*/
152+
everyWeekday() { return ConstantExpressions.EVERY_WEEKDAY },
153+
/**
154+
* Run the task on saturday and sunday at 00:00
155+
* Alias: EVERY_WEEKDAY
156+
*/
157+
everyWeekend() { return ConstantExpressions.EVERY_WEEKEND },
158+
/**
159+
* Run the task every week on monday at 00:00
160+
* Alias: EVERY_WEEK
161+
*/
162+
weekly() { return ConstantExpressions.EVERY_WEEK },
163+
/**
164+
* Run the task every first day of month at 00:00
165+
* Alias: EVERY_1ST_DAY_OF_MONTH_AT_MIDNIGHT
166+
*/
167+
monthly() { return ConstantExpressions.EVERY_1ST_DAY_OF_MONTH_AT_MIDNIGHT },
168+
/**
169+
* Run the task every quarter (3 months)
170+
* Alias: EVERY_QUARTER
171+
*/
172+
everyQuarter() { return ConstantExpressions.EVERY_QUARTER },
173+
/**
174+
* Run the task on the first day of every year at 00:00
175+
* Alias: EVERY_1ST_DAY_OF_MONTH_AT_MIDNIGHT
176+
*/
177+
yearly() { return ConstantExpressions.EVERY_YEAR },
178+
}
179+
180+
181+
const Patterns = {
182+
...FunctionExpressions,
183+
...Days,
184+
...ConstantExpressions,
185+
}
186+
187+
export { Patterns };

test/index.test.ts

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/ban-ts-comment */
22
import { Elysia } from 'elysia'
33
import { cron } from '../src'
4+
import { Patterns } from '../src/schedule'
45

56
import { describe, expect, it } from 'bun:test'
67

@@ -69,31 +70,40 @@ describe('Mutli Cron', () => {
6970
it('run cronjobs', async () => {
7071
let done1 = false
7172
let done2 = false
73+
let done3 = false
7274

73-
new Elysia()
74-
.use(
75-
cron({
76-
pattern: '*/1 * * * * *',
77-
name: 'job1',
78-
run() {
79-
done1 = true
80-
}
81-
})
82-
)
83-
.use(
84-
cron({
85-
pattern: '*/1 * * * * *',
86-
name: 'job2',
87-
run() {
88-
done2 = true
89-
}
90-
})
91-
)
75+
new Elysia().use(
76+
cron({
77+
pattern: '*/1 * * * * *',
78+
name: 'job1',
79+
run() {
80+
done1 = true
81+
}
82+
})
83+
).use(
84+
cron({
85+
pattern: '*/1 * * * * *',
86+
name: 'job2',
87+
run() {
88+
done2 = true
89+
}
90+
})
91+
)
92+
.use(
93+
cron({
94+
pattern: Patterns.everySecond(),
95+
name: 'job3',
96+
run() {
97+
done3 = true
98+
}
99+
})
100+
)
92101

93102
await new Promise((resolve) => setTimeout(resolve, 1100))
94103

95104
expect(done1).toBe(true)
96105
expect(done2).toBe(true)
106+
expect(done3).toBe(true)
97107
})
98108

99109
it('add cronjobs to store', async () => {
@@ -106,21 +116,30 @@ describe('Mutli Cron', () => {
106116
// Not empty
107117
}
108118
})
109-
)
110-
.use(
119+
).use(
111120
cron({
112121
pattern: '*/1 * * * * *',
113122
name: 'job2',
114123
run() {
115124
// Not empty
116125
}
117126
})
127+
).use(
128+
cron({
129+
pattern: Patterns.EVERY_SECOND,
130+
name: 'job3',
131+
run() {
132+
// Not empty
133+
}
134+
})
118135
)
119136

120137
// @ts-expect-error
121-
expect(Object.keys(app.singleton.store.cron)[0]).toBe('job1')
138+
expect(Object.keys(app.store.cron)[0]).toBe('job1')
139+
// @ts-expect-error
140+
expect(Object.keys(app.store.cron)[1]).toBe('job2')
122141
// @ts-expect-error
123-
expect(Object.keys(app.singleton.store.cron)[1]).toBe('job2')
142+
expect(Object.keys(app.store.cron)[2]).toBe('job3')
124143
})
125144

126145
it('stop cronjobs', async () => {

0 commit comments

Comments
 (0)