summaryrefslogtreecommitdiff
path: root/ext/cron/time.rs
diff options
context:
space:
mode:
authorIgor Zinkovsky <igor@deno.com>2023-11-01 11:57:55 -0700
committerGitHub <noreply@github.com>2023-11-01 11:57:55 -0700
commit01d3e0f317ca180bbf0ac8a17c6651869110e02f (patch)
treeb8e3bb91e87cf396bfa782f6377158f8a170b32b /ext/cron/time.rs
parent82643857cc77a80f9a819584035ec147a6114553 (diff)
feat(cron) implement Deno.cron() (#21019)
This PR adds unstable `Deno.cron` API to trigger execution of cron jobs. * State: All cron state is in memory. Cron jobs are scheduled according to the cron schedule expression and the current time. No state is persisted to disk. * Time zone: Cron expressions specify time in UTC. * Overlapping executions: not permitted. If the next scheduled execution time occurs while the same cron job is still executing, the scheduled execution is skipped. * Retries: failed jobs are automatically retried until they succeed or until retry threshold is reached. Retry policy can be optionally specified using `options.backoffSchedule`.
Diffstat (limited to 'ext/cron/time.rs')
-rw-r--r--ext/cron/time.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/ext/cron/time.rs b/ext/cron/time.rs
new file mode 100644
index 000000000..60375818b
--- /dev/null
+++ b/ext/cron/time.rs
@@ -0,0 +1,19 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+
+/// Identical to chrono::Utc::now() but without the system "clock"
+/// feature flag.
+///
+/// The "clock" feature flag pulls in the "iana-time-zone" crate
+/// which links to macOS's "CoreFoundation" framework which increases
+/// startup time for the CLI.
+pub fn utc_now() -> chrono::DateTime<chrono::Utc> {
+ let now = std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .expect("system time before Unix epoch");
+ let naive = chrono::NaiveDateTime::from_timestamp_opt(
+ now.as_secs() as i64,
+ now.subsec_nanos(),
+ )
+ .unwrap();
+ chrono::DateTime::from_naive_utc_and_offset(naive, chrono::Utc)
+}