def schedule_job( client: scheduler_v1.CloudSchedulerClient, project_id: str, location_id: str, timezone: str, schedule: str, path: str, ) -> None: """ Schedules the given job for the specified project and location """ # Create a Job to schedule target = AppEngineHttpTarget(relative_uri=path, http_method="GET") job = Job(app_engine_http_target=target, schedule=schedule, time_zone=timezone) # Schedule the Job we just created parent = client.location_path(project_id, location_id) client.create_job(parent, job)
def create_source_cron_job(source_entity_id, datatype): create_cron_job_env = os.getenv("CREATE_CRONJOB", "false") create_cron_job_bool = json.loads(create_cron_job_env) if create_cron_job_bool is True: # create cron job project_id = os.environ["GOOGLE_CLOUD_PROJECT"] location_id = os.environ["LOCATION_ID"] dispatcher_topic = os.environ["TOPIC_DISPATCHER"] cron_job_message = { SOURCE_ENTITY_ID: source_entity_id, DATATYPE: datatype, } cron_job_message_json = json.dumps(cron_job_message) encoded_cron_job_message = cron_job_message_json.encode("utf-8") topic_name = f"projects/{project_id}/topics/{dispatcher_topic}" pubsub_target = PubsubTarget( topic_name=topic_name, data=encoded_cron_job_message, ) location = f"projects/{project_id}/locations/{location_id}" source_scheduler_job = Job( dict( name=f"{location}/jobs/{source_entity_id}", pubsub_target=pubsub_target, schedule="0 1 */10 * *", ) ) scheduler_client = CloudSchedulerClient() new_job = scheduler_client.create_job(parent=location, job=source_scheduler_job) return new_job else: print( f"Received a message for source entity id: {source_entity_id} " f" on staging or test env. Not creating a cron job" ) return None