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