-
Notifications
You must be signed in to change notification settings - Fork 572
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
implement job persistance #359
Closed
Closed
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 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
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,64 @@ | ||
import os | ||
from datetime import datetime | ||
from zope.interface import implementer | ||
|
||
from .interfaces import IJobStorage | ||
from .sqlite import SqliteFinishedJobs | ||
|
||
|
||
class Job(object): | ||
def __init__(self, project, spider, job=None, start_time=None, end_time=None): | ||
self.project = project | ||
self.spider = spider | ||
self.job = job | ||
self.start_time = start_time if start_time else datetime.now() | ||
self.end_time = end_time if end_time else datetime.now() | ||
|
||
|
||
@implementer(IJobStorage) | ||
class MemoryJobStorage(object): | ||
|
||
def __init__(self, config): | ||
self.jobs = [] | ||
self.finished_to_keep = config.getint('finished_to_keep', 100) | ||
|
||
def add(self, job): | ||
self.jobs.append(job) | ||
del self.jobs[:-self.finished_to_keep] # keep last x finished jobs | ||
|
||
def list(self): | ||
return self.jobs | ||
|
||
def __len__(self): | ||
return len(self.jobs) | ||
|
||
def __iter__(self): | ||
for j in self.jobs: | ||
yield j | ||
|
||
|
||
@implementer(IJobStorage) | ||
class SqliteJobStorage(object): | ||
|
||
def __init__(self, config): | ||
dbsdir = config.get('dbs_dir', 'dbs') | ||
if not os.path.exists(dbsdir): | ||
os.makedirs(dbsdir) | ||
dbpath = os.path.join(dbsdir, 'jobs.db') | ||
self.jstorage = SqliteFinishedJobs(dbpath, "finished_jobs") | ||
self.finished_to_keep = config.getint('finished_to_keep', 100) | ||
|
||
def add(self, job): | ||
self.jstorage.add(job) | ||
self.jstorage.clear(self.finished_to_keep) | ||
|
||
def list(self): | ||
return [j for j in self.__iter__()] | ||
|
||
def __len__(self): | ||
return len(self.jstorage) | ||
|
||
def __iter__(self): | ||
for j in self.jstorage: | ||
yield Job(project=j[0], spider=j[1], job=j[2], | ||
start_time=j[3], end_time=j[4]) |
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,61 @@ | ||
from twisted.trial import unittest | ||
|
||
from zope.interface.verify import verifyObject | ||
|
||
from scrapyd.interfaces import IJobStorage | ||
from scrapyd.config import Config | ||
from scrapyd.jobstorage import Job, MemoryJobStorage, SqliteJobStorage | ||
|
||
j1, j2, j3 = Job('p1', 's1'), Job('p2', 's2'), Job('p3', 's3') | ||
|
||
|
||
class MemoryJobStorageTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
d = self.mktemp() | ||
config = Config(values={'dbs_dir': d, 'finished_to_keep': '2'}) | ||
self.jobst = MemoryJobStorage(config) | ||
self.j1, self.j2, self.j3 = j1, j2, j3 | ||
self.jobst.add(self.j1) | ||
self.jobst.add(self.j2) | ||
self.jobst.add(self.j3) | ||
|
||
def test_interface(self): | ||
verifyObject(IJobStorage, self.jobst) | ||
|
||
def test_add(self): | ||
self.assertEqual(len(self.jobst.list()), 2) | ||
|
||
def test_iter(self): | ||
l = [j for j in self.jobst] | ||
self.assertEqual(l[0], j2) | ||
self.assertEqual(l[1], j3) | ||
self.assertEqual(len(l), 2) | ||
|
||
def test_len(self): | ||
self.assertEqual(len(self.jobst), 2) | ||
|
||
|
||
class SqliteJobsStorageTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
d = self.mktemp() | ||
config = Config(values={'dbs_dir': d, 'finished_to_keep': '2'}) | ||
self.jobst = SqliteJobStorage(config) | ||
self.j1, self.j2, self.j3 = j1, j2, j3 | ||
|
||
def test_interface(self): | ||
verifyObject(IJobStorage, self.jobst) | ||
|
||
def test_add(self): | ||
self.jobst.add(self.j1) | ||
self.jobst.add(self.j2) | ||
self.jobst.add(self.j3) | ||
self.assertEqual(len(self.jobst.list()), 2) | ||
|
||
def test_iter(self): | ||
self.jobst.add(self.j1) | ||
self.jobst.add(self.j2) | ||
self.jobst.add(self.j3) | ||
l = [j for j in self.jobst] | ||
self.assertEqual(len(l), 2) |
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
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.
default is actually:
scrapyd.jobstorage.MemoryJobStorage
let's put full names here:
scrapyd.jobstorage.MemoryJobStorage
and scrapyd.jobstorage.SqliteJobStorage