Python スクリプトを使用したバージョンのバッチ リコンサイルとポスト

ArcGIS ジオデータベース管理者は、Python スクリプトを使用して、通常は複数のジオプロセシング ツールを使用して実行する多数のタスクを自動化できます。このトピックでは、管理者が夜間にスケジュールされたバージョンのリコンサイルを実行するプロセスについて説明します。

管理者の多くは、リコンサイルを実行する際に、他のユーザがデータベースに接続していないかどうかを確認します。ArcPy 関数 ListUsersDisconnectUser を使用すると、データベースに接続しているユーザを管理者だけにすることができます。

接続しているユーザの検索

ユーザを接続する最初のステップは、データベースに接続しているユーザの特定です。ListUsers 関数を使用して管理者接続を調べます。

# get a list of connected users.
userList = arcpy.ListUsers("Database Connections/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 モジュールを使用しますが、標準以外のモジュールを使用して電子メールを送信する方法もあります。

注意注意:

smtplib モジュールの詳細については、次の Web サイトにある Python のドキュメントをご参照ください。

http://docs.python.org/

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 関数 AcceptingConnections を使用します。この関数は、Python スクリプトでのみ使用できます。

ユーザが通知を受け、スクリプトが 15 分間一時停止された後、接続は切断されます。

#block new connections to the database.
arcpy.AcceptConnections('Database Connections/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('Database Connections/admin.sde', "ALL")
注意注意:

特定のユーザのみを切断する場合は、ListUsers 関数から返された ID を文字列として指定するか、または接続しているユーザのダイアログ ボックスの ID を文字列の Python リストとして入力し、該当するユーザを切断できます。

バッチ リコンサイル

[バージョンのリコンサイル(Reconcile Versions)] ツールを使用して、エンタープライズ ジオデータベースのすべてのバージョンをリコンサイルしてポストできます。このツールには、ジオデータベースのすべてのバージョンをターゲット バージョン、または固定デフォルトのバージョンのみにリコンサイルするオプションがあります。このツールを使用すると、一度に複数のバージョンを適切な順序でリコンサイルしてポストできるため、圧縮を効率的に実行できます。この例では、ジオデータベース管理者としてツールを実行しています。ジオデータベース管理者として接続すると、ジオデータベース内のすべてのバージョンばかりでなく、他のユーザが保有しているプライベートなバージョンや保護されているバージョンについてもリコンサイルとポストを実行できます。

バージョンのリコンサイル(Reconcile Versions)ジオプロセシング ツールの詳細

# Get a list of versions to pass into the ReconcileVersions tool.
versionList = arcpy.ListVersions('Database Connections/admin.sde')

# Execute the ReconcileVersions tool.
arcpy.ReconcileVersions_management('Database Connections/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.Compress_management('Database Connections/admin.sde')

インデックスの再構築と統計情報の更新

圧縮操作を実行した後、インデックスを再構築し、統計情報を更新することをお勧めします。これらのステップは、[インデックスの再構築(Rebuild Indexes)] ツールと [データセットの分析(Analyze Datasets)] ツールを使用して実行できます。これらのツールでは、入力データセットのリストの入力が可能であり、すべてのデータセットで同時に関数を実行します。また、該当するシステム テーブルでの統計情報の更新とインデックスの再構築も行います。このプロセスではまず、ユーザが所有するデータのリストを取得します。インデックスと統計情報を更新できるのは、データの所有者または特定の管理権限を付与されているユーザに限られます。

# set the workspace 
arcpy.env.workspace = 'Database Connections/admin.sde'

# Get the user name for the workspace
# this assumes you are using database authentication.
# OS authentication connection files do not have a 'user' property.
userName = arcpy.Describe(arcpy.env.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 featureclasses and add them to the master list.
for dataset in arcpy.ListDatasets('*.' + userName + '.*'):
    dataList += arcpy.ListFeatureClasses(feature_dataset=dataset)

ユーザが所有するデータのリストを特定した後、そのリストを [インデックスの再構築(Rebuild Indexes)] ツールと [データセットの分析(Analyze Datasets)] ツールに渡すことができます。

注意注意:

簡単にするために、このスクリプトでは管理(admin)ユーザがデータの所有者でもあることを前提としています。複数のデータ所有者が存在する場合は、データ所有者ごとにデータ リストを作成して、[インデックスの再構築(Rebuild Indexes)] ツールと [データセットの分析(Analyze Datasets)] ツールに渡す必要があります。

注意注意:

ユーザが所有するデータセットを制限するために使用されるワイルドカード トークンはオペレーティング システムに固有です。上の例で、('*.' + userName + '.*')は SQL Server、PostgreSQL、または DB2 で機能します。Oracle の場合、ワイルドカードとして(userName + '.*')を使用できます。Informix の場合、ワイルドカードとして('*:' + userName + '.*')を使用できます。

# Execute rebuild indexes and analyze datasets
# Note: to use the "SYSTEM" option the user must be an administrator.

workspace = "Database Connections/admin.sde"

arcpy.RebuildIndexes_management(workspace, "SYSTEM", dataList, "ALL")

arcpy.AnalyzeDatasets_management(workspace, "SYSTEM", dataList, "ANALYZE_BASE", "ANALYZE_DELTA", "ANALYZE_ARCHIVE")

完全なコードの例

以下のコード例では、上記のすべての項目を組み合わせて次の操作を実行します。

import arcpy, time, smtplib

# set the workspace 
arcpy.env.workspace = 'Database Connections/admin.sde'

# set a variable for the workspace
workspace = arcpy.env.workspace

# get a list of connected users.
userList = arcpy.ListUsers("Database Connections/admin.sde")

# get a list of usernames of users currently connected and make email addresses
emailList = [u.Name + "@yourcompany.com" for user in arcpy.ListUsers("Database Connections/admin.sde")]

# 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 mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, MESSAGE)
server.quit()

#block new connections to the database.
arcpy.AcceptConnections('Database Connections/admin.sde', False)

# wait 15 minutes
time.sleep(900)

#disconnect all users from the database.
arcpy.DisconnectUser('Database Connections/admin.sde', "ALL")

# Get a list of versions to pass into the ReconcileVersions tool.
versionList = arcpy.ListVersions('Database Connections/admin.sde')

# Execute the ReconcileVersions tool.
arcpy.ReconcileVersions_management('Database Connections/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.Compress_management('Database Connections/admin.sde')

#Allow the database to begin accepting connections again
arcpy.AcceptConnections('Database Connections/admin.sde', True)

#Get a list of datasets owned by the admin user

# Get the user name for the workspace
# this assumes you are using database authentication.
# OS authentication connection files do not have a 'user' property.
userName = arcpy.Describe(arcpy.env.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.
dataList = arcpy.ListTables('*.' + userName + '.*') + arcpy.ListFeatureClasses('*.' + userName + '.*') + arcpy.ListRasters('*.' + userName + '.*')

# Next, for feature datasets get all of the featureclasses
# from the list and add them to the master list.
for dataset in arcpy.ListDatasets('*.' + userName + '.*'):
    dataList += arcpy.ListFeatureClasses(feature_dataset=dataset)

# pass in the list of datasets owned by the admin to the rebuild indexes and analyze datasets tools
# Note: to use the "SYSTEM" option the user must be an administrator.
arcpy.RebuildIndexes_management(workspace, "SYSTEM", dataList, "ALL")

arcpy.AnalyzeDatasets_management(workspace, "SYSTEM", dataList, "ANALYZE_BASE", "ANALYZE_DELTA", "ANALYZE_ARCHIVE")

スクリプトの自動スケジュール設定

スクリプトが完成したら、オペレーティング システムのタスク スケジューラを使用してスケジュールを設定し、特定の時間に設定された時間間隔でスクリプトを実行できます。

Windows で実行するスケジュールされたタスクの設定については、「Python スクリプトを指定の時間に実行するようスケジュールを設定する」をご参照ください。

10/8/2012