@@ -27,20 +27,9 @@ import dev.starry.ktscheduler.triggers.CronTrigger
27
27
import dev.starry.ktscheduler.triggers.DailyTrigger
28
28
import dev.starry.ktscheduler.triggers.IntervalTrigger
29
29
import dev.starry.ktscheduler.triggers.OneTimeTrigger
30
- import kotlinx.coroutines.CoroutineDispatcher
31
- import kotlinx.coroutines.CoroutineScope
32
- import kotlinx.coroutines.Dispatchers
33
- import kotlinx.coroutines.SupervisorJob
34
- import kotlinx.coroutines.cancel
35
- import kotlinx.coroutines.delay
36
- import kotlinx.coroutines.isActive
37
- import kotlinx.coroutines.launch
38
- import java.time.DayOfWeek
39
- import java.time.Duration
40
- import java.time.LocalTime
41
- import java.time.ZoneId
42
- import java.time.ZonedDateTime
43
- import java.util.UUID
30
+ import kotlinx.coroutines.*
31
+ import java.time.*
32
+ import java.util.*
44
33
import java.util.logging.Logger
45
34
46
35
/* *
@@ -163,7 +152,14 @@ class KtScheduler(
163
152
*/
164
153
override fun addJob (job : Job ) {
165
154
logger.info(" Adding job ${job.jobId} " )
166
- jobStore.addJob(job)
155
+ val jobToAdd = job.nextRunTime?.let {
156
+ job
157
+ } ? : job.copy(
158
+ nextRunTime = job.trigger.getNextRunTime(ZonedDateTime .now(timeZone), timeZone)
159
+ ).apply {
160
+ logger.info(" Calculated next run time for job ${job.jobId} : $nextRunTime " )
161
+ }
162
+ jobStore.addJob(jobToAdd)
167
163
}
168
164
169
165
/* *
@@ -282,17 +278,18 @@ class KtScheduler(
282
278
* ```
283
279
* val trigger = CronTrigger(daysOfWeek, time)
284
280
* val job = Job(
285
- * jobId = "runCron-${UUID.randomUUID()}",
286
- * trigger = trigger,
287
- * nextRunTime = trigger.getNextRunTime(ZonedDateTime.now(timeZone), timeZone),
288
- * runConcurrently = runConcurrently,
289
- * dispatcher = dispatcher,
290
- * callback = callback
281
+ * jobId = "runCron-${UUID.randomUUID()}",
282
+ * trigger = trigger,
283
+ * nextRunTime = trigger.getNextRunTime(ZonedDateTime.now(timeZone), timeZone),
284
+ * runConcurrently = runConcurrently,
285
+ * dispatcher = dispatcher,
286
+ * callback = callback
291
287
* )
292
288
*
293
289
* scheduler.addJob(job)
294
290
* ```
295
291
*
292
+ * @param jobId The ID of the job. Default is a random UUID.
296
293
* @param daysOfWeek The set of days of the week on which the job should run.
297
294
* @param time The time at which the job should run.
298
295
* @param dispatcher The coroutine dispatcher to use. Default is [Dispatchers.Default].
@@ -301,6 +298,7 @@ class KtScheduler(
301
298
* @return The ID of the scheduled job.
302
299
*/
303
300
fun runCron (
301
+ jobId : String = "runCron-${UUID .randomUUID()}",
304
302
daysOfWeek : Set <DayOfWeek >,
305
303
time : LocalTime ,
306
304
dispatcher : CoroutineDispatcher = Dispatchers .Default ,
@@ -309,14 +307,14 @@ class KtScheduler(
309
307
): String {
310
308
val trigger = CronTrigger (daysOfWeek, time)
311
309
val job = Job (
312
- jobId = " runCron- ${ UUID .randomUUID()} " ,
310
+ jobId = jobId ,
313
311
trigger = trigger,
314
312
nextRunTime = trigger.getNextRunTime(ZonedDateTime .now(timeZone), timeZone),
315
313
runConcurrently = runConcurrently,
316
314
dispatcher = dispatcher,
317
315
callback = block
318
316
)
319
- job. let { addJob(it) } .also { return job.jobId }
317
+ addJob(job) .also { return job.jobId }
320
318
}
321
319
322
320
/* *
@@ -328,39 +326,41 @@ class KtScheduler(
328
326
* ```
329
327
* val trigger = DailyTrigger(dailyTime)
330
328
* val job = Job(
331
- * jobId = "runDaily-${UUID.randomUUID()}",
332
- * trigger = trigger,
333
- * nextRunTime = trigger.getNextRunTime(ZonedDateTime.now(timeZone), timeZone),
334
- * runConcurrently = runConcurrently,
335
- * dispatcher = dispatcher,
336
- * callback = callback
329
+ * jobId = "runDaily-${UUID.randomUUID()}",
330
+ * trigger = trigger,
331
+ * nextRunTime = trigger.getNextRunTime(ZonedDateTime.now(timeZone), timeZone),
332
+ * runConcurrently = runConcurrently,
333
+ * dispatcher = dispatcher,
334
+ * callback = callback
337
335
* )
338
336
*
339
337
* scheduler.addJob(job)
340
338
* ```
341
339
*
340
+ * @param jobId The ID of the job. Default is a random UUID.
342
341
* @param dailyTime The time at which the job should run daily.
343
342
* @param dispatcher The coroutine dispatcher to use. Default is [Dispatchers.Default].
344
343
* @param runConcurrently Whether the job should run concurrently. Default is `true`.
345
344
* @param block The block of code to execute.
346
345
* @return The ID of the scheduled job.
347
346
*/
348
347
fun runDaily (
348
+ jobId : String = "runDaily-${UUID .randomUUID()}",
349
349
dailyTime : LocalTime ,
350
350
dispatcher : CoroutineDispatcher = Dispatchers .Default ,
351
351
runConcurrently : Boolean = true,
352
352
block : suspend () -> Unit
353
353
): String {
354
354
val trigger = DailyTrigger (dailyTime)
355
355
val job = Job (
356
- jobId = " runDaily- ${ UUID .randomUUID()} " ,
356
+ jobId = jobId ,
357
357
trigger = trigger,
358
358
nextRunTime = trigger.getNextRunTime(ZonedDateTime .now(timeZone), timeZone),
359
359
runConcurrently = runConcurrently,
360
360
dispatcher = dispatcher,
361
361
callback = block
362
362
)
363
- job. let { addJob(it) } .also { return job.jobId }
363
+ addJob(job) .also { return job.jobId }
364
364
}
365
365
366
366
/* *
@@ -372,39 +372,41 @@ class KtScheduler(
372
372
* ```
373
373
* val trigger = IntervalTrigger(intervalSeconds)
374
374
* val job = Job(
375
- * jobId = "runRepeating-${UUID.randomUUID()}",
376
- * trigger = trigger,
377
- * nextRunTime = trigger.getNextRunTime(ZonedDateTime.now(timeZone), timeZone),
378
- * runConcurrently = runConcurrently,
379
- * dispatcher = dispatcher,
380
- * callback = block
375
+ * jobId = "runRepeating-${UUID.randomUUID()}",
376
+ * trigger = trigger,
377
+ * nextRunTime = trigger.getNextRunTime(ZonedDateTime.now(timeZone), timeZone),
378
+ * runConcurrently = runConcurrently,
379
+ * dispatcher = dispatcher,
380
+ * callback = block
381
381
* )
382
382
*
383
383
* scheduler.addJob(job)
384
384
* ```
385
385
*
386
+ * @param jobId The ID of the job. Default is a random UUID.
386
387
* @param intervalSeconds The interval in seconds at which the job should run.
387
388
* @param dispatcher The coroutine dispatcher to use. Default is [Dispatchers.Default].
388
389
* @param runConcurrently Whether the job should run concurrently. Default is `true`.
389
390
* @param block The block of code to execute.
390
391
* @return The ID of the scheduled job.
391
392
*/
392
393
fun runRepeating (
394
+ jobId : String = "runRepeating-${UUID .randomUUID()}",
393
395
intervalSeconds : Long ,
394
396
dispatcher : CoroutineDispatcher = Dispatchers .Default ,
395
397
runConcurrently : Boolean = true,
396
398
block : suspend () -> Unit
397
399
): String {
398
400
val trigger = IntervalTrigger (intervalSeconds)
399
401
val job = Job (
400
- jobId = " runRepeating- ${ UUID .randomUUID()} " ,
402
+ jobId = jobId ,
401
403
trigger = trigger,
402
404
nextRunTime = trigger.getNextRunTime(ZonedDateTime .now(timeZone), timeZone),
403
405
runConcurrently = runConcurrently,
404
406
dispatcher = dispatcher,
405
407
callback = block
406
408
)
407
- job. let { addJob(it) } .also { return job.jobId }
409
+ addJob(job) .also { return job.jobId }
408
410
}
409
411
410
412
/* *
@@ -415,38 +417,40 @@ class KtScheduler(
415
417
*
416
418
* ```
417
419
* val job = Job(
418
- * jobId = "runOnce-${UUID.randomUUID()}",
419
- * trigger = OneTimeTrigger(runAt),
420
- * nextRunTime = runAt,
421
- * runConcurrently = runConcurrently,
422
- * dispatcher = dispatcher,
423
- * callback = callback
420
+ * jobId = "runOnce-${UUID.randomUUID()}",
421
+ * trigger = OneTimeTrigger(runAt),
422
+ * nextRunTime = runAt,
423
+ * runConcurrently = runConcurrently,
424
+ * dispatcher = dispatcher,
425
+ * callback = callback
424
426
* )
425
427
*
426
428
* scheduler.addJob(job)
427
429
* ```
428
430
*
431
+ * @param jobId The ID of the job. Default is a random UUID.
429
432
* @param runAt The time at which the job should run.
430
433
* @param dispatcher The coroutine dispatcher to use. Default is [Dispatchers.Default].
431
434
* @param runConcurrently Whether the job should run concurrently. Default is `true`.
432
435
* @param block The block of code to execute.
433
436
* @return The ID of the scheduled job.
434
437
*/
435
438
fun runOnce (
439
+ jobId : String = "runOnce-${UUID .randomUUID()}",
436
440
runAt : ZonedDateTime ,
437
441
dispatcher : CoroutineDispatcher = Dispatchers .Default ,
438
442
runConcurrently : Boolean = true,
439
443
block : suspend () -> Unit
440
444
): String {
441
445
val job = Job (
442
- jobId = " runOnce- ${ UUID .randomUUID()} " ,
446
+ jobId = jobId ,
443
447
trigger = OneTimeTrigger (runAt),
444
448
nextRunTime = runAt,
445
449
runConcurrently = runConcurrently,
446
450
dispatcher = dispatcher,
447
451
callback = block
448
452
)
449
- job. let { addJob(it) } .also { return job.jobId }
453
+ addJob(job) .also { return job.jobId }
450
454
}
451
455
452
456
// ============================================================================================
0 commit comments