summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pinger.pm
blob: 0aee17f8a582235af049ae5b5a53cc6697e9f0fe (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::pinger;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. my %pages;
  7. my $pinged=0;
  8. sub import { #{{{
  9. hook(type => "getsetup", id => "pinger", call => \&getsetup);
  10. hook(type => "needsbuild", id => "pinger", call => \&needsbuild);
  11. hook(type => "preprocess", id => "ping", call => \&preprocess);
  12. hook(type => "delete", id => "pinger", call => \&ping);
  13. hook(type => "change", id => "pinger", call => \&ping);
  14. } # }}}
  15. sub getsetup () { #{{{
  16. return
  17. pinger_timeout => {
  18. type => "integer",
  19. default => 15,
  20. description => "how many seconds to try pinging before timing out",
  21. safe => 1,
  22. rebuild => 0,
  23. },
  24. } #}}}
  25. sub needsbuild (@) { #{{{
  26. my $needsbuild=shift;
  27. foreach my $page (keys %pagestate) {
  28. if (exists $pagestate{$page}{pinger}) {
  29. $pages{$page}=1;
  30. if (exists $pagesources{$page} &&
  31. grep { $_ eq $pagesources{$page} } @$needsbuild) {
  32. # remove state, will be re-added if
  33. # the ping directive is still present
  34. # on rebuild.
  35. delete $pagestate{$page}{pinger};
  36. }
  37. }
  38. }
  39. } # }}}
  40. sub preprocess (@) { #{{{
  41. my %params=@_;
  42. if (! exists $params{from} || ! exists $params{to}) {
  43. error gettext("requires 'from' and 'to' parameters");
  44. }
  45. if ($params{from} eq $config{url}) {
  46. $pagestate{$params{destpage}}{pinger}{$params{to}}=1;
  47. $pages{$params{destpage}}=1;
  48. return sprintf(gettext("Will ping %s"), $params{to});
  49. }
  50. else {
  51. return sprintf(gettext("Ignoring ping directive for wiki %s (this wiki is %s)"), $params{from}, $config{url});
  52. }
  53. } # }}}
  54. sub ping {
  55. if (! $pinged && %pages) {
  56. $pinged=1;
  57. my $ua;
  58. eval q{use LWPx::ParanoidAgent};
  59. if (!$@) {
  60. $ua=LWPx::ParanoidAgent->new;
  61. }
  62. else {
  63. eval q{use LWP};
  64. if ($@) {
  65. debug(gettext("LWP not found, not pinging"));
  66. return;
  67. }
  68. $ua=LWP::UserAgent->new;
  69. }
  70. $ua->timeout($config{pinger_timeout} || 15);
  71. # daemonise here so slow pings don't slow down wiki updates
  72. defined(my $pid = fork) or error("Can't fork: $!");
  73. return if $pid;
  74. chdir '/';
  75. open STDIN, '/dev/null';
  76. open STDOUT, '>/dev/null';
  77. POSIX::setsid() or error("Can't start a new session: $!");
  78. open STDERR, '>&STDOUT' or error("Can't dup stdout: $!");
  79. # Don't need to keep a lock on the wiki as a daemon.
  80. IkiWiki::unlockwiki();
  81. my %urls;
  82. foreach my $page (%pages) {
  83. if (exists $pagestate{$page}{pinger}) {
  84. $urls{$_}=1 foreach keys %{$pagestate{$page}{pinger}};
  85. }
  86. }
  87. foreach my $url (keys %urls) {
  88. # Try to avoid pinging ourselves. If this check
  89. # fails, it's not the end of the world, since we
  90. # only ping when a page was changed, so a ping loop
  91. # will still be avoided.
  92. next if $url=~/^\Q$config{cgiurl}\E/;
  93. $ua->head($url);
  94. }
  95. exit 0;
  96. }
  97. }
  98. 1