Come impostare una registrazione “live” con pywws

Introduzione

There are two quite different modes of operation with pywws. Traditionally pywws.Hourly would be run at regular intervals (usually an hour) from cron. This is suitable for fairly static websites, but more frequent updates can be useful for sites such as Weather Underground (http://www.wunderground.com/). The newer pywws.LiveLog program runs continuously and can upload data every 48 seconds.

Guida introduttiva

Prima di tutto, è necessario installare pywws e assicurarsi che si possono ottenere i dati dalla tua stazione meteo. Vedere Come iniziare con pywws per i dettagli.

If you have previously been using pywws.Hourly then disable your “cron” job (or whatever else you use to run it) so it no longer runs. You should not run pywws.Hourly and pywws.LiveLog at the same time.

Try running pywws.LiveLog from the command line, with a high level of verbosity so you can see what’s happening. Use the pywws-livelog command to run pywws.LiveLog:

pywws-livelog -vvv ~/weather/data

Within five minutes (assuming you have set a 5 minute logging interval) you should see a “live_data new ptr” message, followed by fetching any new data from the weather station and processing it. Let pywws.LiveLog run for a minute or two longer, then kill the process by typing “<Ctrl>C”.

Cambiato nella versione 14.04.dev1194: the pywws-livelog command replaced scripts/pywws-livelog.py.

Percorsi dei file di configurazione

Aprire il file weather.ini con un editor di testo. Si dovrebbe avere una sezione di [paths] simile al seguente (dove xxx è il tuo nome utente):

[paths]
work = /tmp/weather
templates = /home/xxx/weather/templates/
graph_templates = /home/xxx/weather/graph_templates/
local_files = /home/xxx/weather/results/

Edit these to suit your installation and preferences. work is an existing temporary directory used to store intermediate files, templates is the directory where you keep your text template files, graph_templates is the directory where you keep your graph template files and local_files is a directory where template output that is not uploaded to your web site is put. Don’t use the pywws example directories for your templates, as they will get over-written when you upgrade pywws.

Copy your text and graph templates to the appropriate directories. You may find some of the examples provided with pywws useful to get started. The pywws-version -v command should show you where the examples are on your computer.

Nuovo nella versione 14.04.dev1194: the pywws-version command.

Configurazione dell’esecuzione periodica

In weather.ini si dovrebbe avere una sezione [live] simile alla seguente:

[live]
services = []
plot = []
text = []

Questa sezione specifica cosa pywws dovrebbe fare ogni volta che ottiene una nuova lettura dalla stazione meteo, cioè ogni 48 secondi. La voce services è un elenco di servizi meteo online dove caricare dati, ad esempio ['underground_rf']. Le voci plot e text sono elenchi di file di modello grafici e file di testo che devono essere elaborati e, opzionalmente, caricati sul tuo sito web. Probabilmente si dovrebbe lasciare tutto questo vuoto tranne che per i servizi services.

Se si utilizza YoWindow (http://yowindow.com/) si può aggiungere una voce alla sezione [live] per specificare il file di YoWindow, ad esempio:

[live]
services = ['underground_rf']
text = [('yowindow.xml', 'L')]
...

Si noti l’uso del flag 'L' – questo dice a pywws si inviare il risultato alla directory «local files» invece di caricarlo sul tuo sito ftp.

Se non li hai già, create quattro altre sezioni nel file weather.ini: [logged], [hourly], [12 hourly] e [daily]. Queste sezioni dovrebbero avere voci simili alla sezione [live] e specificare cosa fare ogni volta i dati vengono registrati (5-30 minuti, a seconda l’intervallo di registrazione), ogni ora, due volte al giorno e una volta al giorno. Aggiungere i nomi dei file di modello alle voci appropriate, ad esempio:

[logged]
services = ['underground', 'metoffice']
plot = []
text = []

[hourly]
services = []
plot = ['7days.png.xml', '24hrs.png.xml', 'rose_24hrs.png.xml']
text = [('tweet.txt', 'T'), '24hrs.txt', '6hrs.txt', '7days.txt']

[12 hourly]
services = []
plot = []
text = []

[daily]
services = []
plot = ['28days.png.xml']
text = [('forecast.txt', 'T'), 'allmonths.txt']

Si noti l’uso del flag 'T' – questo dice a pywws si inviare il risultato a Twitter invece di caricarlo sul proprio sito ftp.

Nuovo nella versione 14.05.dev1211: [cron name] sections. If you need more flexibility in when tasks are done you can use [cron name] sections. See weather.ini - configurazione del formato del file for more detail.

Cambiato nella versione 13.06_r1015: Aggiunto il flag 'T'. Precedentemente i modelli Twitter sono stati indicati separatamente in voci twitter in [hourly] e di altre sezioni. La sintassi precedente funziona ancora, ma è obsoleta.

Cambiato nella versione 13.05_r1013: aggiunto il modello 'yowindow.xml'. Precedentemente il file yowindow era generato da un modulo separato, richiamato dalla voce yowindow nella sezione [live]. La sintassi precedente funziona ancora, ma è obsoleta.

Upload asincrono

Nuovo nella versione 13.09_r1057.

Il caricamento dei dati in siti web o “services” a volte può richiedere del tempo, in particolare se un sito è andato off line e i tempi di caricamento sono lunghi. In condizioni di normale funzionamento pywws attende che tutti i caricamenti siano elaborati prima di scaricare ulteriori dati dalla stazione meteo. Questo può portare alcune volte a dati di essere mancanti.

The asynchronous item in the [config] section of weather.ini can be set to True to tell pywws.LiveLog to do these uploads in a separate thread.

Esecuzione in background

Nuovo nella versione 13.12.dev1118.

In order to have pywws.LiveLog carry on running after you finish using your computer it needs to be run as a «background job». On most Linux / UNIX systems you can do this by putting an ampersand (“&”) at the end of the command line. Running a job in the background like this doesn’t always work as expected: the job may suspend when you log out. It’s much better to run as a proper UNIX “daemon” process.

The pywws.livelogdaemon program does this, if you have the python-daemon library installed:

pywws-livelog-daemon -v ~/weather/data ~/weather/data/pywws.log start

Note that the log file is a required parameter, not an option.

Riavvio automatico

There are various ways of configuring a Linux system to start a program when the machine boots up. Typically these involve putting a file in /etc/init.d/, which requires root privileges. A slightly harder problem is ensuring a program restarts if it crashes. My solution to both problems is to run the following script from cron, several times an hour.

#!/bin/sh

export PATH=$PATH:/usr/local/bin

# exit if NTP hasn't set computer clock
[ `ntpdc -c sysinfo | awk '/stratum:/ {print $2}'` -ge 10 ] && exit

pidfile=/var/run/pywws.pid
datadir=/home/jim/weather/data
logfile=$datadir/live_logger.log

# exit if process is running
[ -f $pidfile ] && kill -0 `cat $pidfile` && exit

# email last few lines of the logfile to see why it died
if [ -f $logfile ]; then
  log=/tmp/log-weather
  tail -40 $logfile >$log
  /home/jim/scripts/email-log.sh $log "weather log"
  rm $log
  fi

# restart process
pywws-livelog-daemon -v -p $pidfile $datadir $logfile start

The process id of the daemon is stored in pidfile. If the process is running, the script does nothing. If the process has crashed, it emails the last 40 lines of the log file to me (using a script that creates a message and passes it to sendmail) and then restarts pywws.livelogdaemon. You’ll need to edit this quite a lot to suit your file locations and so on, but it gives some idea of what to do.


Commenti o domande? Si prega di iscriversi per al mailing list pywws http://groups.google.com/group/pywws e facci sapere.