Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
queue/src/schedule_builder.ts at main · boringnode/queue · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
boringnode
queue
Repository navigation
Code
Issues
2
(2)
Pull requests
1
(1)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
queue
/
src
/
schedule_builder.ts
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
233 lines (210 loc) · 5.98 KB
main
Breadcrumbs
queue
/
src
/
schedule_builder.ts
Copy path
Top
File metadata and controls
Code
Blame
233 lines (210 loc) · 5.98 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import
type
{
AdapterSelector
,
Duration
,
JobOptions
,
ScheduleConfig
,
ScheduleResult
,
}
from
'./types/main.js'
import
{
resolveJobDispatchTarget
}
from
'./job_dispatch_runtime.js'
import
{
parse
}
from
'./utils.js'
import
{
CronExpressionParser
}
from
'cron-parser'
import
*
as
errors
from
'./exceptions.js'
/**
* Fluent builder for creating job schedules.
*
*
@example
* ```typescript
* // Create with cron
* const { scheduleId } = await new ScheduleBuilder('CleanupJob', { days: 30 })
* .id('cleanup-daily')
* .cron('0 0 * * *')
* .timezone('Europe/Paris')
* .run()
*
* // Create with interval
* const { scheduleId } = await new ScheduleBuilder('SyncJob', {})
* .every('5m')
* .run()
* ```
*/
export
class
ScheduleBuilder
<
TPayload
=
unknown
>
implements
PromiseLike
<
ScheduleResult
>
{
#name:
string
#payload:
TPayload
#id?:
string
#cronExpression?:
string
#everyMs?:
number
#timezone:
string
=
'UTC'
#from?:
Date
#to?:
Date
#limit?:
number
#adapter?:
AdapterSelector
readonly
#jobOptions?:
(
)
=>
JobOptions
constructor
(
name
:
string
,
payload
:
TPayload
,
jobOptions
?:
(
)
=>
JobOptions
)
{
this
.
#name
=
name
this
.
#payload
=
payload
this
.
#jobOptions
=
jobOptions
}
/**
* Set a custom schedule ID.
* If not specified, defaults to the job name.
* If a schedule with this ID exists, it will be updated (upsert): it keeps
* its status, and its next run unless its timing changes.
*/
id
(
scheduleId
:
string
)
: this
{
this
.
#id
=
scheduleId
return
this
}
/**
* Set a cron expression for the schedule.
* Mutually exclusive with `every()`.
*/
cron
(
expression
:
string
)
: this
{
this
.
#cronExpression
=
expression
return
this
}
/**
* Set a repeating interval for the schedule. The next run is due this
* interval after a Worker claims the current one, so a late claim delays
* the following runs. Mutually exclusive with `cron()`.
*/
every
(
interval
:
Duration
)
: this
{
this
.
#everyMs
=
parse
(
interval
)
return
this
}
/**
* Set the timezone for cron evaluation.
*
@default
'UTC'
*/
timezone
(
tz
:
string
)
: this
{
this
.
#timezone
=
tz
return
this
}
/**
* Set the start boundary for the schedule.
* No jobs will be dispatched before this date.
*/
from
(
date
:
Date
)
: this
{
this
.
#from
=
date
return
this
}
/**
* Set the end boundary for the schedule.
* No jobs will be dispatched after this date.
*/
to
(
date
:
Date
)
: this
{
this
.
#to
=
date
return
this
}
/**
* Set both start and end boundaries for the schedule.
* Shorthand for `.from(start).to(end)`.
*/
between
(
from
:
Date
,
to
:
Date
)
: this
{
return
this
.
from
(
from
)
.
to
(
to
)
}
/**
* Set the maximum number of runs for this schedule.
*/
limit
(
maxRuns
:
number
)
: this
{
this
.
#limit
=
maxRuns
return
this
}
/**
* Store this Schedule on a specific Adapter.
*/
with
(
adapter
:
AdapterSelector
)
: this
{
this
.
#adapter
=
adapter
return
this
}
/**
* Create the schedule and return the schedule ID.
*/
async
run
(
)
:
Promise
<
ScheduleResult
>
{
const
jobOptions
=
this
.
#jobOptions?.
(
)
const
jobName
=
jobOptions
?.
name
??
this
.
#name
// Validation
if
(
!
this
.
#cronExpression
&&
!
this
.
#everyMs
)
{
throw
new
errors
.
E_INVALID_SCHEDULE_CONFIG
(
[
'Schedule must have either a cron expression or an interval'
,
]
)
}
if
(
this
.
#cronExpression
&&
this
.
#everyMs
)
{
throw
new
errors
.
E_INVALID_SCHEDULE_CONFIG
(
[
'Schedule cannot have both a cron expression and an interval'
,
]
)
}
// Validate cron expression. Some expressions parse but never match
// (`0 0 31 4,6 *`), and only throw once asked for an occurrence.
if
(
this
.
#cronExpression
)
{
try
{
CronExpressionParser
.
parse
(
this
.
#cronExpression
,
{
tz
:
this
.
#timezone
}
)
.
next
(
)
}
catch
(
error
)
{
throw
new
errors
.
E_INVALID_CRON_EXPRESSION
(
[
this
.
#cronExpression
,
(
error
as
Error
)
.
message
]
,
{
cause
:
error
,
}
)
}
}
const
config
:
ScheduleConfig
=
{
id
:
this
.
#id
??
jobName
,
name
:
jobName
,
payload
:
this
.
#payload
,
cronExpression
:
this
.
#cronExpression
,
everyMs
:
this
.
#everyMs
,
timezone
:
this
.
#timezone
,
from
:
this
.
#from
,
to
:
this
.
#to
,
limit
:
this
.
#limit
,
// Only used if the schedule is new or its timing changed: running the
// same definition again, at every boot, keeps its status and next run.
nextRunAt
:
this
.
#calculateNextRunAt
(
)
,
}
const
{
adapter
}
=
resolveJobDispatchTarget
(
jobOptions
,
{
adapter
:
this
.
#adapter
,
}
)
const
scheduleId
=
await
adapter
.
upsertSchedule
(
config
)
return
{
scheduleId
}
}
/**
* Calculate the next run time based on cron or interval.
*/
#calculateNextRunAt
(
)
:
Date
{
const
now
=
new
Date
(
)
let
nextRun
:
Date
if
(
this
.
#cronExpression
)
{
const
cron
=
CronExpressionParser
.
parse
(
this
.
#cronExpression
,
{
currentDate
:
now
,
tz
:
this
.
#timezone
,
}
)
nextRun
=
cron
.
next
(
)
.
toDate
(
)
}
else
{
// Interval-based: next run is now + interval
nextRun
=
new
Date
(
now
.
getTime
(
)
+
this
.
#everyMs
!
)
}
// Respect from boundary
if
(
this
.
#from
&&
nextRun
<
this
.
#from
)
{
if
(
this
.
#cronExpression
)
{
// Recalculate from the start boundary
const
cron
=
CronExpressionParser
.
parse
(
this
.
#cronExpression
,
{
currentDate
:
this
.
#from
,
tz
:
this
.
#timezone
,
}
)
nextRun
=
cron
.
next
(
)
.
toDate
(
)
}
else
{
nextRun
=
this
.
#from
}
}
return
nextRun
}
/**
* Implement PromiseLike to allow `await builder.every('5m')` syntax.
*/
then
<
TResult1
=
ScheduleResult
,
TResult2
=
never
>
(
onfulfilled
?:
(
(
value
:
ScheduleResult
)
=>
TResult1
|
PromiseLike
<
TResult1
>
)
|
null
,
onrejected
?:
(
(
reason
:
unknown
)
=>
TResult2
|
PromiseLike
<
TResult2
>
)
|
null
)
:
Promise
<
TResult1
|
TResult2
>
{
return
this
.
run
(
)
.
then
(
onfulfilled
,
onrejected
)
}
}
You can’t perform that action at this time.