|
| 1 | +# Copyright 2019 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the 'License'); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an 'AS IS' BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +def create_scheduler_job(project_id, location_id, service_id): |
| 17 | + """Create a job with an App Engine target via the Cloud Scheduler API""" |
| 18 | + # [START cloud_scheduler_create_job] |
| 19 | + from google.cloud import scheduler |
| 20 | + |
| 21 | + # Create a client. |
| 22 | + client = scheduler.CloudSchedulerClient() |
| 23 | + |
| 24 | + # TODO(developer): Uncomment and set the following variables |
| 25 | + # project_id = 'PROJECT_ID' |
| 26 | + # location_id = 'LOCATION_ID' |
| 27 | + # service_id = 'my-service' |
| 28 | + |
| 29 | + # Construct the fully qualified location path. |
| 30 | + parent = client.location_path(project_id, location_id) |
| 31 | + |
| 32 | + # Construct the request body. |
| 33 | + job = { |
| 34 | + 'app_engine_http_target': { |
| 35 | + 'app_engine_routing': { |
| 36 | + 'service': service_id |
| 37 | + }, |
| 38 | + 'relative_uri': '/log_payload', |
| 39 | + 'http_method': 'POST', |
| 40 | + 'body': 'Hello World'.encode() |
| 41 | + }, |
| 42 | + 'schedule': '* * * * *', |
| 43 | + 'time_zone': 'America/Los_Angeles' |
| 44 | + } |
| 45 | + |
| 46 | + # Use the client to send the job creation request. |
| 47 | + response = client.create_job(parent, job) |
| 48 | + |
| 49 | + print('Created job: {}'.format(response.name)) |
| 50 | + # [END cloud_scheduler_create_job] |
| 51 | + return response |
| 52 | + |
| 53 | + |
| 54 | +def delete_scheduler_job(project_id, location_id, job_id): |
| 55 | + """Delete a job via the Cloud Scheduler API""" |
| 56 | + # [START cloud_scheduler_delete_job] |
| 57 | + from google.cloud import scheduler |
| 58 | + from google.api_core.exceptions import GoogleAPICallError |
| 59 | + |
| 60 | + # Create a client. |
| 61 | + client = scheduler.CloudSchedulerClient() |
| 62 | + |
| 63 | + # TODO(developer): Uncomment and set the following variables |
| 64 | + # project_id = 'PROJECT_ID' |
| 65 | + # location_id = 'LOCATION_ID' |
| 66 | + # job_id = 'JOB_ID' |
| 67 | + |
| 68 | + # Construct the fully qualified job path. |
| 69 | + job = client.job_path(project_id, location_id, job_id) |
| 70 | + |
| 71 | + # Use the client to send the job deletion request. |
| 72 | + try: |
| 73 | + client.delete_job(job) |
| 74 | + print("Job deleted.") |
| 75 | + except GoogleAPICallError as e: |
| 76 | + print("Error: %s" % e) |
| 77 | + # [END cloud_scheduler_delete_job] |
0 commit comments