Changes in / [56fd535:9225351]


Ignore:
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • .gitignore

    r01e3017 rd17381b  
    33debian/
    44*.pyc
     5*.log
     6*.log.*
     7tags
  • server/lib/gutenbach/server/__init__.py

    ra9eb577e rcf0d7e8  
    33__all__ = ['errors']
    44__all__.extend(errors.__all__)
     5
     6def sync(func):
     7    """Lock decorator
     8
     9    Holds a lock (self.lock) for the durration of a method call.
     10    """
     11
     12    def do(self, *args, **kwargs):
     13        with self.lock:
     14            return func(self, *args, **kwargs)
     15
     16    return do
     17__all__.append('sync')
     18
    519
    620from job import GutenbachJob
  • server/lib/gutenbach/server/job.py

    rc1cebbc r9225351  
    322322        self.player.mplayer_pause()
    323323
     324    def resume(self):
     325        """Non-blocking resume.  Job must be paused (see
     326        'GutenbachJob.is_paused').
     327
     328        Raises
     329        ------
     330        InvalidJobStateException
     331            If the job is not paused.
     332
     333        """
     334        if not self.is_paused:
     335            raise errors.InvalidJobStateException(self.state)
     336        self.player.mplayer_pause()
     337
    324338    def cancel(self):
    325339        """Blocking cancel. The job must not have been previously
  • server/lib/gutenbach/server/player.py

    ra9eb577e rcf0d7e8  
    33import subprocess
    44import time
     5from . import sync
    56
    67# initialize logger
     
    2425
    2526    @property
     27    @sync
    2628    def is_playing(self):
    27         with self.lock:
    28             if self._dryrun:
    29                 playing = self.isAlive() and not self.is_done
    30             else:
    31                 playing = self.isAlive() and \
    32                           not self.is_done and \
    33                           self.player is not None and \
    34                           self.player.poll() is None
    35         return playing
     29        if self._dryrun:
     30            return self.isAlive() and not self.is_done
     31        else:
     32            return self.isAlive() and \
     33                      not self.is_done and \
     34                      self.player is not None and \
     35                      self.player.poll() is None
    3636
    3737    @property
     38    @sync
    3839    def is_paused(self):
    39         with self.lock:
    40             paused = self.is_playing and self._paused
    41         return paused
     40        return self.is_playing and self._paused
    4241
    4342    @property
     
    4948        return self._callback
    5049    @callback.setter
     50    @sync
    5151    def callback(self, val):
    52         with self.lock:
    53             self._callback = val
     52        self._callback = val
    5453
    5554    def start(self):
     
    112111
    113112    def mplayer_pause(self):
     113        # Note: Inner lock due to sleep.
    114114        with self.lock:
    115115            if self.is_playing:
     
    123123               
    124124    def mplayer_stop(self):
     125        # Note: Inner Lock due to join.
    125126        with self.lock:
    126127            if self.is_playing:
  • server/lib/gutenbach/server/printer.py

    rc1dc25f r9225351  
    99import traceback
    1010import sys
     11from . import sync
    1112
    1213
     
    100101            with self.lock:
    101102                try:
    102                     if self.current_job is None:
     103                    if not self.paused and self.current_job is None:
    103104                        self.start_job()
    104105                    elif self.current_job.is_done:
     
    124125
    125126    @property
     127    @sync
    126128    def state(self):
    127         with self.lock:
    128             if self.current_job is not None:
    129                 val = States.PROCESSING
    130             elif len(self.pending_jobs) == 0:
    131                 val = States.IDLE
    132             else:
    133                 val = States.STOPPED
    134         return val
    135 
    136     @property
     129        if self.current_job is not None:
     130            return States.PROCESSING
     131        elif len(self.pending_jobs) == 0:
     132            return States.IDLE
     133        else:
     134            return States.STOPPED
     135
     136    @property
     137    @sync
    137138    def active_jobs(self):
    138         with self.lock:
    139             jobs = self.pending_jobs[:]
    140             if self.current_job is not None:
    141                 jobs.insert(0, self.current_job.id)
     139        jobs = self.pending_jobs[:]
     140        if self.current_job is not None:
     141            jobs.insert(0, self.current_job.id)
    142142        return jobs
    143143
     
    146146    ######################################################################
    147147
     148    @sync
    148149    def start_job(self):
    149         with self.lock:
    150             if self.current_job is None:
    151                 try:
    152                     job_id = heapq.heappop(self.pending_jobs)
    153                     self.current_job = self.get_job(job_id)
    154                     self.current_job.play()
    155                 except IndexError:
    156                     self.current_job = None
    157                 except InvalidJobStateException:
    158                     heapq.heappush(self.pending_jobs, self.current_job.id)
    159                     self.current_job = None
     150        if self.current_job is None:
     151            try:
     152                job_id = heapq.heappop(self.pending_jobs)
     153                self.current_job = self.get_job(job_id)
     154                self.current_job.play()
     155            except IndexError:
     156                self.current_job = None
     157            except InvalidJobStateException:
     158                heapq.heappush(self.pending_jobs, self.current_job.id)
     159                self.current_job = None
    160160                   
     161    @sync
    161162    def complete_job(self):
    162         with self.lock:
    163             if self.current_job is None:
    164                 return
    165 
    166             try:
    167                 if not self.current_job.is_done:
    168                     self.current_job.stop()
    169             finally:
    170                 self.finished_jobs.append(self.current_job.id)
    171                 self.current_job = None
    172 
     163        if self.current_job is None:
     164            return
     165
     166        try:
     167            if not self.current_job.is_done:
     168                self.current_job.stop()
     169        finally:
     170            self.finished_jobs.append(self.current_job.id)
     171            self.current_job = None
     172
     173    @sync
    173174    def get_job(self, job_id):
    174         with self.lock:
    175             if job_id not in self.jobs:
    176                 raise InvalidJobException(job_id)
    177             job = self.jobs[job_id]
    178         return job
     175        if job_id not in self.jobs:
     176            raise InvalidJobException(job_id)
     177        return self.jobs[job_id]
    179178
    180179    ######################################################################
     
    412411        return job_id
    413412
     413    @sync
    414414    def pause_printer(self):
    415         pass
    416 
     415        """Pause the printer.
     416
     417        Does nothing if the printer is already paused.
     418        """
     419        if self.paused:
     420            return
     421
     422        if self.current_job is not None and self.current_job.is_playing:
     423            self.current_job.pause()
     424
     425        self.paused = True
     426
     427
     428
     429    @sync
    417430    def resume_printer(self):
    418         pass
     431        """Resume the printer.
     432
     433        Does nothing if the printer is not paused.
     434        """
     435        if not self.paused:
     436            return
     437
     438        if self.current_job is not None:
     439            self.current_job.resume()
     440
     441        self.paused = False
    419442
    420443    def get_printer_attributes(self, requested_attributes=None):
Note: See TracChangeset for help on using the changeset viewer.