Merge lp:~achipa/web2py/cron into lp:~mdipierro/web2py/devel

Proposed by Attila Csipa on 2009-03-05
Status: Needs review
Proposed branch: lp:~achipa/web2py/cron
Merge into: lp:~mdipierro/web2py/devel
Diff against target: None lines
To merge this branch: bzr merge lp:~achipa/web2py/cron
Reviewer Review Type Date Requested Status
Massimo 2009-03-05 Pending
Review via email: mp+4193@code.launchpad.net
To post a comment you must log in.
Attila Csipa (achipa) wrote :

cross-crontype locking to avoid race contitions if several crontypes are activated at the same time (most often soft+external in wsgi mode)

lp:~achipa/web2py/cron updated on 2009-04-22
578. By Attila Csipa <bear@odin> on 2009-03-05

moved hardcron var setting from run to init

579. By Attila Csipa <bear@odin> on 2009-03-06

cleaner fail scenarios

580. By Attila Csipa <bear@odin> on 2009-03-06

sync merge

581. By Attila Csipa <bear@odin> on 2009-03-06

ignore cron.master, minor logging cosmetics

582. By Attila Csipa <bear@odin> on 2009-03-06

merge sync

583. By Attila Csipa <bear@odin> on 2009-03-24

@ prefixes

merge with latest

584. By Attila Csipa <bear@odin> on 2009-03-25

parametrized Popen, more whitespace friendly cron.py

585. By bear <bear@domU-12-31-38-01-B8-85> on 2009-04-22

erased superfluous logging statements

586. By bear <bear@domU-12-31-38-01-B8-85> on 2009-04-22

synced to 1.61.4

Unmerged revisions

586. By bear <bear@domU-12-31-38-01-B8-85> on 2009-04-22

synced to 1.61.4

585. By bear <bear@domU-12-31-38-01-B8-85> on 2009-04-22

erased superfluous logging statements

584. By Attila Csipa <bear@odin> on 2009-03-25

parametrized Popen, more whitespace friendly cron.py

583. By Attila Csipa <bear@odin> on 2009-03-24

@ prefixes

merge with latest

582. By Attila Csipa <bear@odin> on 2009-03-06

merge sync

581. By Attila Csipa <bear@odin> on 2009-03-06

ignore cron.master, minor logging cosmetics

580. By Attila Csipa <bear@odin> on 2009-03-06

sync merge

579. By Attila Csipa <bear@odin> on 2009-03-06

cleaner fail scenarios

578. By Attila Csipa <bear@odin> on 2009-03-05

moved hardcron var setting from run to init

577. By Attila (AchipA) Csipa <bear@odin> on 2009-03-05

upstream cleanup merge

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'applications/examples/views/default/cron.html'
2--- applications/examples/views/default/cron.html 1970-01-01 00:00:00 +0000
3+++ applications/examples/views/default/cron.html 2009-03-05 12:32:00 +0000
4@@ -0,0 +1,87 @@
5+{{extend 'layout.html'}}
6+{{import os}}
7+
8+<h1>web2py<sup style="font-size:0.5em;">TM</sup> cron</h1>
9+
10+The web2py cron provides the ability for applications to execute tasks at preset times, in a platform independent manner (tested on Windows XP, Linux and MacOS X, but should work on any python 2.5 supported platform).
11+
12+Cron functionality is defined by a crontab file (in regular <A href="http://en.wikipedia.org/wiki/Cron#crontab_syntax">crontab syntax</A>) in your application's cron directory. This also means that every application can have a separate cron configuration and that cron config can be changed from within web2py without affecting the host OS itself.
13+<BR>
14+<BR>
15+Example:
16+{{=CODE(""" 0-59/1 * * * * root python /path/to/python/script.py
17+*/30 * * * * root *applications/admin/cron/expire_sessions.py
18+-1 * * * * root *mycontroller/myfunction""")}}
19+
20+As you might have noticed, some of the lines use <A href="#extensions">extensions</A> to regular cron syntax to provide additional web2py functionality.
21+
22+<BR>
23+<h2>Modes of use</h2>
24+
25+Depending on how you are invoking web2py, there are four modes of operation for web2py cron.
26+
27+<UL>
28+<LI><A href="#soft">Soft cron</A>, available under all execution modes</LI>
29+<LI><A href="#hard">Hard cron</A>, available if using the built-in web server (either directly or via Apache mod_proxy)</LI>
30+<LI><A href="#external">External cron</A>, available if you have access to the systems own cron service</LI>
31+<LI><A href="#no">No cron</A></LI>
32+</UL>
33+
34+The default is hard cron if you are using the built-in web server, in all other cases the default is soft-cron.
35+
36+
37+<h3 id="soft">Soft cron</h3>
38+
39+Soft cron is the default if you are using CGI, FASTCGI or WSGI. Your tasks will be executed in the first call (page load) to web2py after the time specified in crontab (but AFTER processing the page, so no delay to the user is visible). Obviously, there is some uncertainty exactly when the task will be executed depending on the traffic the site receives. Also, the cron task may get interrupted if the web server has a page load timeout set. If these limitations are not acceptable, see <A href="#external">external cron</A>. Soft cron is a reasonable last resort, but if your web server allows other cron methods, they should be preferred over soft cron.
40+
41+<h3 id="hard">Hard cron</h3>
42+
43+Hard cron is the default if you are using the built-in web server (either directly or via Apache mod_proxy). Hard cron is executed in a parallel thread, so unlike soft cron there are no limitations with regard to run time or execution time precision.
44+
45+<h3 id="external">External cron</h3>
46+
47+External cron is not default in any scenarios, but requires you to have access to the system cron facilities. It runs in a parallel process, so none of the limitations of soft cron apply. This is the recommended way of using cron under WSGI or FASTCGI.
48+<BR>
49+<BR>
50+
51+Example of line to add to the system crontab, (usually /etc/crontab):
52+{{=CODE("""0-59/1 * * * * web2py cd /var/www/web2py/ && python web2py.py -C -D 1 >> /tmp/cron.output 2>&1""")}}
53+
54+If you are running external cron, make sure you add the -N command line parameter to your web2py startup script or config so there is no collision of multiple types of cron.
55+
56+<h3 id="no">No cron</h3>
57+
58+In case you do not need any cron functionality, you can use the -N command line parameter to disable it. Note that this will disable some maintenance tasks (like the automatic cleaning of session dirs).
59+
60+<BR>
61+<h2 id="extensions">Cron extensions</h2>
62+
63+Web2py cron has a some extra syntax to support web2py application specifics.
64+
65+<h3>Calling scripts in the web2py environment</h3>
66+
67+If the task/script is prefixed with an asterisk and ends with ".py", it will be executed in the web2py environment. This means you will have all the controllers and models at your disposal. Warning: be careful how you use models. While the execution happens in a separate process, database locks have to be taken into account in order to avoid pages waiting for cron tasks that be blocking the database.
68+
69+<BR>
70+<BR>
71+Example:
72+{{=CODE("""*/30 * * * * root *applications/admin/cron/expire_sessions.py""")}}
73+
74+<h3>Calling controller functions</h3>
75+
76+Same as above, but a function from a controller is executed instead of a separate script file.
77+<BR>
78+<BR>
79+
80+Example:
81+{{=CODE("""*/30 * * * * root *mycontroller/myfunction""")}}
82+
83+<h3>Application initialization</h3>
84+
85+If you specify -1 as minutes in the crontab file, the given task will be executed only ONCE, on web2py startup. You can use this feature if you want to precache, check or initialize data for an application on web2py startup.
86+
87+Example:
88+{{=CODE("""
89+-1 * * * * root *mycontroller/myfunction
90+""")}}
91+
92
93=== removed file 'applications/examples/views/default/cron.html'
94--- applications/examples/views/default/cron.html 2009-03-01 19:22:48 +0000
95+++ applications/examples/views/default/cron.html 1970-01-01 00:00:00 +0000
96@@ -1,87 +0,0 @@
97-{{extend 'layout.html'}}
98-{{import os}}
99-
100-<h1>web2py<sup style="font-size:0.5em;">TM</sup> cron</h1>
101-
102-The web2py cron provides the ability for applications to execute tasks at preset times, in a platform independent manner (tested on Windows XP, Linux and MacOS X, but should work on any python 2.5 supported platform).
103-
104-Cron functionality is defined by a crontab file (in regular <A href="http://en.wikipedia.org/wiki/Cron#crontab_syntax">crontab syntax</A>) in your application's cron directory. This also means that every application can have a separate cron configuration and that cron config can be changed from within web2py without affecting the host OS itself.
105-<BR>
106-<BR>
107-Example:
108-{{=CODE(""" 0-59/1 * * * * root python /path/to/python/script.py
109-*/30 * * * * root *applications/admin/cron/expire_sessions.py
110--1 * * * * root *mycontroller/myfunction""")}}
111-
112-As you might have noticed, some of the lines use <A href="#extensions">extensions</A> to regular cron syntax to provide additional web2py functionality.
113-
114-<BR>
115-<h2>Modes of use</h2>
116-
117-Depending on how you are invoking web2py, there are four modes of operation for web2py cron.
118-
119-<UL>
120-<LI><A href="#soft">Soft cron</A>, available under all execution modes</LI>
121-<LI><A href="#hard">Hard cron</A>, available if using the built-in web server (either directly or via Apache mod_proxy)</LI>
122-<LI><A href="#external">External cron</A>, available if you have access to the systems own cron service</LI>
123-<LI><A href="#no">No cron</A></LI>
124-</UL>
125-
126-The default is hard cron if you are using the built-in web server, in all other cases the default is soft-cron.
127-
128-
129-<h3 id="soft">Soft cron</h3>
130-
131-Soft cron is the default if you are using CGI, FASTCGI or WSGI. Your tasks will be executed in the first call (page load) to web2py after the time specified in crontab (but AFTER processing the page, so no delay to the user is visible). Obviously, there is some uncertainty exactly when the task will be executed depending on the traffic the site receives. Also, the cron task may get interrupted if the web server has a page load timeout set. If these limitations are not acceptable, see <A href="#external">external cron</A>. Soft cron is a reasonable last resort, but if your web server allows other cron methods, they should be preferred over soft cron.
132-
133-<h3 id="hard">Hard cron</h3>
134-
135-Hard cron is the default if you are using the built-in web server (either directly or via Apache mod_proxy). Hard cron is executed in a parallel thread, so unlike soft cron there are no limitations with regard to run time or execution time precision.
136-
137-<h3 id="external">External cron</h3>
138-
139-External cron is not default in any scenarios, but requires you to have access to the system cron facilities. It runs in a parallel process, so none of the limitations of soft cron apply. This is the recommended way of using cron under WSGI or FASTCGI.
140-<BR>
141-<BR>
142-
143-Example of line to add to the system crontab, (usually /etc/crontab):
144-{{=CODE("""0-59/1 * * * * web2py cd /var/www/web2py/ && touch applications/admin/cron/cron.master && python web2py.py -C -D 1 >> /tmp/cron.output 2>&1""")}}
145-
146-If you are running external cron, make sure you add the -N command line parameter to your web2py startup script or config so there is no collision of multiple types of cron.
147-
148-<h3 id="no">No cron</h3>
149-
150-In case you do not need any cron functionality, you can use the -N command line parameter to disable it. Note that this will disable some maintenance tasks (like the automatic cleaning of session dirs).
151-
152-<BR>
153-<h2 id="extensions">Cron extensions</h2>
154-
155-Web2py cron has a some extra syntax to support web2py application specifics.
156-
157-<h3>Calling scripts in the web2py environment</h3>
158-
159-If the task/script is prefixed with an asterisk and ends with ".py", it will be executed in the web2py environment. This means you will have all the controllers and models at your disposal. Warning: be careful how you use models. While the execution happens in a separate process, database locks have to be taken into account in order to avoid pages waiting for cron tasks that be blocking the database.
160-
161-<BR>
162-<BR>
163-Example:
164-{{=CODE("""*/30 * * * * root *applications/admin/cron/expire_sessions.py""")}}
165-
166-<h3>Calling controller functions</h3>
167-
168-Same as above, but a function from a controller is executed instead of a separate script file.
169-<BR>
170-<BR>
171-
172-Example:
173-{{=CODE("""*/30 * * * * root *mycontroller/myfunction""")}}
174-
175-<h3>Application initialization</h3>
176-
177-If you specify -1 as minutes in the crontab file, the given task will be executed only ONCE, on web2py startup. You can use this feature if you want to precache, check or initialize data for an application on web2py startup.
178-
179-Example:
180-{{=CODE("""
181--1 * * * * root *mycontroller/myfunction
182-""")}}
183-
184
185=== modified file 'gluon/contrib/cron.py'
186--- gluon/contrib/cron.py 2009-02-22 15:32:13 +0000
187+++ gluon/contrib/cron.py 2009-03-05 12:32:00 +0000
188@@ -31,7 +31,9 @@
189
190 def run(self):
191 logging.debug('External cron invocation')
192- crondance(apppath({'web2py_path': self.basedir}), 'ext')
193+ if tokenmaster(os.path.join(self.basedir, 'applications', 'admin', 'cron')):
194+ crondance(apppath({'web2py_path': self.basedir}), 'ext')
195+ tokenmaster(os.path.join(self.basedir, 'applications', 'admin', 'cron'), action = 'release')
196
197
198 class hardcron(threading.Thread):
199@@ -41,6 +43,12 @@
200 self.setDaemon(True)
201 self.basedir = os.getcwd()
202
203+ def launch(self):
204+ path = apppath({'web2py_path': self.basedir})
205+ if tokenmaster(os.path.join(path, 'admin', 'cron')):
206+ crondance(path, 'hard')
207+ tokenmaster(os.path.join(path, 'admin', 'cron'), action = 'release')
208+
209 def run(self):
210 global crontype
211 crontype = 'Hard'
212@@ -48,8 +56,7 @@
213 logging.info('Hard cron daemon started')
214 while True:
215 now = time.time()
216- s.enter(60 - now % 60, 1, crondance,
217- (apppath({'web2py_path' : self.basedir}), 'hard'))
218+ s.enter(60 - now % 60, 1, self.launch, ())
219 s.run()
220
221 class softcron(threading.Thread):
222@@ -62,33 +69,63 @@
223
224 def run(self):
225 path = apppath(self.env)
226- marker = os.path.join(path, 'admin/cron/cron.master') # location of Chronos, Master of All Time !
227- if not os.path.exists(marker): # cron master missing, try to recreate one
228- logging.warning('WEB2PY CRON: cron.master not found at %s. Trying to recreate.'
229- % marker)
230- mfile = open(marker, 'wb') # touch cron marker
231- mfile.close()
232-
233 now = time.time()
234- if 60 > now - self.cronmaster: # our own thread did a cron check less than a minute ago, don't even bother checking the file
235+ if self.cronmaster and 60 > now - self.cronmaster: # our own thread did a cron check less than a minute ago, don't even bother checking the file
236 logging.debug("Don't bother with cron.master, it's only %s s old"
237 % (now - self.cronmaster))
238 return
239
240- try:
241- self.cronmaster = os.stat(marker).st_mtime # get last_modified timestamp of cron.master file
242- except Exception, e:
243- self.cronmaster = 0
244- logging.warning('cron.master trouble: %s' % e)
245-
246 logging.debug('Cronmaster stamp: %s, Now: %s'
247 % (self.cronmaster, now))
248 if 60 <= now - self.cronmaster: # new minute, do the cron dance
249- mfile = open(marker, 'wb') # touch cron marker
250- mfile.close()
251- crondance(path, 'soft')
252-
253-
254+ self.cronmaster = tokenmaster(os.path.join(path, 'admin', 'cron'))
255+ if self.cronmaster:
256+ crondance(path, 'soft')
257+ self.cronmaster = tokenmaster(os.path.join(path, 'admin', 'cron'), action = 'release')
258+
259+def tokenmaster(path, db = None, action = 'claim'):
260+ token = os.path.join(path, 'cron.master')
261+ tokeninuse = os.path.join(path, 'cron.running')
262+
263+ if action == 'release':
264+ logging.debug('WEB2PY CRON: Releasing cron lock')
265+ os.unlink(tokeninuse)
266+ return time.time()
267+
268+ tokentime = os.stat(token).st_mtime
269+ if tokentime - ( tokentime % 60) + 60 > time.time(): # already ran in this minute
270+ return 0
271+
272+ if os.path.exists(tokeninuse): # running now
273+ logging.warning('alreadyrunning')
274+ if os.stat(tokeninuse).st_mtime + 60 < time.time(): # check if stale, just in case
275+ logging.warning('WEB2PY CRON: Stale cron.master detected')
276+ os.unlink(tokeninuse)
277+
278+ if not (os.path.exists(token) or os.path.exists(tokeninuse)): # no tokens, new install ? Need to regenerate anyho
279+ logging.warning("WEB2PY CRON: cron.master not found at %s. Trying to re-create." % token)
280+ try:
281+ mfile = open(token, 'wb')
282+ mfile.close()
283+ except:
284+ logging.error('WEB2PY CRON: Unable to re-create cron.master, cron functionality likely not available')
285+
286+ if os.path.exists(token) and not os.path.exists(tokeninuse): # has unclaimed token and not running
287+ logging.debug('WEB2PY CRON: Trying to acquire lock')
288+ try:
289+ os.rename(token, tokeninuse)
290+ mfile = open(token, 'wb') # can't must recreate and not rename as we need a correct claim time
291+ mfile.close()
292+ logging.debug('WEB2PY CRON: Locked')
293+ return os.stat(token).st_mtime
294+
295+ except:
296+ logging.info('WEB2PY CRON: Failed to claim %s' % token)
297+ return 0
298+
299+ logging.debug('WEB2PY CRON: already started from another process')
300+ return 0
301+
302 def apppath(env=None):
303 try:
304 apppath = os.path.join(env.get('web2py_path'), 'applications')