]>
git.ipfire.org Git - people/jschlag/pbs.git/blob - src/buildservice/scheduler.py
15 def __init__(self
, *args
, **kwargs
):
19 self
._next
_start
_time
= 0
24 if hasattr(self
, "_next_start_time"):
25 return "<%s next_start_in=%ds>" % \
26 (self
.__class
__.__name
__, self
._next
_start
_time
- time
.time())
28 return "<%s>" % self
.__class
__.__name
__
30 def run(self
, *args
, **kwargs
):
33 def run_subprocess_background(self
, method
, *args
):
34 arguments
= [method
,] + list(args
)
36 process
= multiprocessing
.Process(target
=self
.fork
, args
=arguments
)
37 process
.daemon
= False
44 def run_subprocess(self
, *args
):
45 process
= self
.run_subprocess_background(*args
)
47 # Wait until process has finished.
51 def fork(method
, *args
, **kwargs
):
52 # Create new pakfire instance.
55 return method(backend
, *args
, **kwargs
)
58 class Scheduler(object):
62 def add_event(self
, event
, start_time
=None):
63 event
.scheduler
= self
65 self
._queue
.append(event
)
67 # Set initial start time.
68 if start_time
is None:
69 start_time
= time
.time()
71 event
._next
_start
_time
= start_time
74 self
._queue
.sort(key
=lambda e
: (e
.priority
, e
._next
_start
_time
))
81 for event
in self
._queue
:
82 # If the event has to be started some time in
84 if event
._next
_start
_time
<= time
.time():
86 logging
.info("Running %s..." % event
)
88 event
.run(*event
.args
, **event
.kwargs
)
90 # In case the user interrupts the scheduler.
91 except KeyboardInterrupt:
99 # Set the next execution time if the event
100 # should be run again.
102 event
._next
_start
_time
= time
.time() + event
.interval
104 # Otherwise remove it from the queue.
106 self
._queue
.remove(event
)
108 # Get back to outer loop and sort the queue again.