def cobble_unit(state_abbrev, is_deliverable, is_main):
    log_file = '{}_{}_{}_{}'.format(log_name, state_abbrev,
                                    'Delivered' if is_deliverable else 'Approved',
                                    'Main' if is_main else 'Service')
    state_logger = cobb_common.setup_logging(log_file)
    state_logger.info("Starting {}...".format(log_file))

    app = win32com.client.Dispatch("Miner.Framework.Dispatch.MMAppInitializeDispatch")
    runtime = win32com.client.Dispatch("Miner.Framework.Dispatch.MMRuntimeEnvironmentDispatch")
    au = win32com.client.Dispatch('Miner.Framework.Dispatch.MMAutoupdaterDispatch')
    runtime.RuntimeMode = 0x4  # mmRuntimeModeArcServer
    app.Initialize(0x5)
    au.AutoUpdaterMode = 0x8  # mmAUMNoEvents
    # au.AutoUpdaterMode = 0x4  # mmAUMStandAlone

    dest_env = r"connections\{}_sde.sde".format(
        get_gps_db(state_abbrev) if is_deliverable else get_gis_db(state_abbrev))

    time_travel_amount = get_time_travel(is_deliverable)
    qa_where = get_qa_where(is_deliverable)

    source_fc = dest_env + '/NISOURCE.GPS_GasMain_Point' if is_main else dest_env + '/NISOURCE.GPS_ServiceLine_G_Point'
    dest_fc = dest_env + '/NISOURCE.GPS_GasMain_Line' if is_main else dest_env + '/NISOURCE.GPS_ServiceLine_G_Line'

    fields = main_point_fields if is_main else service_point_fields

    delta = 20 if is_main else 15

    # print 'Cobble'
    cobble_fc(
        state_logger,
        dest_env,
        state_abbrev,
        source_fc,
        dest_fc,
        qa_where,
        fields,
        pipe_line_fields,
        delta,
        is_deliverable,
        time_travel_amount)

    state_logger.info("Completed")

    app.Shutdown
    del app, runtime, au
def main(state):
    log_name = "Cobbler_{}".format(state)

    main_logger = cobb_common.setup_logging(log_name)
    main_logger.info("Starting {}...".format(state))

    start = time.clock()
    main_logger.info(datetime.datetime.now())

    cobbler.cobble_unit(state, True, True)  # GPS Main
    cobbler.cobble_unit(state, True, False)  # GPS Service
    cobbler.cobble_unit(state, False, True)  # GIS Main
    cobbler.cobble_unit(state, False, False)  # GIS Service

    main_logger.info(datetime.datetime.now())
    elapsed = (time.clock() - start)
    main_logger.info("Execution time: {}".format(elapsed))
    main_logger.info('Done {}'.format(state))
    #     'PA',
    #     'VA'
    # ]

    args_by_db = [
        # ('KY', True),
        ('OH', True),
        ('PA', True),
        ('VA', True),
        # ('KY', False),
        ('OH', False),
        ('PA', False),
        ('VA', False),
    ]

    main_logger = cobb_common.setup_logging("Cobbler")
    main_logger.info("Starting...")

    start = time.clock()

    pool = ThreadPool(4)
    # pool.map(worker, states)
    pool.map(worker_by_db, args_by_db)

    pool.close()
    pool.join()

    # processes = []
    # for curr_state in states:
    #     p1 = multiprocessing.Process(target=worker, args=(curr_state,))
    #     p1.start()
        dest_fc,
        qa_where,
        fields,
        pipe_line_fields,
        delta,
        is_deliverable,
        time_travel_amount)

    state_logger.info("Completed")

    app.Shutdown
    del app, runtime, au


if __name__ == "__main__":
    main_logger = cobb_common.setup_logging(log_name)
    main_logger.info("Starting program...")

    start = time.clock()
    main_logger.info(datetime.datetime.now())

    states = [
        # 'OH',
        # 'PA',
        'VA'
    ]

    for curr_state in states:
        # cobble_unit(curr_state, True, True)  # GPS Main
        cobble_unit(curr_state, True, False)  # GPS Service
        # cobble_unit(curr_state, False, True)  # GIS Main