0
votes

I see that all of the examples per the documentation use some form of a simple web application (For example, Flask in Python). Is it possible to use cloud run as a non web application? For example, deploy cloud run to use a python script and then use GCP Scheduler to invoke cloud run every hour to run that script? Basically my thinking for this is to avoid having to deploy and pay for Compute Engine, and only pay for when the cloud run container is invoked via the scheduler.

2

2 Answers

5
votes

It's mandatory to answer to HTTP request. It's the contract of Cloud Run

  • Stateless (no volume attached to the container)
  • Answer to HTTP request

However, if you already have a python script, it's easy to wrap it in a flask webserver. Let's say, you have something like this (I assume that the file name is main.py -> important for the Dockerfile at the end)

import ....

var = todo(...)
connect = connect(...)
connect(var)

  1. Firstly, wrap it in a function like this
import ....

def my_function(request):
  var = todo(...)
  connect = connect(...)
  connect(var)
  return 'ok',200

  1. Secondly, add a flask server
from flask import Flask, request
import os
import ....

app = Flask(__name__)

@app.route('/')
def my_function(request):
  var = todo(...)
  connect = connect(...)
  connect(var)
  return 'ok',200

if __name__ == "__main__":
  app.run(host='0.0.0.0',port=int(os.environ.get('PORT',8080)))
  1. Add flask in your requirements.txt
  2. Build a standard container, here an example of Dockerfile
FROM python:3-alpine

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .
ENV PORT 8080

CMD [ "python", "main.py" ]
  1. Build (with Cloud Build for example) and deploy the service on Cloud Run

Now you have an URL, that you can call with Cloud Scheduler.

Be careful, the max request duration is, for now, limited to 15 minutes (soon 4x more) and limited to 2vCPU and 2Gb of memory (again, soon more).

0
votes

It depends what is being installed in the container image, as there is no requirement that one would have to install a web-server. For example, with such an image I can build Android applications, triggered whenever a repository changes (file excludes recommend) ...and likely could even run a head-less Android emulator for Gradle test tasks and publish test results to Pub/Sub (at least while the test-suite wouldn't run for too long). I mean, one has to understand the possibilities of Cloud Build to understand what Cloud Run can do.