<s> import logging <EOL> import subprocess <EOL> import re <EOL> import time <EOL> import datetime <EOL> from django . conf import settings <EOL> from django . core . management . base import BaseCommand <EOL> from monitor . models import Table <EOL> logger = logging . getLogger ( __name__ ) <EOL> class Command ( BaseCommand ) : <EOL> def handle ( self , * args , ** options ) : <EOL> count_period = settings . COUNT_PERIOD <EOL> count_start_hour = settings . COUNT_START_HOUR <EOL> count_end_hour = settings . COUNT_END_HOUR <EOL> logger . info ( "<STR_LIT>" + str ( count_start_hour ) + "<STR_LIT>" + str ( count_end_hour ) + "<STR_LIT>" + str ( count_period ) + "<STR_LIT>" ) <EOL> self . wait_to_next_day ( count_start_hour ) <EOL> while True : <EOL> period_start_time = datetime . datetime . now ( ) <EOL> tables = Table . objects . filter ( is_count_rows = True ) <EOL> for table in tables : <EOL> table_name = table . name <EOL> cluster_name = table . cluster . name <EOL> count = self . count_rows ( cluster_name , table_name ) <EOL> if count == - <NUM_LIT:1> : <EOL> logger . info ( "<STR_LIT>" ) <EOL> else : <EOL> table . rows = count <EOL> table . last_update_time = datetime . datetime . now ( ) <EOL> table . save ( ) <EOL> logger . info ( "<STR_LIT>" + table . rows + "<STR_LIT>" ) <EOL> if datetime . datetime . now ( ) . hour >= count_end_hour : <EOL> logger . info ( "<STR_LIT>" ) <EOL> self . wait_to_next_day ( count_start_hour ) <EOL> next_start_time = period_start_time + datetime . timedelta ( days = count_period ) <EOL> sleep_time = ( next_start_time - datetime . datetime . now ( ) ) . total_seconds ( ) <EOL> logger . info ( "<STR_LIT>" + str ( sleep_time ) + "<STR_LIT>" ) <EOL> time . sleep ( sleep_time ) <EOL> def wait_to_next_day ( self , hour ) : <EOL> logger . info ( "<STR_LIT>" + str ( hour ) + "<STR_LIT>" ) <EOL> now = datetime . datetime . now ( ) <EOL> next_day = datetime . datetime ( now . year , now . month , now . day + <NUM_LIT:1> , hour , <NUM_LIT:0> ) <EOL> sleep_time = ( next_day - now ) . total_seconds ( ) <EOL> logger . info ( "<STR_LIT>" + str ( sleep_time ) + "<STR_LIT>" ) <EOL> time . sleep ( sleep_time ) <EOL> def count_rows ( self , cluster_name , table_name ) : <EOL> logger . info ( "<STR_LIT>" + table_name + "<STR_LIT>" + cluster_name ) <EOL> try : <EOL> deploy_command = settings . DEPLOY_COMMAND <EOL> command_list = [ deploy_command , "<STR_LIT>" , "<STR_LIT>" ] + [ cluster_name ] + [ "<STR_LIT>" ] + [ table_name ] + [ "<STR_LIT>" ] <EOL> rowcounter_process = subprocess . Popen ( command_list , stdout = subprocess . PIPE ) <EOL> rowcounter_result = rowcounter_process . communicate ( ) <EOL> rowcounter_status = rowcounter_process . wait ( ) <EOL> pattern = table_name + "<STR_LIT>" ; <EOL> compiled_pattern = re . compile ( pattern ) <EOL> re_result = compiled_pattern . search ( rowcounter_result [ <NUM_LIT:0> ] ) <EOL> return re_result . group ( <NUM_LIT:1> ) <EOL> except : <EOL> logger . error ( "<STR_LIT>" ) <EOL> return - <NUM_LIT:1> </s>
