-
Notifications
You must be signed in to change notification settings - Fork 162
Refactor and merge datastore head task pollers. #320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,101 @@ | ||
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:gcloud/db.dart'; | ||
import 'package:logging/logging.dart'; | ||
|
||
import '../frontend/models.dart'; | ||
|
||
import 'task_scheduler.dart'; | ||
|
||
final Logger _logger = new Logger('pub.shared.task_sources'); | ||
|
||
const Duration _defaultWindow = const Duration(minutes: 5); | ||
const Duration _defaultSleep = const Duration(minutes: 1); | ||
|
||
/// Creates tasks by polling the datastore for new versions. | ||
class DatastoreVersionsHeadTaskSource implements TaskSource { | ||
final DatastoreDB _db; | ||
final Duration _window; | ||
final Duration _sleep; | ||
final bool _onlyLatest; | ||
DateTime _lastTs; | ||
|
||
DatastoreVersionsHeadTaskSource( | ||
this._db, { | ||
|
||
/// Whether to return only the latest versions of the packages. | ||
bool onlyLatest: false, | ||
|
||
/// Whether to scan the entire datastore in the first run or skip old ones. | ||
bool skipHistory: false, | ||
|
||
/// Tolerance window for eventually consistency in Datastore. | ||
Duration window, | ||
|
||
/// Inactivity duration between two polls. | ||
Duration sleep, | ||
}) | ||
: _window = window ?? _defaultWindow, | ||
_sleep = sleep ?? _defaultSleep, | ||
_onlyLatest = onlyLatest, | ||
_lastTs = | ||
skipHistory ? new DateTime.now().toUtc().subtract(window) : null; | ||
|
||
@override | ||
Stream<Task> startStreaming() async* { | ||
for (;;) { | ||
try { | ||
final DateTime now = new DateTime.now().toUtc(); | ||
if (_onlyLatest) { | ||
yield* _pollPackages(); | ||
} else { | ||
yield* _pollPackageVersions(); | ||
} | ||
_lastTs = now.subtract(_window); | ||
} catch (e, st) { | ||
_logger.severe('Error polling head.', e, st); | ||
} | ||
await new Future.delayed(_sleep); | ||
} | ||
} | ||
|
||
Future<bool> shouldYieldTask(Task task) async => true; | ||
|
||
Future dbScanComplete(int count) async {} | ||
|
||
Stream<Task> _pollPackages() async* { | ||
final Query q = _db.query(Package); | ||
if (_lastTs != null) { | ||
q.filter('updated >=', _lastTs); | ||
} | ||
int count = 0; | ||
await for (Package p in q.run()) { | ||
final task = new Task(p.name, p.latestVersion ?? p.latestDevVersion); | ||
if (await shouldYieldTask(task)) { | ||
count++; | ||
yield task; | ||
} | ||
} | ||
await dbScanComplete(count); | ||
} | ||
|
||
Stream<Task> _pollPackageVersions() async* { | ||
final Query q = _db.query(PackageVersion); | ||
if (_lastTs != null) { | ||
q.filter('created >=', _lastTs); | ||
} | ||
int count = 0; | ||
await for (PackageVersion pv in q.run()) { | ||
final task = new Task(pv.package, pv.version); | ||
if (await shouldYieldTask(task)) { | ||
count++; | ||
yield task; | ||
} | ||
} | ||
await dbScanComplete(count); | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally I would prefer separating these concerns via composition rather than inheritance, e.g.
new FilteredTaskSource(new DatastoreHeadTaskSource())
.