.../articles/
Running a Crawler with Cloud Functions, Pub/Sub, and Cloud Scheduler

Running a Crawler with Cloud Functions, Pub/Sub, and Cloud Scheduler

2019.10.14

We run an agriculture curation media outlet called qrop docs, and I wanted to automate a crawler that collects agriculture-related news articles as a serverless task. So this time I tried using Cloud Functions, Pub/Sub, and Cloud Scheduler. (Some of you might be thinking App Engine would work just as well — but I decided to give this a try as an experiment.)

1. Creating the file, deploying to Functions, and creating the Pub/Sub topic

This time I created main.py, which runs the crawler, as shown below. request['data'] contains the content set in the Cloud Scheduler payload. Since Cloud Functions currently has a maximum execution time of 9 minutes, I set it up so that the Scheduler passes an ID to run at that time, and the task is processed based on that ID.

./app/main.py

import argparse
import base64
from batch import crawler
import os

# for google cloud functions
def crawl(request, callback):
    id = int(base64.b64decode(request['data']))
    crawler.runForRss(id)

The command to deploy this file looks like this. --set-env-vars lets you set environment variables to be used in Functions, --timeout lets you set the maximum execution time for Functions. --trigger-resource sets the topic ID of the Pub/Sub you want to create. --entry-point specifies the name of the method in main.py you want to run. In this case, that's the crawl function written above.

$ gcloud functions deploy ${FUNCTION_NAME} \
		--source ./app --runtime python37 \
    --region asia-northeast1 --timeout 540 \
		--trigger-resource ${TRIGGER_NAME} \
    --trigger-event google.pubsub.topic.publish \
		--entry-point ${ENTRY_POINT_NAME} \
    --project $(PROD_PROJECT_ID)\
		--set-env-vars="API_URL"="$(PROD_API_URL)","BASIC_USER"="${BASIC_USER}","BASIC_PASSWORD"="$(BASIC_PASSWORD)"

Once you run the above, you can confirm the created topic name on the Pub/Sub page in the GCP console.

2. Creating the Cloud Scheduler job

Next, let's create a scheduler to run the Functions on a regular basis. Create it from the Cloud Scheduler page.

From "create job," you'll move to the screen below and register the job.

The frequency can be written the same way as cron, so if you want to run it at X:15 every 3 hours, you'd write 15 */3 * * *. Set the target to Pub/Sub, and set the topic to the name of the Pub/Sub topic we created earlier. For the payload, since I'm setting an ID at execution time this time, I've entered a numeric int value, but you should set whatever value fits your needs as a developer. (If I'm using this wrong, please let me know!)

Summary

Just by setting these things up, I was able to get Cloud Functions running on a regular schedule! For tasks where using something like App Engine feels like overkill, using Cloud Functions could be a good option. If I find any other good use cases, I'll write about them on the blog.

References

written by

.../article/

Articles

All articles

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

We moved our corporate site's hosting and CMS, and made it bilingual along the way. The constraints we only found by running against real data were more useful than the migration itself, so this post focuses on where we got stuck.

Can't Read POST Data with Firebase Functions × Remix?

Can't Read POST Data with Firebase Functions × Remix?

How to read POST data from a Remix action when running on Firebase Functions.

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Building a structure where executives and leaders themselves can identify issues and evaluate solutions using generative AI. We introduce how combining this with our hands-on support dramatically improves both the quality and speed of digital transformation.

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Notes on the obstacles we hit while deploying a Next.js app managed in a monorepo to AWS Amplify.

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Many production companies have adopted remote work as a result of the pandemic, and we are one of them.

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

There is no single formula for e-commerce design that sells. Driving revenue requires a solid concept, and getting to that concept requires thorough research.

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

With remote work becoming the norm during the COVID-19 pandemic, our team now works from home most days of the week.

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We hope this helps web designers who work in Figma. Read on for how to use it.

How to Build an E-Commerce Site, and Which Platforms We Recommend

How to Build an E-Commerce Site, and Which Platforms We Recommend

Shopping online for fashion, appliances, and even groceries is now routine. With the pandemic accelerating the shift, we receive a steady stream of questions about which platform to use and how much it costs.

Generating FastAPI Schema Classes from OpenAPI

Generating FastAPI Schema Classes from OpenAPI

We chose FastAPI, a relatively modern framework, for a Python API project. FastAPI can generate an OpenAPI definition from your backend code, but here we do the opposite: generating FastAPI schema classes from an OpenAPI definition prepared in advance.

View all articles

Contact us