Python スクリプトを使用したトラディショナル バージョンのバッチ リコンサイルとポスト
Standard または Advanced ライセンスで利用可能。
ArcGIS ジオデータベース管理者は、Python スクリプトを使用して、通常は複数のジオプロセシング ツールを使用して実行する多数のタスクを自動化できます。 このトピックでは、管理者が夜間にスケジュールされたトラディショナル バージョンのリコンサイルを実行するプロセスについて説明します。
注意:
このワークフローはトラディショナル バージョニングのためのものです。 ブランチ バージョンニングにはトラディショナル バージョニングのような ADD および DELETE テーブルがないため、このトピックで説明される同じ管理タスクの多くは必要ありません。
管理者の多くは、リコンサイルを実行する際に、他のユーザーがデータベースに接続していないかどうかを確認します。 ArcPy 関数 ListUsers と DisconnectUser を使用すると、ジオデータベースに接続しているユーザーを管理者だけにすることができます。
接続しているユーザーの検索
まず、ListUsers 関数を使用して、ジオデータベースに接続しているユーザーを特定します。 ListUsers 関数を実行するには、ジオデータベース管理者で接続する必要があります。
# get a list of connected users.
userList = arcpy.ListUsers("C:\\Projects\\MyProject\\admin.sde")
接続しているユーザーのリストの解析
接続しているユーザーのリストを取得したら、ジオデータベースへの接続を切断する必要があることをそれらのユーザーに通知できます。 これを行うには、ユーザーおよびユーザーに関連付けられた電子メール アドレスのリストを取得します。
この例では、説明を簡略化するため、ジオデータベースに接続している各ユーザーが、それぞれの電子メール アドレスと同じベース名を持つと見なしています。 電子メール アドレスを決定する方法は、この例とは異なる方法に変更できます。
# get a list of usernames from the list of named tuples returned from ListUsers
userNames = [u.Name for u in userList]
# take the userNames list and make email addresses by appending the appropriate suffix.
emailList = [name + '@company.com' for name in userNames]
電子メールの生成と送信
電子メールのリストを使用して、Python からそれらのユーザーに電子メールを送信し、ジオデータベースへの接続を切断する必要があることを知らせます。 この例では、Python の smtplib モジュールを使用しますが、標準以外のモジュールを使用して電子メールを送信する方法もあります。
import smtplib
SERVER = "mailserver.yourcompany.com"
FROM = "SDE Admin <python@yourcompany.com>"
TO = emailList
SUBJECT = "Maintenance is about to be performed"
MSG = "Auto generated Message.\n\rServer maintenance will be performed in 15 minutes. Please log off."
# Prepare actual message
MESSAGE = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, MSG)
# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, MESSAGE)
server.quit()
ジオデータベースへの接続のブロック
ArcPy 関数 AcceptConnections をスクリプトで使用して、ジオデータベースへの接続をブロックします。 この関数は、Python スクリプトでのみ使用できます。
これによって、新しいユーザーがジオデータベースに接続するのを防ぎます。 既存の接続は切断されません。
#block new connections to the database.
arcpy.AcceptConnections('C:\\Projects\\MyProject\\admin.sde', False)
注意:
このメンテナンスを実行するために、データベースへの接続を禁止したり、すべてのユーザーを切断したりする必要はありません。 組織がすべての接続の切断に対応できる場合、圧縮プロセスをより効率的に実行できます。
スクリプトの一時停止
接続を切断する前にユーザーに作業を完了させる時間を与えるために、スクリプトを 15 分間一時停止する必要があります。 接続しているユーザーの切断前に 15 分間の猶予時間を与えるには、Python の time モジュールを使用します。
import time
time.sleep(900)#time is specified in seconds
ユーザーの切断
ArcPy 関数 DisconnectUser をスクリプトで使用して、ユーザーの接続を切断します。 この関数は、Python スクリプトでのみ使用できます。
ユーザーが通知を受け、スクリプトが 15 分間一時停止された後、接続は切断されます。
#disconnect all users from the database.
arcpy.DisconnectUser('C:\\Projects\\MyProject\\admin.sde', "ALL")
注意:
特定のユーザーの接続のみを切断する場合、それらのユーザーの接続 ID を、文字列または文字列の Python リストで指定します。 それらの ID は、ListUsers 関数から返されます。
バージョンのバッチ リコンサイルと変更内容のポスト
バージョンのリコンサイル ツールを使用して、エンタープライズ ジオデータベースのすべてのバージョンをリコンサイルしてポストできます。 このツールを使用して、ジオデータベース内のすべてのバージョン (ALL_VERSIONS)、またはターゲット バージョンを圧縮からブロックしているバージョン (BLOCKING_VERSIONS) のみを、ターゲット バージョンにリコンサイルできます。 このツールを使用すると、一度に複数のバージョンを適切な順序でリコンサイルしてポストできるため、圧縮を効率的に実行できます。 この例では、ジオデータベース管理者としてツールを実行しています。 ジオデータベース管理者として接続すると、ジオデータベース内のすべてのバージョンだけでなく、他のユーザーが保有しているプライベートのバージョンやプロテクトのバージョンもリコンサイルしてポストできます。
# Get a list of versions to pass into the ReconcileVersions tool.
versionList = arcpy.da.ListVersions('C:\\Projects\\MyProject\\admin.sde')
# Run the ReconcileVersions tool.
arcpy.management.ReconcileVersions('C:\\Projects\\MyProject\\admin.sde', "ALL_VERSIONS", "sde.DEFAULT", versionList, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST", "DELETE_VERSION", "c:/temp/reconcilelog.txt")
ジオデータベースの圧縮
リコンサイルと変更内容のポストが完了したら、ジオデータベースを圧縮して重複する情報を削除し、編集をビジネス テーブルに移行することが重要です。
# Run the compress tool.
arcpy.management.Compress('C:\\Projects\\MyProject\\admin.sde')
ジオデータベースへの接続の許可
これで、バージョンのリコンサイルとポストが完了し、ジオデータベースが圧縮されたため、ユーザーに接続を許可できます。
# Allow new connections to the database.
arcpy.AcceptConnections('C:\\Projects\\MyProject\\admin.sde', True)
インデックスの再構築と統計の更新
圧縮操作の実行後、インデックスの再構築と統計の更新を行うことをおすすめします。 これらの手順を実行するには、インデックスの再構築 ツールと データセットの分析 ツールを使用します。 これらのツールでは、データセットのリストの入力が可能であり、すべてのデータセットで同時に関数を実行できます。 これらのツールをジオデータベース管理者で実行すると、該当するシステム テーブルの統計情報が更新され、インデックスが再構築されます。 このプロセスの最初の部分では、データおよびデータを所有するユーザーのリストを取得してから、データ所有者としてインデックスと統計情報を更新します。
ユーザーが所有するデータのリストを特定した後、そのリストを インデックスの再構築 ツールと データセットの分析 ツールに渡すことができます。
データの所有者が複数存在する場合、データ所有者ごとにデータ リストを生成する必要があります。データ所有者ごとに接続して、インデックスの再構築 ツールと データセットの分析 ツールを実行しなければなりません。
# set the workspace
arcpy.env.workspace = "C:\\Projects\\MyProject\\user1.sde"
# Set a variable for the workspace
workspace = arcpy.env.workspace
# Get the username for the workspace
# this assumes you are using database authentication.
# OS authentication connection files do not have a 'user' property.
userName = arcpy.Describe(workspace).connectionProperties.user
# Get a list of all the datasets the user has access to.
# First, get all the stand alone tables, feature classes and rasters owned by the current user.
dataList = arcpy.ListTables('*.' + userName + '.*') + arcpy.ListFeatureClasses('*.' + userName + '.*') + arcpy.ListRasters('*.' + userName + '.*')
# Next, for feature datasets owned by the current user
# get all of the feature classes and add them to the primary list.
for dataset in arcpy.ListDatasets('*.' + userName + '.*'):
dataList += arcpy.ListFeatureClasses(feature_dataset=dataset)
# Pass in the list of datasets owned by the connected user to the rebuild indexes
# and update statistics on the data tables
arcpy.management.RebuildIndexes(workspace, "NO_SYSTEM", dataList, "ALL")
arcpy.management.AnalyzeDatasets(workspace, "NO_SYSTEM", dataList, "ANALYZE_BASE", "ANALYZE_DELTA", "ANALYZE_ARCHIVE")
注意:
ユーザーが所有するデータセットを制限するために使用されるワイルドカード トークンは、データベースに固有です。 上の例では、('*.' + userName + '.*') は、SQL Server、PostgreSQL、または DB2 で動作します。 Oracle の場合、ワイルドカードとして (userName + '.*') を使用できます。
完全なコードの例
以下のコード例では、上記のすべての項目を組み合わせて次の操作を、ジオデータベース管理者ユーザーで実行します。
接続しているユーザーを特定します。
電子メール通知を送信します。
ジオデータベースによる新しい接続の受け入れを禁止します。
ユーザーの接続を切断します。
バージョンのリコンサイルと変更内容のポストを行います。
ジオデータベースを圧縮します。
ジオデータベースによる新しい接続の受け入れを許可します。
システム テーブルのインデックスを再構築し、統計情報を更新します。
import arcpy, time, smtplib
Set the workspace.
arcpy.env.workspace = 'C:\Projects\MyProject\admin.sde'
Set a variable for the workspace.
adminConn = arcpy.env.workspace
Get a list of connected users.
userList = arcpy.ListUsers(adminConn)
Get a list of usernames of users currently connected and make email addresses.
emailList = [user.Name + "@yourcompany.com" for user in arcpy.ListUsers(adminConn)]
Take the email list and use it to send an email to connected users.
SERVER = "mailserver.yourcompany.com" FROM = "SDE Admin python@yourcompany.com" TO = emailList SUBJECT = "Maintenance is about to be performed" MSG = "Auto generated Message.\n\rServer maintenance will be performed in 15 minutes. Please log off."
Prepare actual message.
MESSAGE = """
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, MSG)
Send the email.
print("Sending email to connected users") server = smtplib.SMTP(SERVER) server.sendmail(FROM, TO, MESSAGE) server.quit()
Block new connections to the database.
print("The database is no longer accepting connections") arcpy.AcceptConnections(adminConn, False)
Wait 15 minutes.
time.sleep(900)
Disconnect all users from the database.
print("Disconnecting all users") arcpy.DisconnectUser(adminConn, "ALL")
Get a list of versions to pass into the ReconcileVersions tool.
Only reconcile versions that are children of Default.
print("Compiling a list of versions to reconcile") verList = arcpy.da.ListVersions(adminConn) versionList = [ver.name for ver in verList if ver.parentVersionName == 'sde.DEFAULT']
Run the ReconcileVersions tool.
print("Reconciling all versions") arcpy.management.ReconcileVersions(adminConn, "ALL_VERSIONS", "sde.DEFAULT", versionList, "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST", "DELETE_VERSION", "c:/temp/reconcilelog.txt")
Run the compress tool.
print("Running compress") arcpy.management.Compress(adminConn)
Allow the database to begin accepting connections again.
print("Allow users to connect to the database again") arcpy.AcceptConnections(adminConn, True)
Update statistics and indexes for the system tables.
Note: To use the "SYSTEM" option, the user must be an geodatabase or database administrator.
Rebuild indexes on the system tables.
print("Rebuilding indexes on the system tables") arcpy.management.RebuildIndexes(adminConn, "SYSTEM")
Update statistics on the system tables.
print("Updating statistics on the system tables") arcpy.management.AnalyzeDatasets(adminConn, "SYSTEM") print("Finished.")
## スクリプトの自動スケジュール設定 {#D21}
スクリプトが完成したら、オペレーティング システムのタスク スケジューラを使用してスケジュールを設定し、特定の時間に設定された時間間隔でスクリプトを実行できます。
## 関連トピック {#related-topics}
- [トラディショナル バージョンの管理](create-modify-and-delete-versions.md)
- [トラディショナル バージョニングの編集プロセス](the-version-editing-process.md)
- [トラディショナル バージョン変更の表示](using-the-version-changes-command.md)
- [トラディショナル バージョンへの編集のリコンサイルおよびポスト](reconcile-and-post-edits-to-a-version.md)
- [トラディショナル バージョンの競合の管理](manage-traditional-version-conflicts.md)
- [トラディショナル バージョンの管理](recommended-version-administration-workflow.md)