|
| 1 | +# Cron Job Source example |
| 2 | + |
| 3 | +Cron Job Source example shows how to configure Cron Job as event source for |
| 4 | +functions. |
| 5 | + |
| 6 | +## Deployment Steps |
| 7 | + |
| 8 | +### Prerequisites |
| 9 | + |
| 10 | +1. Setup [Knative Serving](https://github.com/knative/docs/tree/master/serving). |
| 11 | +1. Setup |
| 12 | + [Knative Eventing](https://github.com/knative/docs/tree/master/eventing). |
| 13 | + |
| 14 | +### Create a Knative Service |
| 15 | + |
| 16 | +In order to verify `CronJobSource` is working, we will create a simple Knative |
| 17 | +Service that dumps incoming messages to its log. |
| 18 | + |
| 19 | +```yaml |
| 20 | +apiVersion: serving.knative.dev/v1alpha1 |
| 21 | +kind: Service |
| 22 | +metadata: |
| 23 | + name: message-dumper |
| 24 | +spec: |
| 25 | + runLatest: |
| 26 | + configuration: |
| 27 | + revisionTemplate: |
| 28 | + spec: |
| 29 | + container: |
| 30 | + image: github.com/knative/eventing-sources/cmd/message_dumper |
| 31 | +``` |
| 32 | +
|
| 33 | +Use following command to create the service from `service.yaml`: |
| 34 | + |
| 35 | +```shell |
| 36 | +kubectl apply -f service.yaml |
| 37 | +``` |
| 38 | + |
| 39 | +### Create Cron Job Event Source |
| 40 | + |
| 41 | +For each set of cron events you want to request, you need to create an Event |
| 42 | +Source in the same namespace as the destiantion. If you need a different |
| 43 | +ServiceAccount to create the Deployment, modify the entry accordingly in the |
| 44 | +yaml. |
| 45 | + |
| 46 | +```yaml |
| 47 | +apiVersion: sources.eventing.knative.dev/v1alpha1 |
| 48 | +kind: CronJobSource |
| 49 | +metadata: |
| 50 | + name: test-cronjob-source |
| 51 | +spec: |
| 52 | + schedule: "*/2 * * * *" |
| 53 | + data: '{"message": "Hello world!"}' |
| 54 | + sink: |
| 55 | + apiVersion: serving.knative.dev/v1alpha1 |
| 56 | + kind: Service |
| 57 | + name: message-dumper |
| 58 | +``` |
| 59 | + |
| 60 | +Use following command to create the event source from `cronjob-source.yaml`: |
| 61 | + |
| 62 | +```shell |
| 63 | +kubectl apply -f cronjob-source.yaml |
| 64 | +``` |
| 65 | + |
| 66 | +### Verify |
| 67 | + |
| 68 | +We will verify that the message was sent to the Knative eventing system by |
| 69 | +looking at message dumper logs. |
| 70 | + |
| 71 | +1. Use [`kail`](https://github.com/boz/kail) to tail the logs of the subscriber. |
| 72 | + |
| 73 | + ```shell |
| 74 | + kail -d message-dumper -c user-container --since=10m |
| 75 | + ``` |
| 76 | + |
| 77 | +You should see log lines similar to: |
| 78 | + |
| 79 | +```json |
| 80 | +{ |
| 81 | + "ID": "1543616460000180552-203", |
| 82 | + "EventTime": "2018-11-30T22:21:00.000186721Z", |
| 83 | + "Body": "{\"message\": \"Hello world!\"}" |
| 84 | +} |
| 85 | +``` |
0 commit comments