This documentation is for Magento 1.x. For Magento 2.x, see here.
At the time of writing, Cloud Functions only supports three runtimes: Node.js 6, Node.js 8, and Python 3.7.

Example: Clean Visitor Logs

This example shows how to use Cloud Functions to clean the visitor logs from Magento.

Prerequisites

Downloads

To use the Python runtime, two files are required: main.py and requirements.txt. You can download them here:

mkdir -pv visitor_log_clean && \
curl -fsL \
     https://docs.auroraextensions.com/magento/extensions/1.x/magecroncloudfunctions/latest/python/src/visitor_log_clean/main.py
     > ./visitor_log_clean/main.py && \
curl -fsL \
     https://docs.auroraextensions.com/magento/extensions/1.x/magecroncloudfunctions/latest/python/src/visitor_log_clean/requirements.txt
     > ./visitor_log_clean/requirements.txt

Objective

Clean the database of visitor log entries using Cloud Functions.

Steps

  1. Create visitor_log_clean cloud function

    The following environment variables are required for Cloud Functions to access Cloud SQL:

    • DB_NAME: The name of the Magento database.
    • DB_USER: The name of the database user.
    • DB_PASS: The database user's password.
    • PROJECT_ID: The Google Cloud Platform project ID.
    • REGION_ID: The region where the Cloud SQL instance exists (e.g. us-central1, us-east4).
    • INSTANCE_NAME: The name of Cloud SQL instance.

    Using gcloud, run the following:

      
      gcloud functions deploy visitor_log_clean
                              --entry-point="log_clean" \
                              --runtime=python37 \
                              --set-env-vars=DB_NAME=<YOUR_DB_NAME>,DB_USER=<YOUR_DB_USER>,DB_PASS=<YOUR_DB_PASS>,PROJECT_ID=<GCP_PROJECT_ID>,REGION_ID=<CLOUD_SQL_REGION_ID>,INSTANCE_NAME=<CLOUD_SQL_INSTANCE_NAME> \
                              --source="./visitor_log_clean" \
                              --trigger-http
      
    

    Alternatively, you can use the Cloud Functions interface. Click Create Function from the list overview, and specify the following:

    • Name: visitor_log_clean
    • Memory: 256MB
    • Trigger: HTTP
    • Source code: Inline editor
    • Runtime: Python 3.7
    • main.py: Contents of main.py from above
    • requirements.txt: Contents of requirements.txt from above
    • Function to execute: log_clean

    Next, click More and set each of the environment variables listed above. Then click Create.

  2. Add entry to <crontab>

    To make use of visitor_log_clean from within Magento, you will need to add the following to your module config.xml file:

      
        <crontab>
          <jobs>
            <visitor_log_clean>
              <schedule>
                <cron_expr>30 2 * * *</cron_expr>
              </schedule>
              <run>
                <model>magecroncloudfunctions/observer::run</model>
              </run>
            </visitor_log_clean>
          </jobs>
        </crontab>
      
    

    Alternatively, you can use Aoe_Scheduler to create and schedule cron jobs. It's a substantially nicer solution and it provides a ton of additional benefits.