<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>程式設計 遇上 小提琴 &#187; TurboGears2</title>
	<atom:link href="http://blog.ez2learn.com/tag/turbogears2/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ez2learn.com</link>
	<description>Victor&#039;s個人部落格，關於程式設計與小提琴</description>
	<lastBuildDate>Tue, 07 Feb 2012 03:26:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Autostart script for TurboGears2</title>
		<link>http://blog.ez2learn.com/2009/09/20/autostart-script-for-turbogears2/</link>
		<comments>http://blog.ez2learn.com/2009/09/20/autostart-script-for-turbogears2/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 05:16:44 +0000</pubDate>
		<dc:creator>victor</dc:creator>
				<category><![CDATA[網站]]></category>
		<category><![CDATA[English Articles]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Autostart]]></category>
		<category><![CDATA[Pylons]]></category>
		<category><![CDATA[TurboGears2]]></category>
		<category><![CDATA[WebFaction]]></category>

		<guid isPermaLink="false">http://blog.ez2learn.com/?p=785</guid>
		<description><![CDATA[A autostart script for TurboGears2 and Pylons <a href="http://blog.ez2learn.com/2009/09/20/autostart-script-for-turbogears2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When I complete a TurboGears2 application, I got a problem, how to keep my tg2 application always running on <a href="http://www.webfaction.com/?affiliate=victorlin">Webfaction</a>? You know, there is scheduled downtime of virtul host. Therefore, if you just run "paster serve &#8211;daemon production.ini", once the machine down, your application is down, too. So you have to find a way to keep your tg2 application up. I can use crontab to check tg2 application every 5 minutes, but during the just-start-up 5 minutes, your application is not working, so it is not a good idea. I notice <a href="http://www.webfaction.com/?affiliate=victorlin">Webfaction</a> use a autostart CGI script for TurboGears1 application. So I decide to use that autostart CGI script to run my tg2 application. You might ask, what is autostart and how it works? Autostart script is a simple script to keep your web application up. It is executed when mod_rewrite of apache can&#8217;t connect to your server. By using autostart script, the application runs on-demand! If there are no users browse your application, it&#8217;s no need to run your application, that saves your memory usage.</p>
<p>However, the autostart <a href="http://www.webfaction.com/?affiliate=victorlin">Webfaction</a> provided is  for tg1, so I modify it for tg2, here is the modified version of autostart:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&lt;!</span><span style="color: black;">&#91;</span>CDATA<span style="color: black;">&#91;</span>
<span style="color: #808080; font-style: italic;">#!/bin/env python2.4</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Test if the process is already running</span>
running = <span style="color: #008000;">False</span>
<span style="color: #808080; font-style: italic;"># read status of tg2 application</span>
lines = <span style="color: #dc143c;">os</span>.<span style="color: black;">popen</span><span style="color: black;">&#40;</span>
    <span style="color: #483d8b;">'source /home/victorlin/webapps/tiange/tg2env/bin/activate;'</span>
    <span style="color: #483d8b;">'cd /home/victorlin/webapps/tiange/tiange/tiange;'</span>
    <span style="color: #483d8b;">'paster serve  status production.ini'</span><span style="color: black;">&#41;</span>.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
line = lines<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> line.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Server running in PID'</span><span style="color: black;">&#41;</span>:
    running = <span style="color: #008000;">True</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Content-type: text/html<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> running:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;&quot;&quot;&lt;head&gt;&lt;META HTTP-EQUIV=&quot;Refresh&quot; CONTENT=&quot;2; URL=.&quot;&gt;&lt;/head&gt;&lt;body&gt;
    Site is starting ...&lt;a href=&quot;.&quot; mce_href=&quot;.&quot;&gt;click here&lt;a&gt;&lt;/body&gt;&quot;&quot;&quot;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;&quot;&quot;&lt;head&gt;&lt;META HTTP-EQUIV=&quot;Refresh&quot; CONTENT=&quot;2; URL=.&quot;&gt;&lt;/head&gt;&lt;body&gt;
    Restarting site ...&lt;a href=&quot;.&quot; mce_href=&quot;.&quot;&gt;click here&lt;a&gt;&lt;/body&gt;&quot;&quot;&quot;</span>
    <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span>
        <span style="color: #483d8b;">'source /home/victorlin/webapps/tiange/tg2env/bin/activate;'</span>
        <span style="color: #483d8b;">'cd /home/victorlin/webapps/tiange/tiange/tiange;'</span>
        <span style="color: #483d8b;">'paster serve  daemon production.ini'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">&gt;</span></pre></div></div>

<p><a><br />
By the way, you can also use this script for Pylons applications. I hope this simple autostart script would be helpful :D</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ez2learn.com/2009/09/20/autostart-script-for-turbogears2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

