def execute(self,job,previous_state): p = job.publish #import ipdb;ipdb.set_trace() # Write JSON output file json_out = {} json_out["name"] = p.table_name json_out["job_id"] = job.id json_out["job_batch_id"] = job.batch_id json_out["schema"] = p.workspace.publish_schema json_out["data_schema"] = p.workspace.publish_data_schema json_out["outdated_schema"] = p.workspace.publish_outdated_schema json_out["workspace"] = p.workspace.name json_out["channel"] = p.workspace.publish_channel.name json_out["spatial_data"] = SpatialTable.check_spatial(job.publish.spatial_type) json_out["spatial_type"] = SpatialTable.get_spatial_type_desc(job.publish.spatial_type) json_out["sync_postgres_data"] = p.workspace.publish_channel.sync_postgres_data json_out["sync_geoserver_data"] = p.workspace.publish_channel.sync_geoserver_data json_out["dump_path"] = "{}{}".format(BorgConfiguration.MASTER_PATH_PREFIX, p.pgdump_file.path) json_out["data_md5"] = file_md5(p.pgdump_file.path) json_out["preview_path"] = "{}{}".format(BorgConfiguration.MASTER_PATH_PREFIX, settings.PREVIEW_ROOT) json_out["applications"] = ["{0}:{1}".format(o.application,o.order) for o in Application_Layers.objects.filter(publish=p)] json_out["title"] = p.title json_out["abstract"] = p.abstract json_out["allow_authenticated"] = p.workspace.allow_authenticated if p.geoserver_setting: json_out["geoserver_setting"] = json.loads(p.geoserver_setting) if p.workspace.publish_channel.sync_geoserver_data and p.style_file: json_out["style_path"] = "{}{}".format(BorgConfiguration.MASTER_PATH_PREFIX, p.style_file.path) json_out["style_md5"] = file_md5(p.style_file.path) #bbox if SpatialTable.check_spatial(job.publish.spatial_type): cursor=connection.cursor() st = SpatialTable.get_instance(cursor,p.workspace.schema,p.table_name,True) if st.geometry_columns: json_out["bbox"] = st.geometry_columns[0][2] elif st.geography_columns: json_out["bbox"] = st.geography_columns[0][2] #create the dir if required if not os.path.exists(os.path.dirname(p.output_filename_abs)): os.makedirs(os.path.dirname(p.output_filename_abs)) with open(p.output_filename_abs, "wb") as output: json.dump(json_out, output, indent=4) # Try and add file to repository, if no changes then continue hg = hglib.open(BorgConfiguration.BORG_STATE_REPOSITORY) try: hg.add(files=[p.output_filename_abs]) hg.commit(include=[p.output_filename_abs],addremove=True, user=BorgConfiguration.BORG_STATE_USER, message="{} - updated {}.{}".format(p.job_batch_id, p.workspace.name, p.name)) except hglib.error.CommandError as e: if e.out != "nothing changed\n": return (HarvestStateOutcome.failed, self.get_exception_message()) finally: hg.close() return (HarvestStateOutcome.succeed, None)
def refresh(self,time=None): self.try_begin_transaction("livelayer_refresh") try: time = time or timezone.now() if Layer.is_system_table(self.table): return False st = SpatialTable.get_instance(self.datasource.schema,self.table,refresh=True,bbox=True,crs=True,dbUtil=self.datasource.dbUtil) self.last_refresh_time = time self.crs = st.crs self.bbox = json.dumps(st.bbox) self.spatial_type = st.spatial_type sql = self.datasource.dbUtil.get_create_table_sql(self.table,self.datasource.schema) if not self.sql or self.sql != sql: self.last_modify_time = time self.status = self.next_status(ResourceAction.UPDATE) self.sql = sql self.save() return True finally: self.try_clear_transaction("livelayer_refresh")