Skip to content

Commit

Permalink
feat: add sample for CronJobs
Browse files Browse the repository at this point in the history
Signed-off-by: Thorsten Hans <[email protected]>
  • Loading branch information
ThorstenHans committed Jul 12, 2024
1 parent c6b27fc commit 8c3b100
Show file tree
Hide file tree
Showing 20 changed files with 1,636 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ If you haven't used Spin before, we highly recommend exploring the [Fermyon Deve

## Architectures & Patterns in this Repository

### CronJobs in Kubernetes
- [HTTP APIs and CronJob](./api-with-cronjob)

### CRUD APIs
- [Go CRUD API with persistence in SQLite](./http-crud-go-sqlite/)
- [JavaScript CRUD API with persistence in PostgreSQL](./http-crud-js-pg/)
Expand Down
2 changes: 2 additions & 0 deletions api-with-cronjob/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main.wasm
.spin/
60 changes: 60 additions & 0 deletions api-with-cronjob/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# HTTP API with CronJob

This sample illustrates how you can use the [Spin Command Trigger](https://github.com/fermyon/spin-trigger-command) to one time commands with Spin.

In the context of Kubernetes we can turn one-time commands into Jobs and CronJobs to perform individual tasks once or on a schedule.

The sample consists of two Spin Apps:

* API: A simple HTTP API that interacts with a key-value store
* CRON: A command app which wipes data from the key-value store

The API exposes the following endpoints

* `GET /value` - Returns the value of a counter from key-value store
* `POST /value` - Increments the counter in key-value store by `1`
* `DELETE /value` - Removes the counter from the key-value store
* `GET /` - Prints available API endpoints

The Command App leverages Spin's key-value store API to load all keys available in the key-value store and deletes them.

## Supported Platforms

- Local (`spin up`)
- SpinKube
- Fermyon Platform for Kubernetes

## Prerequisites

To use this sample you must have

- [Rust](https://www.rust-lang.org/) installed on your machine
- The `wasm32-wasi` target for Rust installed (`rustup target add wasm32-wasi`)
- [Spin](https://developer.fermyon.com/spin/v2/index) CLI installed on your machine
- [Command Trigger Plugin](https://github.com/fermyon/spin-trigger-command) must be installed


## Running the Sample

### Local (`spin up`)

Follow the steps outlined below to run the API and the Command App:

- Start the API using `spin up -f spin.toml --runtime-config-file ./local.rtc.toml`
- Increment the counter by sending `POST` requests to `/value`: `curl -X POST localhost:3000/value`
- Check the value of the counter using `curl localhost:3000/value` (you should see a value other than `0`)
- Run the Command App from a clean terminal instance using `spin up -f spin-cron.toml --runtime-config-file ./local.rtc.toml`
- Check the value of the counter using `curl localhost:3000/value` (it should now return `0`)

### Running on Kubernetes with SpinKube

The [`k8s`](./k8s) folder contains a `deploy.sh` script that you can use to deploy the sample to your Kubernetes cluster.

> You must have [SpinKube](https://spinkube.dev) installed
The script itself deploys the following artifacts to your cluster:

- Redis will be deployed to the `redis` namespace
- The Runtime Configuration File will be stored in the `rtc` secret in the `default` namespace
- The API will be deployed to the `default` namespace
- The Command App will be deployed as a CronJob to the `default` namespace and will be executed every 2nd minute
2 changes: 2 additions & 0 deletions api-with-cronjob/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.spin/
Loading

0 comments on commit 8c3b100

Please sign in to comment.