# Path: netl2api/lib/utils.py
# def gen_context_uid():
#     _thr_local.l2api_ctx_uid = str(uuid4())
#     return _thr_local.l2api_ctx_uid
# 
# Path: netl2api/lib/utils.py
# def get_switch_instance(device):
#     switches = config.get_devices_cfg()
#     if not switches.has_key(device):
#         raise DeviceNotFound("Switch not known/configured => '%s'" % device)
#     if not switches[device].get("mgmt-api") or not switches[device].get("mgmt-user") \
#             or not switches[device].get("mgmt-pass"):
#         raise MisconfiguredDevice("The device '%s' are misconfigured - see the devices.cfg or redis-devices.cfg" % device)
#     swapi = get_sw_handler_class(switches[device]["mgmt-api"])
#     return swapi(host=switches[device]["mgmt-host"], port=int(switches[device]["mgmt-port"]),
#                  username=switches[device]["mgmt-user"], passwd=switches[device]["mgmt-pass"])
# 
# Path: netl2api/lib/config.py
# def get_netl2server_cfg():
#     return get_cfg("netl2server", check_permission=None)
# 
# def setup_persistence_ctrl_logger(cfg):
#     return setup_logger(cfg, "netl2server.job.switch_cfg_persistence")


import signal
from apscheduler.scheduler import Scheduler
from netl2api.lib.utils import gen_context_uid
from netl2api.lib.utils import get_switch_instance
from netl2api.server.workers.switch_cfg_persistence_utils import *
from netl2api.lib.config import get_netl2server_cfg, setup_persistence_ctrl_logger
        from setproctitle import setproctitle
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#
# @author: Eduardo S. Scarpellini
# @author: Luiz Ozaki


__copyright__ = "Copyright 2012, Locaweb IDC"




cfg            = get_netl2server_cfg()
logger         = setup_persistence_ctrl_logger(cfg)
worker_minutes = cfg.get("job.switch_cfg_persistence", "run_on_minutes")


def worker():
    logger.info("Waking up persistence-control worker...")
    devices = list_pending_persistence_jobs()
    if devices is None:
        logger.info("No pending persistence jobs found")
        return
    for device in devices:
        sw_cfg_persist_lock = acquire_persistence_lock(device)

# Path: netl2api/lib/utils.py
# def gen_context_uid():
#     _thr_local.l2api_ctx_uid = str(uuid4())
#     return _thr_local.l2api_ctx_uid
# 
# Path: netl2api/lib/utils.py
# def get_switch_instance(device):
#     switches = config.get_devices_cfg()
#     if not switches.has_key(device):
#         raise DeviceNotFound("Switch not known/configured => '%s'" % device)
#     if not switches[device].get("mgmt-api") or not switches[device].get("mgmt-user") \
#             or not switches[device].get("mgmt-pass"):
#         raise MisconfiguredDevice("The device '%s' are misconfigured - see the devices.cfg or redis-devices.cfg" % device)
#     swapi = get_sw_handler_class(switches[device]["mgmt-api"])
#     return swapi(host=switches[device]["mgmt-host"], port=int(switches[device]["mgmt-port"]),
#                  username=switches[device]["mgmt-user"], passwd=switches[device]["mgmt-pass"])
# 
# Path: netl2api/lib/config.py
# def get_netl2server_cfg():
#     return get_cfg("netl2server", check_permission=None)
# 
# def setup_persistence_ctrl_logger(cfg):
#     return setup_logger(cfg, "netl2server.job.switch_cfg_persistence")


# Path: netl2api/server/workers/switch_cfg_persistence.py
import signal
from apscheduler.scheduler import Scheduler
from netl2api.lib.utils import gen_context_uid
from netl2api.lib.utils import get_switch_instance
from netl2api.server.workers.switch_cfg_persistence_utils import *
from netl2api.lib.config import get_netl2server_cfg, setup_persistence_ctrl_logger
        from setproctitle import setproctitle

#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#
# @author: Eduardo S. Scarpellini
# @author: Luiz Ozaki


__copyright__ = "Copyright 2012, Locaweb IDC"




cfg            = get_netl2server_cfg()
logger         = setup_persistence_ctrl_logger(cfg)
worker_minutes = cfg.get("job.switch_cfg_persistence", "run_on_minutes")


def worker():
    logger.info("Waking up persistence-control worker...")
    devices = list_pending_persistence_jobs()
    if devices is None:
        logger.info("No pending persistence jobs found")
        return
    for device in devices:
        sw_cfg_persist_lock = acquire_persistence_lock(device)

        if sw_cfg_persist_lock is None: