惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
有赞技术团队
有赞技术团队
V
Visual Studio Blog
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
Vercel News
Vercel News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
D
DataBreaches.Net
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
The Cloudflare Blog
宝玉的分享
宝玉的分享
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog

Deno

Deno 2.8 | Deno Claw Patrol: an open-source security firewall for agents | Deno Fresh 2.3: Zero JS by default, View Transitions, and Temporal support | Deno Deno 2.7: Temporal API, Windows ARM, and npm overrides | Deno Build a dinosaur runner game with Deno, pt. 6 | Deno Build a dinosaur runner game with Deno, pt. 5 | Deno Deno Deploy is Generally Available | Deno Introducing Deno Sandbox | Deno Build a dinosaur runner game with Deno, pt. 4 | Deno Build a dinosaur runner game with Deno, pt. 3 | Deno Build a dinosaur runner game with Deno, pt. 2 | Deno React / Next.js Denial-of-Service Vulnerability: Deno Deploy users protected | Deno Deno 2.6: dx is the new npx | Deno Build a dinosaur runner game with Deno, pt. 1 | Deno React Server Functions / Next.js Vulnerability: Deno Deploy users protected | Deno My highlights from the new Deno Deploy | Deno Deno's Other Open Source Projects | Deno How Deno protects against npm exploits | Deno Help Us Raise $200k to Free JavaScript from Oracle | Deno Deno 2.5: Permissions in the config file | Deno Fresh 2.0 Graduates to Beta, Adds Vite Support | Deno Deno 2.4: deno bundle is back | Deno JavaScript™ Trademark Update | Deno What's coming to JavaScript | Deno A brief history of JavaScript | Deno Reports of Deno's Demise Have Been Greatly Exaggerated | Deno An Update on Fresh | Deno How Plaid migrated 100 services to a new database platform 5x faster with Deno | Deno Deno 2.3: Improved deno compile, local npm packages, and more | Deno Add JSR packages with pnpm and Yarn | Deno
Announcing Deno Cron | Deno
2023-11-29 · via Deno

Building for the web is increasingly complex. Writing modern software includes leveraging cloud infrastructure, dissecting boilerplate code, and managing intricate configurations — when developers only want to focus on writing business logic.

Deno aims to radically simplify web development by removing config and unnecessary boilerplate. We’ve built Deno KV, a serverless database, and Deno Queues, a way to offload tasks or schedule future work, right into the runtime, so adding them to your application only requires a few lines of code.

Today, we’re thrilled to take another step in simplifying web development by introducing Deno Cron, an easy way to create scheduled jobs:

Deno.cron("Sample cron job", "*/10 * * * *", () => {
  console.log("This will run every 10 minutes");
});

In this post, we’ll go over:

  • Using Deno.cron
  • Deno Cron on Deno Deploy
  • How it works on Deno Deploy
  • What’s next?
Learn some tips and tricks about Deno.cron() in the above YouTube video.

Using Deno Cron

Deno.cron() (available behind the --unstable flag as of 1.38), is a function that takes three parameters:

  • name , the name of your scheduled job
  • schedule , which uses the Unix cron format and where the time is in the UTC timezone
  • handler, a function that is executed on the schedule provided

Unlike cron on UNIX/Linux, Deno Cron executions do not overlap. This means that if you schedule something to run every 10 minutes, but the task takes 30 minutes to complete, Deno Cron automatically skips the next scheduled run until the task is complete. Overlapping cron jobs can lead to unintended issues and requires extra tedious logic to avoid, but Deno Cron side steps that completely.

We’re also working to support a JavaScript friendly API for specifying the cron schedule.

Deno Cron on Deno Deploy

On Deno Deploy, our multi-tenant distributed serverless JavaScript platform, Deno.cron() is automatically detected and managed so you don’t need to worry about anything.

You can run cron jobs without a web server or even consistent incoming requests to keep your isolate alive. That’s because whenever your project is deployed, Deno Deploy automatically detects your cron jobs and evaluates them. When its time for your handler to run, Deno Deploy automatically spins up an isolate on-demand to run them.

This code works on Deno Deploy. Check out the playground.

We’ve also added a new Cron tab in the Deno Deploy dashboard, which shows all active cron jobs in your project:

The new Cron tab in your project settings will show your scheduled jobs detected by Deno Deploy.

A new cron tab in your Project that shows your active cron jobs.

Your cron jobs will also appear in your logs.

Your cron jobs will appear in your logs.

To modify or stop an existing cron, change your code and create a new deployment. For example, if you remove a  Deno.cron from your code and deploy it, those jobs will no longer be scheduled to run.

Your Deno Cron handlers can perform all sorts of actions, such as updating state in Deno KV, pinging a website, sending an email, initiating a database backup, calling an API at regular intervals, and more.

How does it work on Deno Deploy?

How exactly does Deno Deploy know there’s a cron in your code, even when there’s no web server handling requests?

When a new production deployment of your project is created, an ephemeral V8 isolate is used to evaluate your project’s top-level scope and to discover any Deno.cron definitions. A global cron scheduler is then updated with your project’s latest cron definitions, which includes updates to your existing crons, new crons, and deleted crons.

The global cron scheduler is a reliable service that’s responsible for scheduling and dispatching cron jobs based on the specified schedule. During dispatch, an on-demand v8 isolate is spun up to execute the job using the same production deployment.

These 24 lines of code creates a cron job to fetch weather data from a public API and adds it to Deno KV every hour. The web server endpoint prints out all of the timestamps and weather data.

Using Deno Cron on Deno Deploy makes it simple to create cron jobs and host them in the cloud without any configuration in a matter of minutes.

Other resources

What’s next?

Building a scalable business requires the ability to schedule jobs reliably and easily. Deno Cron is a simple way to schedule jobs without unnecessary configuration.

With Deno Cron, Deno KV, Deno Queues, npm, and web standards APIs, building and launching a production-ready server on Deno is simpler and more productive. But we are not there yet. We have a few more cloud primitives that we hope to add to the runtime — stay tuned.

Join the discussion on Hacker News.

🚨️ We’re actively seeking feedback for Deno Cron 🚨️

If you’re using it (or plan to use it) in a commercial capacity and want to connect directly to one of our engineers for technical support, please let us know via email or Discord.