-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#19] restructured the scheduler code and made it pluggable
- Loading branch information
Showing
8 changed files
with
144 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import sys | ||
import importlib | ||
import appliance | ||
|
||
from abc import ABCMeta | ||
|
||
from commons import AutonomousMonitor, Singleton, Loggable | ||
from container.manager import ContainerManager | ||
from config import config | ||
|
||
|
||
def get_scheduler(): | ||
try: | ||
sched_mod = '.'.join(config.pivot.scheduler.split('.')[:-1]) | ||
sched_class = config.pivot.scheduler.split('.')[-1] | ||
return getattr(importlib.import_module(sched_mod), sched_class) | ||
except Exception as e: | ||
sys.stderr.write(str(e) + '\n') | ||
from schedule.default import DefaultApplianceScheduler | ||
return DefaultApplianceScheduler | ||
|
||
|
||
class ApplianceScheduleNegotiator(AutonomousMonitor): | ||
|
||
def __init__(self, app_id, interval=3000): | ||
super(ApplianceScheduleNegotiator, self).__init__(interval) | ||
self.__app_id = app_id | ||
self.__executor = ApplianceScheduleExecutor() | ||
self.__scheduler = get_scheduler()() | ||
self.__app_mgr = appliance.manager.ApplianceManager() | ||
|
||
async def callback(self): | ||
# read appliance from DB | ||
status, app, err = await self.__app_mgr.get_appliance(self.__app_id) | ||
if not app: | ||
if status == 404: | ||
self.logger.info('Appliance %s no longer exists'%self.__app_id) | ||
else: | ||
self.logger.error(err) | ||
self.stop() | ||
return | ||
# contact the scheduler for new schedule | ||
sched = self.__scheduler.schedule(app) | ||
self.logger.info('Containers to be scheduled: %s'%[c.id for c in sched.containers]) | ||
# if the scheduling is done | ||
if sched.done: | ||
self.logger.info('Scheduling is done for appliance %s'%self.__app_id) | ||
self.stop() | ||
return | ||
# execute the new schedule | ||
await self.__executor.execute(sched) | ||
|
||
|
||
class ApplianceScheduleExecutor(Loggable, metaclass=Singleton): | ||
|
||
def __init__(self): | ||
self.__contr_mgr = ContainerManager() | ||
|
||
async def execute(self, sched): | ||
for c in sched.containers: | ||
_, msg, err = await self.__contr_mgr.provision_container(c) | ||
if err: | ||
self.logger.error(err) | ||
self.logger.info('Container %s is being provisioned'%c.id) | ||
|
||
|
||
class Schedule: | ||
|
||
def __init__(self, done=False, containers=[]): | ||
self.__done = done | ||
self.__containers = list(containers) | ||
|
||
@property | ||
def done(self): | ||
return self.__done | ||
|
||
@property | ||
def containers(self): | ||
return list(self.__containers) | ||
|
||
@done.setter | ||
def done(self, done): | ||
self.__done = done | ||
|
||
def add_containers(self, contrs): | ||
self.__containers += list(contrs) | ||
|
||
|
||
class ApplianceScheduler(Loggable, metaclass=ABCMeta): | ||
|
||
def schedule(self, app): | ||
raise NotImplemented | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from container.base import ContainerType, ContainerState | ||
from schedule import ApplianceScheduler, Schedule | ||
|
||
|
||
class DefaultApplianceScheduler(ApplianceScheduler): | ||
|
||
def schedule(self, app): | ||
sched = Schedule() | ||
free_contrs = self._resolve_dependencies(app) | ||
self.logger.info('Free containers: %s'%[c.id for c in free_contrs]) | ||
if not free_contrs: | ||
sched.done = True | ||
return sched | ||
sched.add_containers([c for c in free_contrs if c.state in | ||
(ContainerState.SUBMITTED, ContainerState.FAILED)]) | ||
return sched | ||
|
||
def _resolve_dependencies(self, app): | ||
contrs = {c.id: c for c in app.containers | ||
if (c.type == ContainerType.JOB and c.state != ContainerState.SUCCESS) | ||
or (c.type == ContainerType.SERVICE and c.state != ContainerState.RUNNING)} | ||
parents = {} | ||
for c in contrs.values(): | ||
parents.setdefault(c.id, set()).update([d for d in c.dependencies if d in contrs]) | ||
return [contrs[k] for k, v in parents.items() if not v] | ||
|
||
|