Merge lp:~abentley/launchpad/celery-everywhere-7 into lp:launchpad

Proposed by Aaron Bentley
Status: Merged
Approved by: Aaron Bentley
Approved revision: no longer in the source branch.
Merged at revision: 15150
Proposed branch: lp:~abentley/launchpad/celery-everywhere-7
Merge into: lp:launchpad
Diff against target: 121 lines (+52/-3)
3 files modified
lib/lp/services/job/model/job.py (+2/-1)
lib/lp/translations/model/pofilestatsjob.py (+18/-1)
lib/lp/translations/tests/test_pofilestatsjob.py (+32/-1)
To merge this branch: bzr merge lp:~abentley/launchpad/celery-everywhere-7
Reviewer Review Type Date Requested Status
Abel Deuring (community) code Approve
Review via email: mp+103483@code.launchpad.net

Commit message

Support running POFileStatsJob via Celery.

Description of the change

= Summary =
Support running POFileStatsJob via Celery

== Pre-implementation notes ==
None

== LOC Rationale ==
Part of a resourced arc that will remove code.

== Implementation details ==
Update POFileStatsJob to provide config and makeDerived. Update pofilestatsjob.schedule to request Celery to run the job.

Update UniversalJobSource to return POFileStatsJob.

== Tests ==
bin/test --layer=CeleryJobLayer test_pofilestatsjob

== Demo and Q/A ==
None

= Launchpad lint =

Checking for conflicts and issues in changed files.

Linting changed files:
  lib/lp/translations/tests/test_pofilestatsjob.py
  lib/lp/translations/model/pofilestatsjob.py
  lib/lp/services/job/model/job.py

To post a comment you must log in.
Revision history for this message
Abel Deuring (adeuring) wrote :

looks good

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/services/job/model/job.py'
2--- lib/lp/services/job/model/job.py 2012-04-24 18:26:06 +0000
3+++ lib/lp/services/job/model/job.py 2012-04-25 14:31:24 +0000
4@@ -277,6 +277,7 @@
5 BranchMergeProposalJob,
6 )
7 from lp.soyuz.model.distributionjob import DistributionJob
8+ from lp.translations.model.pofilestatsjob import POFileStatsJob
9 from lp.translations.model.translationsharingjob import (
10 TranslationSharingJob,
11 )
12@@ -285,7 +286,7 @@
13
14 for baseclass in [
15 ApportJob, BranchJob, BranchMergeProposalJob, DistributionJob,
16- TranslationSharingJob,
17+ POFileStatsJob, TranslationSharingJob,
18 ]:
19 derived, base_class, store = cls._getDerived(job_id, baseclass)
20 if derived is not None:
21
22=== modified file 'lib/lp/translations/model/pofilestatsjob.py'
23--- lib/lp/translations/model/pofilestatsjob.py 2012-01-03 13:47:27 +0000
24+++ lib/lp/translations/model/pofilestatsjob.py 2012-04-25 14:31:24 +0000
25@@ -24,6 +24,7 @@
26 implements,
27 )
28
29+from lp.services.config import config
30 from lp.services.database.stormbase import StormBase
31 from lp.services.job.interfaces.job import IRunnableJob
32 from lp.services.job.model.job import Job
33@@ -43,6 +44,8 @@
34
35 __storm_table__ = 'POFileStatsJob'
36
37+ config = config.pofile_stats
38+
39 # Instances of this class are runnable jobs.
40 implements(IRunnableJob)
41
42@@ -100,7 +103,21 @@
43 And(POFileStatsJob.job == Job.id,
44 Job.id.is_in(Job.ready_jobs)))
45
46+ def makeDerived(self):
47+ """Support UniversalJobSource.
48+
49+ (Most Job ORM classes are generic, because their database table is
50+ used for several related job types. Therefore, they have derived
51+ classes to implement the specific Job.
52+
53+ POFileStatsJob implements the specific job, so its makeDerived returns
54+ itself.)
55+ """
56+ return self
57+
58
59 def schedule(pofile):
60 """Schedule a job to update a POFile's stats."""
61- return POFileStatsJob(pofile)
62+ job = POFileStatsJob(pofile)
63+ job.celeryRunOnCommit()
64+ return job
65
66=== modified file 'lib/lp/translations/tests/test_pofilestatsjob.py'
67--- lib/lp/translations/tests/test_pofilestatsjob.py 2012-01-04 16:17:43 +0000
68+++ lib/lp/translations/tests/test_pofilestatsjob.py 2012-04-25 14:31:24 +0000
69@@ -6,16 +6,25 @@
70 __metaclass__ = type
71
72
73+import transaction
74+
75 from lp.app.enums import ServiceUsage
76 from lp.services.config import config
77+from lp.services.features.testing import FeatureFixture
78 from lp.services.job.interfaces.job import (
79 IJobSource,
80 IRunnableJob,
81 )
82+from lp.services.job.tests import (
83+ block_on_job
84+ )
85 from lp.services.webapp.testing import verifyObject
86 from lp.testing import TestCaseWithFactory
87 from lp.testing.dbuser import dbuser
88-from lp.testing.layers import LaunchpadZopelessLayer
89+from lp.testing.layers import (
90+ CeleryJobLayer,
91+ LaunchpadZopelessLayer,
92+ )
93 from lp.translations.interfaces.pofilestatsjob import IPOFileStatsJobSource
94 from lp.translations.interfaces.side import TranslationSide
95 from lp.translations.model import pofilestatsjob
96@@ -190,3 +199,25 @@
97 pofile2 = template2.getPOFileByLang(language.code)
98
99 self.assertJobUpdatesStats(pofile1, pofile2)
100+
101+
102+class TestViaCelery(TestCaseWithFactory):
103+
104+ layer = CeleryJobLayer
105+
106+ def test_run(self):
107+ # POFileJob can run via Celery.
108+ self.useFixture(FeatureFixture(
109+ {'jobs.celery.enabled_classes': 'POFileStatsJob'}))
110+ # Running a job causes the POFile statistics to be updated.
111+ singular = self.factory.getUniqueString()
112+ pofile = self.factory.makePOFile(side=TranslationSide.UPSTREAM)
113+ # Create a message so we have something to have statistics about.
114+ self.factory.makePOTMsgSet(pofile.potemplate, singular)
115+ # The statistics start at 0.
116+ self.assertEqual(pofile.potemplate.messageCount(), 0)
117+ pofilestatsjob.schedule(pofile.id)
118+ with block_on_job():
119+ transaction.commit()
120+ # Now that the job ran, the statistics have been updated.
121+ self.assertEqual(pofile.potemplate.messageCount(), 1)