<s> from __future__ import print_function , unicode_literals <EOL> from voodoo . typechecker import typecheck , ITERATION <EOL> from voodoo . log import logged <EOL> import voodoo . log as log <EOL> from weblab . data . experiments import ExperimentId <EOL> import weblab . core . coordinator . sql . db as CoordinationDatabaseManager <EOL> import weblab . core . coordinator . sql . resource_manager as ResourcesManager <EOL> import weblab . core . coordinator . sql . reservations_manager as ReservationsManager <EOL> import weblab . core . coordinator . sql . post_reservation as PostReservationDataManager <EOL> from weblab . core . coordinator . sql . meta_scheduler import IndependentSchedulerAggregator <EOL> from weblab . core . coordinator . sql . no_scheduler import NoScheduler <EOL> from weblab . core . coordinator . sql . priority_queue_scheduler import PriorityQueueScheduler <EOL> from weblab . core . coordinator . sql . externals . weblabdeusto_scheduler import ExternalWebLabDeustoScheduler <EOL> from weblab . core . coordinator . sql . externals . ilab_batch_scheduler import ILabBatchScheduler <EOL> from weblab . core . coordinator . resource import Resource <EOL> from weblab . core . coordinator . coordinator import AbstractCoordinator <EOL> from weblab . core . coordinator . coordinator import NO_SCHEDULER , PRIORITY_QUEUE , EXTERNAL_WEBLAB_DEUSTO , ILAB_BATCH_QUEUE <EOL> class Coordinator ( AbstractCoordinator ) : <EOL> SCHEDULING_SYSTEMS = { <EOL> NO_SCHEDULER : NoScheduler , <EOL> PRIORITY_QUEUE : PriorityQueueScheduler , <EOL> EXTERNAL_WEBLAB_DEUSTO : ExternalWebLabDeustoScheduler , <EOL> ILAB_BATCH_QUEUE : ILabBatchScheduler , <EOL> } <EOL> AGGREGATOR = IndependentSchedulerAggregator <EOL> def __init__ ( self , locator , cfg_manager , ConfirmerClass = None ) : <EOL> coordination_database_manager = CoordinationDatabaseManager . CoordinationDatabaseManager ( cfg_manager ) <EOL> self . _session_maker = coordination_database_manager . session_maker <EOL> super ( Coordinator , self ) . __init__ ( self . _session_maker , locator , cfg_manager , ConfirmerClass ) <EOL> def _initial_clean ( self , coordination_configuration_parser ) : <EOL> session = self . _session_maker ( ) <EOL> try : <EOL> external_servers_config = coordination_configuration_parser . parse_external_servers ( ) <EOL> for external_server_str in external_servers_config : <EOL> for resource_type_name in external_servers_config [ external_server_str ] : <EOL> self . resources_manager . add_experiment_id ( session , ExperimentId . parse ( external_server_str ) , resource_type_name ) <EOL> session . commit ( ) <EOL> finally : <EOL> session . close ( ) <EOL> def _initialize_managers ( self ) : <EOL> self . reservations_manager = ReservationsManager . ReservationsManager ( self . _session_maker ) <EOL> self . resources_manager = ResourcesManager . ResourcesManager ( self . _session_maker ) <EOL> self . post_reservation_data_manager = PostReservationDataManager . PostReservationDataManager ( self . _session_maker , self . time_provider ) <EOL> @ typecheck ( Resource , ITERATION ( basestring ) ) <EOL> @ logged ( ) <EOL> def mark_resource_as_broken ( self , resource_instance , messages = [ ] ) : <EOL> scheduler = self . _get_scheduler_per_resource ( resource_instance ) <EOL> anything_changed = False <EOL> session = self . _session_maker ( ) <EOL> try : <EOL> changed = scheduler . removing_current_resource_slot ( session , resource_instance ) <EOL> anything_changed = anything_changed or changed <EOL> changed = self . resources_manager . mark_resource_as_broken ( session , resource_instance ) <EOL> anything_changed = anything_changed or changed <EOL> if anything_changed : <EOL> session . commit ( ) <EOL> finally : <EOL> session . close ( ) <EOL> if anything_changed : <EOL> log . log ( Coordinator , log . level . Warning , <EOL> "<STR_LIT>" % ( resource_instance , messages ) ) <EOL> if self . notifications_enabled : <EOL> return self . _notify_experiment_status ( '<STR_LIT>' , resource_instance , messages ) <EOL> return { } <EOL> def _release_resource_instance ( self , experiment_instance_id ) : <EOL> session = self . _session_maker ( ) <EOL> try : <EOL> resource_instance = self . resources_manager . get_resource_instance_by_experiment_instance_id ( experiment_instance_id ) <EOL> self . resources_manager . release_resource_instance ( session , resource_instance ) <EOL> session . commit ( ) <EOL> finally : <EOL> session . close ( ) <EOL> def _delete_reservation ( self , reservation_id ) : <EOL> session = self . _session_maker ( ) <EOL> try : <EOL> self . reservations_manager . delete ( session , reservation_id ) <EOL> session . commit ( ) <EOL> finally : <EOL> session . close ( ) <EOL> def _clean ( self ) : <EOL> for scheduler in self . schedulers . values ( ) : <EOL> scheduler . _clean ( ) <EOL> self . reservations_manager . _clean ( ) <EOL> self . resources_manager . _clean ( ) <EOL> self . post_reservation_data_manager . _clean ( ) </s>
