Skip to content

Commit

Permalink
feat: gen2 trigger function
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobdadams committed Aug 9, 2024
1 parent 9719c90 commit a8a415a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
with:
name: projectname-skid
runtime: python311
entry_point: main
entry_point: subscribe
source_dir: src/projectname
service_account: cloud-function-sa@${{ secrets.PROJECT_ID }}.iam.gserviceaccount.com
event_trigger_type: google.cloud.pubsub.topic.v1.messagePublished
Expand Down Expand Up @@ -111,7 +111,7 @@ jobs:
--time-zone=America/Denver \
--location=us-central1 \
--topic=$SCHEDULE_NAME-topic \
--message-body='{"run": "now"}' \
--message-body='foo' \
--quiet
deploy-prod:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ The first step in creating a skid is to create a new repository in GitHub that u
1. Clone the repo on your local computer
- Use GitHub Desktop or any terminal with the git cli installed.
- git cli commands:
- `cd c:\where\you\store\git\repos`
- `git clone https://github.com/agrc/projectname-skid`
- `cd c:\where\you\store\git\repos`
- `git clone https://github.com/agrc/projectname-skid`

## Initial Skid Development

Expand Down
27 changes: 13 additions & 14 deletions src/skidname/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
Run the SKIDNAME script as a cloud function.
"""
import base64
import json
import logging
import sys
Expand All @@ -12,6 +13,8 @@
from types import SimpleNamespace

import arcgis
import functions_framework
from cloudevents.http import CloudEvent
from palletjack import extract, load, transform, utils
from supervisor.message_handlers import SendGridHandler
from supervisor.models import MessageDetails, Supervisor
Expand Down Expand Up @@ -182,31 +185,27 @@ def process():
_remove_log_file_handlers(log_name, loggers)


def main(event, context): # pylint: disable=unused-argument
@functions_framework.cloud_event
def subscribe(cloud_event: CloudEvent) -> None:
"""Entry point for Google Cloud Function triggered by pub/sub event
Args:
event (dict): The dictionary with data specific to this type of
event. The `@type` field maps to
cloud_event (CloudEvent): The CloudEvent object with data specific to this type of
event. The `type` field maps to
`type.googleapis.com/google.pubsub.v1.PubsubMessage`.
The `data` field maps to the PubsubMessage data
in a base64-encoded string. The `attributes` field maps
to the PubsubMessage attributes if any is present.
context (google.cloud.functions.Context): Metadata of triggering event
including `event_id` which maps to the PubsubMessage
messageId, `timestamp` which maps to the PubsubMessage
publishTime, `event_type` which maps to
`google.pubsub.topic.publish`, and `resource` which is
a dictionary that describes the service API endpoint
pubsub.googleapis.com, the triggering topic's name, and
the triggering event type
`type.googleapis.com/google.pubsub.v1.PubsubMessage`.
Returns:
None. The output is written to Cloud Logging.
"""

#: This function must be called 'main' to act as the Google Cloud Function entry point. It must accept the two
#: arguments listed, but doesn't have to do anything with them (I haven't used them in anything yet).
#: This function must be called 'subscribe' to act as the Google Cloud Function entry point. It must accept the
#: CloudEvent object as the only argument.

#: You can get the message-body value from the Cloud Scheduler event sent via pub/sub to customize the run
if base64.b64decode(cloud_event.data["message"]["data"]).decode() == "foo":
pass

#: Call process() and any other functions you want to be run as part of the skid here.
process()
Expand Down

0 comments on commit a8a415a

Please sign in to comment.