~nataliabidart/ubuntuone-client/fix-u1sync

« back to all changes in this revision

Viewing changes to ubuntuone/syncdaemon/action_queue.py

  • Committer: Tarmac
  • Author(s): facundo at com
  • Date: 2010-09-01 21:20:58 UTC
  • mfrom: (664.1.5 release-waiting)
  • Revision ID: tarmac-20100901212058-p4t5ord4m9csiczd
Cancelled uploads don't survive forever now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
354
354
            self.action_queue.event_queue.push('SYS_' + self.name + '_WAITING')
355
355
 
356
356
        # check conditions if the command is runnable
357
 
        if command.is_runnable():
 
357
        if command.is_runnable:
358
358
            self.check_conditions()
359
359
 
360
360
    def done(self):
374
374
            return True
375
375
        if self.commutative:
376
376
            for command in self.waiting:
377
 
                if command.is_runnable():
 
377
                if command.is_runnable:
378
378
                    return True
379
379
            return False
380
380
        else:
381
 
            return self.waiting[0].is_runnable()
 
381
            return self.waiting[0].is_runnable
382
382
 
383
383
    def _get_next_runnable_command(self):
384
384
        """Returns the next runnable command, if there is one."""
385
385
        if self.commutative:
386
386
            n_skipped = 0
387
387
            for command in self.waiting:
388
 
                if command.is_runnable():
 
388
                if command.is_runnable:
389
389
                    break
390
390
                n_skipped += 1
391
391
            if n_skipped < len(self.waiting):
397
397
            else:
398
398
                command = None
399
399
        else:
400
 
            if self.waiting and self.waiting[0].is_runnable():
 
400
            if self.waiting and self.waiting[0].is_runnable:
401
401
                command = self.waiting.popleft()
402
402
            else:
403
403
                command = None
439
439
        """Get the full queue (head + waiting)."""
440
440
        return [self._head] + list(self.waiting)
441
441
 
 
442
    def remove(self, command):
 
443
        """Removes a command from 'waiting', if there."""
 
444
        if command in self.waiting:
 
445
            self.waiting.remove(command)
 
446
 
442
447
 
443
448
class UniqueRequestQueue(RequestQueue):
444
449
    """A unique RequestQueue.
1165
1170
    )
1166
1171
 
1167
1172
    logged_attrs = ()
 
1173
    is_runnable = True
1168
1174
 
1169
1175
    __slots__ = ('_queue', 'start_done', 'running', 'log',
1170
1176
                 'markers_resolved_deferred')
1189
1195
        return mklog(logger, self.__class__.__name__,
1190
1196
                     share_id, node_id, **self.to_dict())
1191
1197
 
1192
 
    def is_runnable(self):
1193
 
        """Returns True if the command is eligible to run."""
1194
 
        return self._is_runnable()
1195
 
 
1196
 
    def _is_runnable(self):
1197
 
        """Hook for subclasses to supply a predicate for runnability."""
1198
 
        return True
1199
 
 
1200
1198
    def check_conditions(self):
1201
1199
        """Check conditions on which the command may be waiting."""
1202
1200
 
1377
1375
        else:
1378
1376
            d = defer.Deferred()
1379
1377
            self._deferred = d
1380
 
            event_name = 'SYS_' + self._queue.name + '_BLOCKED'
1381
 
            self.action_queue.event_queue.push(event_name)
1382
1378
        return d
1383
1379
 
1384
1380
    def check_conditions(self):
2146
2142
        self.cancelled = True
2147
2143
        if self.download_req is not None:
2148
2144
            self.download_req.cancel()
 
2145
        self._queue.remove(self)
2149
2146
        self.cleanup()
2150
2147
 
2151
2148
    def store_marker_result(self, (share_id, node_id)):
2316
2313
            self.action_queue.cancel_upload(self.share_id, self.node_id)
2317
2314
        self.n_bytes_written_last = None # set by _run
2318
2315
 
2319
 
    def _is_runnable(self):
2320
 
        """Returns True if there is sufficient space available to complete
2321
 
        the upload."""
2322
 
        return self.action_queue.have_sufficient_space_for_upload(self.share_id,
2323
 
                                                                  self.size)
 
2316
    @property
 
2317
    def is_runnable(self):
 
2318
        """Tell if the upload is ok to be carried on.
 
2319
 
 
2320
        Return True if there is sufficient space available to complete
 
2321
        the upload, or if the upload is cancelled so it can pursue
 
2322
        its fate.
 
2323
        """
 
2324
        if self.cancelled:
 
2325
            return True
 
2326
        else:
 
2327
            return self.action_queue.have_sufficient_space_for_upload(
 
2328
                                                    self.share_id, self.size)
2324
2329
 
2325
2330
    def cancel(self):
2326
2331
        """Cancel the upload."""
2327
2332
        self.cancelled = True
2328
2333
        if self.upload_req is not None:
2329
2334
            self.upload_req.cancel()
 
2335
        self._queue.remove(self)
2330
2336
        self.cleanup()
2331
2337
 
2332
2338
    def cleanup(self):