Skip to content

Commit da54b1f

Browse files
committed
Update the queries to use SQLalchemy #127
1 parent 8602fb3 commit da54b1f

1 file changed

Lines changed: 53 additions & 21 deletions

File tree

common/check_expired_locked_rules

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1-
#!/usr/bin/env python
2-
# Copyright European Organization for Nuclear Research (CERN) 2013
1+
#!/usr/bin/env python3
2+
# Copyright European Organization for Nuclear Research (CERN) since 2012
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
5-
# You may not use this file except in compliance with the License.
6-
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
77
#
8-
# Authors:
9-
# - Cedric Serfon, <cedric.serfon@cern.ch>, 2015
10-
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
1115
'''
1216
Probe to check the locked expired rules or datasets with locked rules
1317
'''
1418

1519
import sys
20+
from datetime import datetime
21+
22+
from sqlalchemy import and_, select
23+
from sqlalchemy.sql import null, true
24+
25+
from rucio.db.sqla import models
1626
from rucio.db.sqla.session import get_session
1727

1828
# Exit statuses
@@ -25,28 +35,50 @@ def main():
2535
'''
2636
status = OK
2737
session = get_session()
38+
39+
base_statement = select(
40+
models.ReplicationRule.id,
41+
models.ReplicationRule.name,
42+
models.ReplicationRule.scope,
43+
models.ReplicationRule.rse_expression,
44+
)
45+
2846
try:
29-
query = "select rawtohex(id), scope, name, rse_expression from atlas_rucio.rules where locked=1 and expires_at<sys_extract_utc(localtimestamp)"
30-
print 'Locked expired rules'
31-
for row in session.execute(query):
47+
expired_locked_rules = base_statement.where(
48+
and_(
49+
models.ReplicationRule.locked == true(),
50+
models.ReplicationRule.expires_at < datetime.utcnow() # see https://github.com/rucio/rucio/issues/6476
51+
)
52+
)
53+
for row in session.execute(expired_locked_rules):
3254
status = CRITICAL
33-
print row[0], row[1], row[2]
55+
print(row[0], row[1], row[2])
56+
3457
except Exception as error:
35-
print error
58+
print(error)
3659
status = UNKNOWN
3760
sys.exit(status)
3861
try:
39-
query = """select rawtohex(c.id), c.scope, c.name, c.rse_expression from atlas_rucio.rules c,
40-
(select a.scope, a.name from atlas_rucio.dids a
41-
where a.expired_at is not null and a.expired_at < sys_extract_utc(localtimestamp)
42-
and exists (select 1 from atlas_rucio.rules b where a.scope=b.scope and a.name=b.name and locked=1)) d
43-
where c.scope=d.scope and c.name=d.name and locked=1"""
44-
print 'Datasets expired with locked rules'
45-
for row in session.execute(query):
62+
dids_of_locked_expired_rules = base_statement.join(
63+
models.DataIdentifier,
64+
and_(
65+
models.ReplicationRule.scope == models.DataIdentifier.scope,
66+
models.ReplicationRule.name == models.DataIdentifier.name,
67+
),
68+
).where(
69+
and_(
70+
models.ReplicationRule.locked == true(),
71+
models.DataIdentifier.expired_at != null(),
72+
models.DataIdentifier.expired_at < datetime.utcnow() # see https://github.com/rucio/rucio/issues/6476
73+
)
74+
)
75+
print('Datasets expired with locked rules')
76+
for row in session.execute(dids_of_locked_expired_rules):
4677
status = CRITICAL
47-
print row[0], row[1], row[2], row[3]
78+
print(row[0], row[1], row[2], row[3])
79+
4880
except Exception as error:
49-
print error
81+
print(error)
5082
status = UNKNOWN
5183
sys.exit(status)
5284
sys.exit(status)

0 commit comments

Comments
 (0)