FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 554b6b71 authored by E. Kirk's avatar E. Kirk
Browse files

feat: add cron schedule trigger option

parent b30a8f58
No related branches found
No related tags found
1 merge request!8feat: add cron schedule trigger option
# Change Log
## [0.3.0]
### Added
- Crontab style schedule option added to terraform module
## [0.2.0]
### Added
......
[tool.poetry]
name = "ucam-faas"
version = "0.2.0"
version = "0.3.0"
description = "Opinionated FaaS support framework extending Google's functions-framework"
authors = ["University of Cambridge Information Services <devops-wilson@uis.cam.ac.uk>"]
readme = "README.md"
......@@ -70,6 +70,10 @@ cwd = "terraform"
cmd = "logan --workspace=development terraform apply"
cwd = "terraform"
[tool.poe.tasks."terraform:dev"]
cmd = "logan --workspace=development terraform"
cwd = "terraform"
[tool.poetry.dependencies]
python = "^3.9.1"
requests = "^2.31.0"
......
......@@ -58,3 +58,32 @@ module "faas_service_set_retry" {
}
}
module "faas_service_cron" {
source = "./modules/faas"
name = "faas-test-cron"
project = local.project
function_container_image = "${local.container_images.function_base}:${local.container_images.function_tag}"
function = "example_cloud_event"
timeout_seconds = 20
triggers = {
cron_schedules = ["00 0 */7 * *", "0 12 */7 * *", ]
}
retry_count = 4
function_env = [
{
name = "TEST_VAR"
value = "WORKING"
}
]
concurrency = {
max_concurrent_functions = 1
}
}
......@@ -99,13 +99,19 @@ resource "google_cloud_run_v2_service" "faas" {
}
}
resource "google_pubsub_subscription" "provided_topic" {
locals {
for_each = { for i, v in [var.triggers.pubsub_topic_id] : i => v }
cron_schedules_supplied = length(var.triggers.cron_schedules) > 0
name = var.name
topic = var.triggers.pubsub_topic_id
}
resource "google_pubsub_subscription" "main" {
# Set count to aid adding non-pubsub based trigger if needed in the future
count = 1
name = var.name
topic = local.cron_schedules_supplied ? google_pubsub_topic.scheduler_pubsub[0].id : var.triggers.pubsub_topic_id
push_config {
push_endpoint = google_cloud_run_v2_service.faas.uri
......@@ -136,6 +142,32 @@ resource "google_pubsub_subscription" "provided_topic" {
}
resource "google_pubsub_topic" "scheduler_pubsub" {
count = local.cron_schedules_supplied ? 1 : 0
name = "${var.name}-cron"
project = var.project
}
resource "google_cloud_scheduler_job" "cron" {
for_each = { for index, cron in var.triggers.cron_schedules : index => cron }
name = "${var.name}-${each.key}"
project = var.project
schedule = each.value
time_zone = "Europe/London"
pubsub_target {
topic_name = google_pubsub_topic.scheduler_pubsub[0].id
data = base64encode(jsonencode({ "cron_schedule" : each.value }))
}
}
# FIXME when deadletter functionality added, ensure subscription exists
# Currently just used to provider retry count control
resource "google_pubsub_topic" "dead_letter" {
......
......@@ -114,15 +114,23 @@ variable "triggers" {
type = object({
pubsub_topic_id = optional(string, null)
cron_schedules = optional(list(string), [])
})
description = <<EOI
pubsub_topic_id - PUB/SUB topic ID to subscribe to and trigger function from.
cron_schedules - list of crontab style scheduler strings, e.g. ["30 16 * * 7"]
See http://man7.org/linux/man-pages/man5/crontab.5.html
EOI
validation {
condition = var.triggers.pubsub_topic_id != null
error_message = "pubsub_topic_id must be set"
condition = var.triggers.pubsub_topic_id != null || length(var.triggers.cron_schedules) > 0
error_message = "either pubsub_topic_id must be set or cron_schedules must be of length > 0"
}
validation {
condition = var.triggers.pubsub_topic_id == null || length(var.triggers.cron_schedules) < 1
error_message = "only one of pubsub_topic_id or cron_schedules must be set"
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment