3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """A handler that allows the user to see and test cron tasks."""
23 from google
.appengine
.cron
import groctimespecification
24 from google
.appengine
.api
import croninfo
25 from google
.appengine
.api
import yaml_errors
26 from google
.appengine
.tools
.devappserver2
.admin
import admin_request_handler
36 class CronHandler(admin_request_handler
.AdminRequestHandler
):
40 values
['has_pytz'] = bool(pytz
)
42 values
['cronjobs'] = self
._get
_cron
_jobs
()
43 except (StandardError, yaml_errors
.Error
):
44 values
['cron_error'] = traceback
.format_exc()
45 self
.response
.write(self
.render('cron.html', values
))
47 def _get_cron_jobs(self
):
48 cron_info
= self
._parse
_cron
_yaml
()
49 if not cron_info
or not cron_info
.cron
:
52 for entry
in cron_info
.cron
:
54 if not entry
.timezone
or pytz
:
55 now
= datetime
.datetime
.utcnow()
56 schedule
= groctimespecification
.GrocTimeSpecification(
57 entry
.schedule
, entry
.timezone
)
58 matches
= schedule
.GetMatches(now
, 3)
62 {'runtime': match
.strftime('%Y-%m-%d %H:%M:%SZ'),
63 'difference': str(match
- now
)})
67 def _parse_cron_yaml(self
):
68 """Loads the cron.yaml file and parses it.
71 A croninfo.CronInfoExternal containing cron jobs.
74 yaml_errors.Error, StandardError: The cron.yaml was invalid.
76 for cron_yaml
in ('cron.yaml', 'cron.yml'):
78 with
open(os
.path
.join(self
.configuration
.modules
[0].application_root
,
80 cron_info
= croninfo
.LoadSingleCron(f
)
87 self
.response
.status
= self
.dispatcher
.add_request(
89 relative_url
=self
.request
.get('url'),
90 headers
=[('X-AppEngine-Cron', 'true')],
92 source_ip
=REMOTE_IP
).status