-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[yaml-v2] Update Cron Methods page (#49)
- Loading branch information
Showing
1 changed file
with
55 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,87 @@ | ||
--- | ||
sidebar_position: 7 | ||
--- | ||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
# Cron Jobs | ||
|
||
A scheduled method or a cron job is a method that will run periodically. By using a specific cron syntax, you can define the frequency and timing for each method. | ||
|
||
With genezio decorators, you can set the method type to `cron` and the `cronString` accordingly to the desired frequency. | ||
:::tip | ||
The time specified in the cron strings is in UTC. You can use this [converter](https://dateful.com/convert/utc) to convert your local time to UTC. | ||
::: | ||
|
||
## Use scheduled methods in your project | ||
|
||
In the example below, the method `sayHiEveryMinute()` will be called every minute: | ||
|
||
<!-- {% code title="index.ts" %} --> | ||
|
||
```javascript title="index.ts" showLineNumbers | ||
import { GenezioDeploy, GenezioMethod } from "@genezio/types"; | ||
|
||
@GenezioDeploy() | ||
export class BackendService { | ||
@GenezioMethod({ type: "cron", cronString: "* * * * *" }) | ||
sayHiEveryMinute() { | ||
console.log("Hi!"); | ||
} | ||
} | ||
``` | ||
|
||
<!-- {% endcode %} --> | ||
There are two ways to declare a scheduled method: | ||
|
||
- Using decorators (only available for TypeScript and JavaScript projects) | ||
- Using the `genezio.yaml` configuration file (available for all supported languages, including TypeScript and JavaScript) | ||
|
||
<Tabs> | ||
<TabItem value="decorators" label="Decorators (TS/JS)"> | ||
```ts title="cron.ts" showLineNumbers | ||
import { GenezioDeploy, GenezioMethod } from "@genezio/types"; | ||
|
||
@GenezioDeploy() | ||
export class BackendService { | ||
@GenezioMethod({ type: "cron", cronString: "* * * * *" }) | ||
sayHiEveryMinute() { | ||
console.log("I will run every minute!"); | ||
} | ||
} | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="yaml" label="genezio.yaml"> | ||
```yaml title="genezio.yaml" showLineNumbers | ||
name: cron-example | ||
yamlVersion: 2 | ||
backend: | ||
path: . | ||
language: | ||
name: go | ||
classes: | ||
- path: cron.go | ||
methods: | ||
- name: sayHiEveryMinute | ||
type: cron | ||
cronString: * * * * * | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
|
||
### Cron strings examples | ||
|
||
Below, you have the most commonly used `cronString` examples: | ||
|
||
<table><thead><tr><th>Cron string</th><th>Description</th><th data-hidden></th><th data-hidden></th></tr></thead><tbody><tr><td><code>* * * * *</code></td><td>Triggers at every minute</td><td></td><td></td></tr><tr><td><code>0/5 * * * *</code></td><td>Triggers every 5 minutes</td><td></td><td></td></tr><tr><td><code>0 * * * *</code></td><td>Triggers every hour</td><td></td><td></td></tr><tr><td><code>0 8 * * *</code></td><td>Triggers every day at 8 a.m.</td><td></td><td></td></tr></tbody></table> | ||
| Cron string | Description | | ||
| ------------- | ---------------------------- | | ||
| `* * * * *` | Triggers at every minute | | ||
| `0/5 * * * *` | Triggers every 5 minutes | | ||
| `0 * * * *` | Triggers every hour | | ||
| `0 8 * * *` | Triggers every day at 8 a.m. | | ||
|
||
You can, also, [use crontab.guru](https://crontab.guru/) to build specific cron strings. | ||
|
||
## Examples using scheduled methods | ||
|
||
<!-- :::info --> | ||
|
||
:::info | ||
For more details, check out an example using [scheduled methods](https://github.com/Genez-io/genezio-examples/tree/master/javascript/cron). | ||
::: | ||
|
||
<!-- ::: --> | ||
|
||
## More details | ||
|
||
For more information on genezio decorators, check out [genezio-decorators](../project-structure/genezio-decorators "mention"). | ||
For more information on genezio decorators, check out [Genezio Decorators](../project-structure/genezio-decorators). | ||
|
||
## Next Steps | ||
|
||
Also, you can find more details on deploying the backend and frontend here: | ||
|
||
- [Backend Deployment](backend-deployment) | ||
- [Frontend Deployment](frontend-deployment) | ||
- [Backend Deployment](backend-deployment) | ||
- [Frontend Deployment](frontend-deployment) | ||
|
||
Now you are ready for some more advanced use cases: | ||
|
||
- [Web3 Application](https://genezio.com/blog/create-your-first-web3-app/) | ||
- [ChatGPT App](https://genezio.com/blog/create-your-first-app-using-chatgpt/) | ||
- [Shopping Cart Implementation](https://genezio.com/blog/implement-a-shopping-cart-using-typescript-redis-and-react/) | ||
- [Integrate Stripe Payments](https://genezio.com/blog/integrate-stripe-payments/) | ||
|
||
- [Web3 Application](https://genezio.com/blog/create-your-first-web3-app/) | ||
- [ChatGPT App](https://genezio.com/blog/create-your-first-app-using-chatgpt/) | ||
- [Shopping Cart Implementation](https://genezio.com/blog/implement-a-shopping-cart-using-typescript-redis-and-react/) | ||
- [Integrate Stripe Payments](https://genezio.com/blog/integrate-stripe-payments/) |