Beispiel #1
0
def create_scheduler_job(
        project_id,
        topic,
        schedule='* * * * *',
        location_id='us-central1'):
    """Create a job with a PubSub topic via the Cloud Scheduler API"""

    # Create a client.
    client = scheduler.CloudSchedulerClient()

    # Construct the fully qualified location path.
    parent = f"projects/{project_id}/locations/{location_id}"

    # Construct the request body.
    job = {
        'pubsub_target': {
            'topic_name': f'projects/{project_id}/topics/{topic}',
            'data': '{}'.encode("utf-8")
        },
        'schedule': schedule,
        'time_zone': 'America/Los_Angeles'
    }

    # Use the client to send the job creation request.
    response = client.create_job(
        request={
            "parent": parent,
            "job": job
        }
    )

    logging.debug('Created scheduler job: {}'.format(response.name))
    return response
Beispiel #2
0
def schedule_crons():
    """Endpoint for scheduling cron jobs for updating imports upon
    GitHub commits."""
    task_info = flask.request.get_json(force=True)
    if 'COMMIT_SHA' not in task_info:
        return 'COMMIT_SHA not found'
    task_configs = task_info.get('configs', {})
    config = configs.ExecutorConfig(**task_configs)
    import_scheduler = update_scheduler.UpdateScheduler(
        client=scheduler.CloudSchedulerClient(),
        github=github_api.GitHubRepoAPI(
            repo_owner_username=config.github_repo_owner_username,
            repo_name=config.github_repo_name,
            auth_username=config.github_auth_username,
            auth_access_token=config.github_auth_access_token),
        config=config)
    return dataclasses.asdict(
        import_scheduler.schedule_on_commit(task_info['COMMIT_SHA']))
Beispiel #3
0
def delete_scheduler_job(project_id, location_id, job_id):
    """Delete a job via the Cloud Scheduler API"""
    # [START cloud_scheduler_delete_job]
    from google.cloud import scheduler
    from google.api_core.exceptions import GoogleAPICallError

    # Create a client.
    client = scheduler.CloudSchedulerClient()

    # TODO(developer): Uncomment and set the following variables
    # project_id = 'PROJECT_ID'
    # location_id = 'LOCATION_ID'
    # job_id = 'JOB_ID'

    # Construct the fully qualified job path.
    job = client.job_path(project_id, location_id, job_id)

    # Use the client to send the job deletion request.
    try:
        client.delete_job(job)
        print("Job deleted.")
    except GoogleAPICallError as e:
        print("Error: %s" % e)
Beispiel #4
0
def create_scheduler_job(project_id, location_id, service_id):
    """Create a job with an App Engine target via the Cloud Scheduler API"""
    # [START cloud_scheduler_create_job]
    from google.cloud import scheduler

    # Create a client.
    client = scheduler.CloudSchedulerClient()

    # TODO(developer): Uncomment and set the following variables
    # project_id = 'PROJECT_ID'
    # location_id = 'LOCATION_ID'
    # service_id = 'my-service'

    # Construct the fully qualified location path.
    parent = client.location_path(project_id, location_id)

    # Construct the request body.
    job = {
        'app_engine_http_target': {
            'app_engine_routing': {
                'service': service_id
            },
            'relative_uri': '/log_payload',
            'http_method': 'POST',
            'body': 'Hello World'.encode()
        },
        'schedule': '* * * * *',
        'time_zone': 'America/Los_Angeles'
    }

    # Use the client to send the job creation request.
    response = client.create_job(parent, job)

    print('Created job: {}'.format(response.name))
    # [END cloud_scheduler_create_job]
    return response