def test_do_work(self): response = self.client.post(reverse('jobs.start'), { 'job_name': 'counter' }) json_response = simplejson.loads(response.content) self.assert_json_success(json_response) job_key = json_response['job_key'] # work the counter three times: response = self.client.post(reverse('jobs.work'), { 'job_key': job_key }) json_response = simplejson.loads(response.content) self.assert_json_success(json_response) job = Job.all().filter('job_name =', 'counter')[0] self.assertEqual(simplejson.loads(job.result), {'count':1}) self.assertEqual(json_response['finished'], False) response = self.client.post(reverse('jobs.work'), { 'job_key': job_key }) json_response = simplejson.loads(response.content) self.assert_json_success(json_response) job = Job.all().filter('job_name =', 'counter')[0] self.assertEqual(simplejson.loads(job.result), {'count':2}) self.assertEqual(json_response['finished'], False) response = self.client.post(reverse('jobs.work'), { 'job_key': job_key }) json_response = simplejson.loads(response.content) self.assert_json_success(json_response) job = Job.all().filter('job_name =', 'counter')[0] self.assertEqual(simplejson.loads(job.result), {'count':3}) self.assertEqual(json_response['finished'], True)
def test_do_work(self): response = self.client.post(reverse('jobs.start'), {'job_name': 'counter'}) json_response = simplejson.loads(response.content) self.assert_json_success(json_response) job_key = json_response['job_key'] # work the counter three times: response = self.client.post(reverse('jobs.work'), {'job_key': job_key}) json_response = simplejson.loads(response.content) self.assert_json_success(json_response) job = Job.all().filter('job_name =', 'counter')[0] self.assertEqual(simplejson.loads(job.result), {'count': 1}) self.assertEqual(json_response['finished'], False) response = self.client.post(reverse('jobs.work'), {'job_key': job_key}) json_response = simplejson.loads(response.content) self.assert_json_success(json_response) job = Job.all().filter('job_name =', 'counter')[0] self.assertEqual(simplejson.loads(job.result), {'count': 2}) self.assertEqual(json_response['finished'], False) response = self.client.post(reverse('jobs.work'), {'job_key': job_key}) json_response = simplejson.loads(response.content) self.assert_json_success(json_response) job = Job.all().filter('job_name =', 'counter')[0] self.assertEqual(simplejson.loads(job.result), {'count': 3}) self.assertEqual(json_response['finished'], True)
def test_start(self): response = self.client.post(reverse('jobs.start'), {'job_name': 'counter'}) json_response = simplejson.loads(response.content) self.assert_json_success(json_response) job = Job.all().filter('job_name =', 'counter')[0] self.assertEqual(json_response['job_key'], str(job.key())) self.assertEqual(job.result, None)
def test_start(self): response = self.client.post(reverse('jobs.start'), { 'job_name': 'counter' }) json_response = simplejson.loads(response.content) self.assert_json_success(json_response) job = Job.all().filter('job_name =', 'counter')[0] self.assertEqual(json_response['job_key'], str(job.key())) self.assertEqual(job.result, None)
def rss(request): jobs = Job.all().filter('status =', 'published').order('-published_at').fetch(20) jobs_published_at = [job.published_at for job in jobs] if jobs_published_at: pub_date = max(jobs_published_at) else: pub_date = None return render_to_response('rss.xml', {'jobs': jobs, 'host': request.get_host(), 'pub_date': pub_date}, mimetype='application/rss+xml')
def check_queued(request): job = Job.all().filter('status =', 'queued').order('queued_at').fetch(1) if job: job = job[0] # fetch() returns list of items result = urlfetch.fetch(job.owner_profile_url) if result.status_code == 200: try: # if owner's profile contents code, this will pass silently result.content.index(job.code) job.publish() job.put() return HttpResponse('Published') except ValueError: pass job.fail() job.put() return HttpResponse('Failed') else: return HttpResponse('No queued jobs.')
def my(request): jobs = Job.all().filter('owner =', users.get_current_user()).order('-created_at') return _custom_render_to_response('my_jobs.html', {'jobs': jobs, 'title': 'My jobs'})
def reap_dead_jobs(): q = Job.all().filter("started <", datetime.datetime.now() - timedelta(days=2)) for job in q: job.delete()
def teardown_data(): for ob in Job.all(): ob.delete() jobs._reset_registry()