Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

trigger cloud run job not scheduling

Good day,

 

I would like that when someone uploads something new to a bucket (can be 1 or several files at a time) that event triggers the execution of Cloud Run Jobs (can not be scheduled, it has to be by an event) could someone help me figure out how to do that ? please and by the way if anyone knows how I could read the event when a cloud run job ends, I would appreciate it.

 

(scenario: I upload one or more images to a bucket, the cloud run job processes them and after the cloud run job finishes the images have to be moved to another bucket so that they are not reanalyzed again.)

 

thanks in advance

0 1 1,871
1 REPLY 1

Hi @gpaanibal777,

You can trigger a Cloud Run Jobs using Trigger with Events.

Cloud Storage:

gcloud eventarc triggers create TRIGGER \
  --location=LOCATION \
  --destination-run-service=DESTINATION_RUN_SERVICE \
  --destination-run-region=DESTINATION_RUN_REGION \
  --event-filters="type=EVENT_FILTER_TYPE" \
  --event-filters="bucket=BUCKET" \
  --service-account=PROJECT_NUMBER-compute@developer.gserviceaccount.com

Replace the following:

  • TRIGGER: the ID of the trigger or a fully qualified identifier
  • LOCATION: the location of the Eventarc trigger. Alternatively, set the eventarc/location property; for example, gcloud config set eventarc/location us-central1.

     

    Cloud Storage triggers for Eventarc are available in single-region, dual-region, and multi-region locations. Note that the trigger must be in the same location as the Cloud Storage bucket.

  • DESTINATION_RUN_SERVICE: the name of the Cloud Run service that receives the events for the trigger. The service can be in any of the Cloud Run supported locations and does not need to be in the same location as the trigger. However, the service must be in the same project as the trigger and will receive events as HTTP POST requests sent to its root URL path (/), whenever the event is generated.
  • DESTINATION_RUN_REGION: the region in which the destination Cloud Run service can be found. If not specified, it is assumed that the service is in the same region as the trigger.
  • EVENT_FILTER_TYPE: the identifier of the Cloud Storage event and can be one of the following:
    • google.cloud.storage.object.v1.finalized: event is sent when a new object is created (or an existing object is overwritten, and a new generation of that object is created) in the bucket
    • google.cloud.storage.object.v1.archived: event is sent when a live version of an object is archived or deleted. This event is only sent for versioning buckets.
    • google.cloud.storage.object.v1.deleted: event is sent when an object is permanently deleted. Depending on the object versioning setting for a bucket this means:
      • For versioning buckets, this is only sent when a version is permanently deleted (but not when an object is archived).
      • For non-versioning buckets, this is sent when an object is deleted or overwritten.
    • google.cloud.storage.object.v1.metadataUpdated: event is sent when the metadata of an existing object changes.
  • BUCKET: the globally unique identifier of the Cloud Storage bucket
  • PROJECT_NUMBER: your Google Cloud project number

Notes:

  • These flags are required:
    • --event-filters="type=EVENT_FILTER_TYPE"
    • --event-filters="bucket=BUCKET"
  • After a trigger is created, EVENT_FILTER_TYPE can't be changed. For a different event type, you must create a new trigger.
  • Events are delivered using Pub/Sub notifications from Cloud Storage. Setting up too many notifications registered against the same bucket might exhaust the notification limit for the bucket as indicated by the error Cloud Storage bucket ...: Pub/Sub notification limit reached. The bucket can have up to 10 notification configurations set to trigger for a specific event. See more quotas and limitations in the Cloud Storage quotas and limits page.
  • Each trigger can have multiple event filters, comma delimited in one --event-filters=[ATTRIBUTE=VALUE,...] flag, or you can repeat the flag to add more filters. Only events that match all the filters are sent to the destination. Wildcards and regular expressions are not supported.
  • The --service-account flag is used to specify the Identity and Access Management (IAM) service account email associated with the trigger.
  • Optionally, specify a relative path on the destination Cloud Run service to which the events for the trigger should be sent by using the --destination-run-path flag.
  • The Cloud Storage bucket must reside in the same Google Cloud project and region or multi-region as the Eventarc trigger.

Example:

gcloud eventarc triggers create helloworld-trigger \
    --location=us-central1 \
    --destination-run-service=helloworld-events \
    --destination-run-region=us-central1 \
    --event-filters="type=google.cloud.storage.object.v1.finalized" \
    --event-filters="bucket=my-project-bucket" \
    --service-account=${PROJECT_NUMBER}-compute@developer.gserviceaccount.com

This command creates a trigger called helloworld-trigger for the Cloud Storage bucket my-project-bucket and the event identified as google.cloud.storage.object.v1.finalized.

If you need more help you may contact Google Cloud Support.

Thanks