Question

Pause and Resume Schedules for Look and Dashboards Example Scripts

  • 27 June 2021
  • 1 reply
  • 63 views

Userlevel 2
  • Looker Staff
  • 8 replies

Currently, there is no native solution within the UI to temporarily disable/resume schedules in Looker. However, we do have three workarounds listed in this article. This article provides two sample scripts with Python to interact with update_schedule_plan()

METHOD 1: Using Python’s requests module to interact with Looker API 

Here is the link to Google Colab if you want to run as a one-off task. 

import requests
import json

def login(instance, credential):

""" Return an access token to be used in subsequent API calls
An access token is only valid for 60 minutes and can not be extended"""

instance = instance
credential = credential
access_token = requests.post(instance+'/login', data=credential).json()
headers = {'Authorization': 'token ' + access_token['access_token']}
return headers


def update_schedule(look_id = 321, enabled = True):

""" Pause a schedule plan (enabled = False) or resume a schedule plan (enabled = True)

Args:
look_id (int): id of a look (https://company.looker.com/look/id)
enabled (boolean): set to "True" to resume schedule, and "False" to pause schedule

Returns: "Successfully updated all schedules for look id " (str)

Notes: Schedules with "enabled = False" will disappear from Admin > Schedules but its data
can be retrived in System Activity. Once schedules are resumed with "enabled = True", they
will appear in Admin > Schedules
"""

headers = login(instance = instance, credential = credential)

""" Change to <schedule_plans/dashboard/> or <schedule_plans/lookml_dashboard>
to retrieve schedule_plan_id for these two contents """
get_url = instance + '/api/4.0/scheduled_plans/look/' + str(look_id)
schedule_plans = requests.get(get_url,headers=headers).json()


""" Execute through loops for all schedule plans belonging to one Looker type (look or dashboards)"""
for i in range(0, len(schedule_plans)):
schedule_plan = schedule_plans[i]
schedule_plan['enabled'] = enabled
data = json.dumps(schedule_plan)
update_url = instance + '/scheduled_plans/' + str(schedule_plan['id'])
requests.patch(update_url, headers = headers, data = data)

return "Successfully updated all schedules for look id " + str(look_id)



# Put in look_id and change the enabled parameter

instance = 'https://yourcompany.looker.com:19999'

credential = {
'client_id': 'foo',
'client_secret': 'bar'
}

update_schedule(look_id = 321, enabled = False)

 


1 reply

Userlevel 2

METHOD 2: Using Looker Python SDK - “WriteScheduledPlan” method 

Here is the link to Google Colab

 

# Install Looker SDK
!pip install looker_sdk
import looker_sdk
from looker_sdk import models40 #using Looker 4.0
import os

# Environmental variables to login
os.environ['LOOKERSDK_BASE_URL'] = 'XXX'
os.environ['LOOKERSDK_CLIENT_ID'] = 'YYY'
os.environ['LOOKERSDK_CLIENT_SECRET'] = 'ZZZ'


sdk = looker_sdk.init40()


def update_schedule(look_id = 321, enabled = True):

"""
Args:
look_id (int): id of a look
enabled (boolean): set to "True" to resume schedule, and "False" to pause schedule

Notes: Schedules with "enabled = False" will disappear from Admin > Schedules but its data
can be retrieved in System Activity. Once schedules are resumed with "enabled = True", they
will reappear in Admin > Schedules
"""

""" Change to `sdk.scheduled_plans_for_dashboard` or `sdk.scheduled_plans_for_lookml_dashboard`
to retrieve schedule_plan_id for these two type of contents if needed"""
contents = sdk.scheduled_plans_for_look(look_id, all_users=True)

for i in range(0, len(contents)):
sdk.update_scheduled_plan(
scheduled_plan_id=contents[i]['id'],
body=models40.WriteScheduledPlan(
enabled = enabled
))

print("Successfully updated all schedules for look id " + str(look_id))


update_schedule(looker_id = 321, enabled = True)

 

Reply