Slide 1

Slide 1 text

Creating an Active Job Adapter for Cloud Run 2020/02/04 Tokyo Rubyist Meetup (Rubyist show and tell) @Impact Hub Tokyo

Slide 2

Slide 2 text

Hi there ● Gentaro Terada (@hibariya) ● A programmer who works at ESM, Inc. ● A college student (engineering) ● Likes Ruby, Internet, and Programming ● https://hibariya.org

Slide 3

Slide 3 text

Motivation: background jobs on GCP? When running Rails applications on GCP (GAE, GKE), where is some possibilities for the place to run its background workers: ● Use Compute Engine ● Create workers on an existing K8s cluster as a deployment ● Use Cloud Run (today I talk about it)

Slide 4

Slide 4 text

What is Cloud Run It provides a way to run containers on demand. “Cloud Run is a fully managed compute platform that automatically scales your stateless containers. Cloud Run is serverless: it abstracts away all infrastructure management, so you can focus on what matters most—building great applications.” https://cloud.google.com/run/

Slide 5

Slide 5 text

What can be done with Cloud Run ● To run background/batch jobs ● To run web apps ● To manage cloud resources (shutting down staging during nighttime)

Slide 6

Slide 6 text

How to invoke containers: HTTP Containers must have an HTTP interface. Each Cloud Run service has its endpoint URL that exposes the interface. However, from Rails applications, it’s nice for us if we were able to invoke it asynchronous via Active Job interface.

Slide 7

Slide 7 text

Run jobs asynchronous from Rails apps Cloud Run Cloud Tasks enqueue HTTP request

Slide 8

Slide 8 text

Active Job interface Active Job interfaces are required to implement only two methods: ● enqueue(job) ● enqueue_at(job, time) The interface itself is not so difficult: https://rip.hibariya.org/post/creating-an-active-job-adapter/

Slide 9

Slide 9 text

Active Job interface for Cloud Run github.com/esminc/activejob-google_cloud_tasks-http Implemented a simple Active Job interface to run jobs via Cloud Tasks.

Slide 10

Slide 10 text

ActiveJob::GoogleCloudTasks::HTTP It provides: ● An Active Job interface ● A simple Rack application to expose HTTP interface for Cloud Run It does not provide: ● Authentication mechanism for the Rack app

Slide 11

Slide 11 text

Usage: configure, implement, and mount

Slide 12

Slide 12 text

Run jobs asynchronous from Rails apps Cloud Run Cloud Tasks enqueue HTTP request POST /_jobs XXJob.perform_later

Slide 13

Slide 13 text

Summary ● Cloud Run provides a way to run containers on demand. ● We can even use it for Active Job backend with github.com/esminc/activejob-google_cloud_tasks-http . ● Implementing Active Job interface itself is not so difficult.