Comment configurer le mode “live” avec pywws

Introduction

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.

Mise en route

Avant tout, vous devez installer pywws et vous assurer qu’il reçoit bien les informations de votre station météo. Voir Comment démarrer avec pywws pour plus de détails.

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”.

Modifié dans la version 14.04.dev1194: the pywws-livelog command replaced scripts/pywws-livelog.py.

Configurer l’emplacement des fichiers

Ouvrez votre fichier weather.ini avec un éditeur de texte. Vous devriez avoir une section [paths] similaire à ce qui suit (où xxx est votre nom d’usager):

[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.

Nouveau dans la version 14.04.dev1194: the pywws-version command.

Configurer les tâches périodiques

Dans weather.ini vous devriez avoir une section [live] similaire à celle-ci:

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

This section specifies what pywws should do every time it gets a new reading from the weather station, i.e. every 48 seconds. The services entry is a list of online weather services to upload data to, e.g. ['underground_rf']. The plot and text entries are lists of template files for plots and text files to be processed and, optionally, uploaded to your web site. You should probably leave all of these blank except for services.

Si vous utilisez YoWindow (http://yowindow.com/) vous pouvez ajouter l’entrée à la section [live] pour spécifier votre fichier YoWindow, ex.:

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

Note the use of the 'L' flag – this tells pywws to copy the template result to your « local files » directory instead of uploading it to your ftp site.

Si vous ne les avez pas déjà, créez quatre sections supplémentaires dans votre fichier weather.ini : [logged], [hourly], [12 hourly] et [daily]. Ces sections doivent avoir des entrées similaires à la section [live], et spécifiez ce qui doit être fait chaque fois qu’une donnée est enregistrée (5 à 30 minutes, dépendant de votre intervalle), chaque heure, deux fois par jour et chaque jour. Ajoutez les noms de vos fichiers de gabarit à l’entrée appropriée, par exemple:

[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']

Note the use of the 'T' flag – this tells pywws to send the template result to Twitter instead of uploading it to your ftp site.

Nouveau dans la version 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 - format du fichier de configuration for more detail.

Modifié dans la version 13.06_r1015: added the 'T' flag. Previously Twitter templates were listed separately in twitter entries in the [hourly] and other sections. The older syntax still works, but is deprecated.

Modifié dans la version 13.05_r1013: added a 'yowindow.xml' template. Previously yowindow files were generated by a separate module, invoked by a yowindow entry in the [live] section. This older syntax still works, but is deprecated.

Asynchronous uploads

Nouveau dans la version 13.09_r1057.

Uploading data to web sites or “services” can sometimes take a long time, particularly if a site has gone off line and the upload times out. In normal operation pywws waits until all uploads have been processed before fetching any more data from the weather station. This can lead to data sometimes being missed.

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.

Exécuter en arrière-plan

Nouveau dans la version 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.

Redémarrage automatique

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.


Commentaires et questions? SVP, abonnez-vous à la liste pywws http://groups.google.com/group/pywws et faites-vous entendre.