|
| 1 | +import { getOrdinalNumber } from '@shared/utils'; |
| 2 | + |
| 3 | +import { CRON_VALUE_DEFAULT, DAYS_OF_WEEK } from './constants'; |
| 4 | +import { CronSegmentKey, CronSegmentValue, Period } from './types'; |
| 5 | + |
| 6 | +/** Class for convenient handling cron settings */ |
| 7 | +export class CronService { |
| 8 | + private initialValueLength = 5; |
| 9 | + |
| 10 | + private period: Period; |
| 11 | + |
| 12 | + private value: Map<CronSegmentKey, CronSegmentValue>; |
| 13 | + |
| 14 | + constructor(initialValue?: string) { |
| 15 | + this.value = this.transformInitialValueToMap(initialValue); |
| 16 | + this.period = this.initPeriod(); |
| 17 | + } |
| 18 | + |
| 19 | + private transformInitialValueToMap(initialValue?: string) { |
| 20 | + const splittedValue = initialValue?.split(' '); |
| 21 | + if (splittedValue?.length !== this.initialValueLength) { |
| 22 | + return CRON_VALUE_DEFAULT; |
| 23 | + } |
| 24 | + |
| 25 | + const cronValue = splittedValue.map((segment) => { |
| 26 | + const parsedValue = parseInt(segment); |
| 27 | + if (Number.isInteger(parsedValue)) { |
| 28 | + return parsedValue; |
| 29 | + } |
| 30 | + return null; |
| 31 | + }); |
| 32 | + |
| 33 | + return new Map<CronSegmentKey, CronSegmentValue>([ |
| 34 | + ['minute', cronValue[0]], |
| 35 | + ['hour', cronValue[1]], |
| 36 | + ['date', cronValue[2]], |
| 37 | + ['day', cronValue[4]], |
| 38 | + ]); |
| 39 | + } |
| 40 | + |
| 41 | + private initPeriod() { |
| 42 | + if (this.getMonthDay() === null && this.getWeekDay() === null) { |
| 43 | + return Period.DAY; |
| 44 | + } |
| 45 | + if (this.getMonthDay()) { |
| 46 | + return Period.MONTH; |
| 47 | + } |
| 48 | + return Period.WEEK; |
| 49 | + } |
| 50 | + |
| 51 | + getPeriod() { |
| 52 | + return this.period; |
| 53 | + } |
| 54 | + |
| 55 | + getMinute(): number { |
| 56 | + return this.value.get('minute')!; |
| 57 | + } |
| 58 | + |
| 59 | + getHour(): number { |
| 60 | + return this.value.get('hour')!; |
| 61 | + } |
| 62 | + |
| 63 | + getTime() { |
| 64 | + return `${this.getHour()}:${this.getMinute()}`; |
| 65 | + } |
| 66 | + |
| 67 | + getMonthDay(): CronSegmentValue { |
| 68 | + return this.value.get('date') ?? null; |
| 69 | + } |
| 70 | + |
| 71 | + getWeekDay(): CronSegmentValue { |
| 72 | + return this.value.get('day') ?? null; |
| 73 | + } |
| 74 | + |
| 75 | + setPeriod(period: Period) { |
| 76 | + this.period = period; |
| 77 | + switch (period) { |
| 78 | + case Period.DAY: |
| 79 | + this.setMonthDay(null); |
| 80 | + this.setWeekDay(null); |
| 81 | + break; |
| 82 | + case Period.WEEK: |
| 83 | + this.setWeekDay(new Date().getDay()); |
| 84 | + this.setMonthDay(null); |
| 85 | + break; |
| 86 | + case Period.MONTH: |
| 87 | + this.setWeekDay(null); |
| 88 | + this.setMonthDay(new Date().getDate()); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + setMinute(value: number) { |
| 93 | + if (value < 0 || value > 59) { |
| 94 | + throw new Error('Invalid value'); |
| 95 | + } |
| 96 | + this.value.set('minute', value); |
| 97 | + } |
| 98 | + |
| 99 | + setHour(value: number) { |
| 100 | + if (value < 0 || value > 23) { |
| 101 | + throw new Error('Invalid value'); |
| 102 | + } |
| 103 | + this.value.set('hour', value); |
| 104 | + } |
| 105 | + |
| 106 | + setTime(hour?: number, minute?: number) { |
| 107 | + this.setHour(hour ?? new Date().getHours()); |
| 108 | + this.setMinute(minute ?? new Date().getMinutes()); |
| 109 | + } |
| 110 | + |
| 111 | + setMonthDay(value: CronSegmentValue) { |
| 112 | + if (value === null) { |
| 113 | + this.value.set('date', null); |
| 114 | + return; |
| 115 | + } |
| 116 | + if (value < 1 || value > 31) { |
| 117 | + throw new Error('Invalid value'); |
| 118 | + } |
| 119 | + this.value.set('date', value); |
| 120 | + } |
| 121 | + |
| 122 | + setWeekDay(value: CronSegmentValue) { |
| 123 | + if (value === null) { |
| 124 | + this.value.set('day', null); |
| 125 | + return; |
| 126 | + } |
| 127 | + if (value < 0 || value > 6) { |
| 128 | + throw new Error('Invalid value'); |
| 129 | + } |
| 130 | + this.value.set('day', value); |
| 131 | + } |
| 132 | + |
| 133 | + toString() { |
| 134 | + const minute = this.getMinute(); |
| 135 | + const hour = this.getHour(); |
| 136 | + const date = this.getMonthDay() ?? '*'; |
| 137 | + const day = this.getWeekDay() ?? '*'; |
| 138 | + return `${minute} ${hour} ${date} * ${day}`; |
| 139 | + } |
| 140 | + |
| 141 | + getSchedule() { |
| 142 | + const time = this.getTime(); |
| 143 | + const day = this.getWeekDay(); |
| 144 | + const date = this.getMonthDay(); |
| 145 | + |
| 146 | + let schedule = `Every ${this.period} `; |
| 147 | + |
| 148 | + if (day !== null) { |
| 149 | + schedule += `on ${DAYS_OF_WEEK[day]} `; |
| 150 | + } else if (date) { |
| 151 | + schedule += `${getOrdinalNumber(date)} `; |
| 152 | + } |
| 153 | + |
| 154 | + schedule += `at ${time}`; |
| 155 | + |
| 156 | + return schedule; |
| 157 | + } |
| 158 | +} |
0 commit comments