De:OnLoad

Aus YaCyWiki
Wechseln zu: Navigation, Suche

Der Artikel beschreibt wie YaCy abhängig vom Load des *nix-System gesteuert werden kann.

#!/bin/sh

maxload=2.30
http_user=admin
http_password=deinpassword

#$1 = 1 minute
#$2 = 5 minutes
#$3 = 15 minutes
load=$(cat /proc/loadavg| awk '{print $1}')

if perl -e 'if('$load'>='$maxload'){exit(1)};' #compare floats
then
   wget -q -O /dev/null --http-user $http_user --http-password $http_password "http://localhost:8080/WatchCrawler_p.html?continuecrawlqueue=true"
else
   wget -q -O /dev/null --http-user $http_user --http-password $http_password "http://localhost:8080/WatchCrawler_p.html?pausecrawlqueue=true"
fi

Das Script sollte dann von einem Cronjob aufgerufen werden der in kurzen Intervallen (z.B. 5 min.) läuft.

Umsetzung in Perl

Das funktioniert nur da wo man auch ein /proc/loadav auswerten kann.

#!/usr/bin/perl

use LWP::Simple;
use LWP::UserAgent;

use constant YACY  => 'http://USER:PASSWORD@localhost:8080/WatchCrawler_p.html'; # ANPASSEN
use constant ON    => '?continuecrawlqueue=true';
use constant OFF   => '?pausecrawlqueue=true';

$wait=60*5; # Sekunden
$load=2.0;  # Max Load

$ua=new LWP::UserAgent;
$ua->timeout(60);
$req=new HTTP::Request 'GET'=>YACY.ON;
$res=$ua->request($req);
$res->is_success() ? $res->content() : "ERROR: ".$res->code()." ".$res->message();
$on=1;

while (1) {
    open(TMP, "/proc/loadavg") || die "Failed to open /proc/loadav\n$!";
    while(<TMP>) { $l=index($_," "); $l=substr($_,$l+1,index($_," ")); }
    close(TMP);

    if ($l<$load && $on==0) {
      $on=1;
      $req = new HTTP::Request 'GET' => YACY . ON;
      $res = $ua->request($req);
      $res->is_success() ? $res->content() : "ERROR: " . $res->code() . " " . $res->message();
    } elsif ($l>$load && $on==1) {
      $on=0;
      $req = new HTTP::Request 'GET' => YACY . OFF;
      $res = $ua->request($req);
      $res->is_success() ? $res->content() : "ERROR: " . $res->code() . " " . $res->message();
    }
    sleep($wait);
}
exit;