From df3736e81e56e598cb720fdc3f07af82cef7aac0 Mon Sep 17 00:00:00 2001 From: epriestley Date: Tue, 17 Jun 2014 15:33:08 -0700 Subject: [PATCH] Garbage collect daemon logs Summary: We already have GC for daemon log events, but not for daemon logs themselves. Collect old daemon logs which aren't still running. Test Plan: Ran `phd debug garbage`, observed old logs get cleaned up. Started some daemons, re-ran garbage, made sure they stuck around. Reviewers: joshuaspence Reviewed By: joshuaspence Subscribers: epriestley Differential Revision: https://secure.phabricator.com/D9610 --- .../sql/autopatches/20140617.daemonlog.sql | 2 ++ src/__phutil_library_map__.php | 2 ++ ...bricatorDaemonLogEventGarbageCollector.php | 24 +++++++++++++++++++ .../PhabricatorDaemonLogGarbageCollector.php | 7 +++--- 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 resources/sql/autopatches/20140617.daemonlog.sql create mode 100644 src/applications/daemon/garbagecollector/PhabricatorDaemonLogEventGarbageCollector.php diff --git a/resources/sql/autopatches/20140617.daemonlog.sql b/resources/sql/autopatches/20140617.daemonlog.sql new file mode 100644 index 0000000000..5904af039c --- /dev/null +++ b/resources/sql/autopatches/20140617.daemonlog.sql @@ -0,0 +1,2 @@ +ALTER TABLE {$NAMESPACE}_daemon.daemon_log + ADD KEY (dateCreated); diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index fab6d43807..cd8ff2e05c 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1454,6 +1454,7 @@ phutil_register_library_map(array( 'PhabricatorDaemonEventListener' => 'applications/daemon/event/PhabricatorDaemonEventListener.php', 'PhabricatorDaemonLog' => 'applications/daemon/storage/PhabricatorDaemonLog.php', 'PhabricatorDaemonLogEvent' => 'applications/daemon/storage/PhabricatorDaemonLogEvent.php', + 'PhabricatorDaemonLogEventGarbageCollector' => 'applications/daemon/garbagecollector/PhabricatorDaemonLogEventGarbageCollector.php', 'PhabricatorDaemonLogEventViewController' => 'applications/daemon/controller/PhabricatorDaemonLogEventViewController.php', 'PhabricatorDaemonLogEventsView' => 'applications/daemon/view/PhabricatorDaemonLogEventsView.php', 'PhabricatorDaemonLogGarbageCollector' => 'applications/daemon/garbagecollector/PhabricatorDaemonLogGarbageCollector.php', @@ -4265,6 +4266,7 @@ phutil_register_library_map(array( 1 => 'PhabricatorPolicyInterface', ), 'PhabricatorDaemonLogEvent' => 'PhabricatorDaemonDAO', + 'PhabricatorDaemonLogEventGarbageCollector' => 'PhabricatorGarbageCollector', 'PhabricatorDaemonLogEventViewController' => 'PhabricatorDaemonController', 'PhabricatorDaemonLogEventsView' => 'AphrontView', 'PhabricatorDaemonLogGarbageCollector' => 'PhabricatorGarbageCollector', diff --git a/src/applications/daemon/garbagecollector/PhabricatorDaemonLogEventGarbageCollector.php b/src/applications/daemon/garbagecollector/PhabricatorDaemonLogEventGarbageCollector.php new file mode 100644 index 0000000000..c5a502c599 --- /dev/null +++ b/src/applications/daemon/garbagecollector/PhabricatorDaemonLogEventGarbageCollector.php @@ -0,0 +1,24 @@ +establishConnection('w'); + + queryfx( + $conn_w, + 'DELETE FROM %T WHERE epoch < %d LIMIT 100', + $table->getTableName(), + time() - $ttl); + + return ($conn_w->getAffectedRows() == 100); + } + +} diff --git a/src/applications/daemon/garbagecollector/PhabricatorDaemonLogGarbageCollector.php b/src/applications/daemon/garbagecollector/PhabricatorDaemonLogGarbageCollector.php index 22a11523ae..bd8b92bf92 100644 --- a/src/applications/daemon/garbagecollector/PhabricatorDaemonLogGarbageCollector.php +++ b/src/applications/daemon/garbagecollector/PhabricatorDaemonLogGarbageCollector.php @@ -9,14 +9,15 @@ final class PhabricatorDaemonLogGarbageCollector return false; } - $table = new PhabricatorDaemonLogEvent(); + $table = new PhabricatorDaemonLog(); $conn_w = $table->establishConnection('w'); queryfx( $conn_w, - 'DELETE FROM %T WHERE epoch < %d LIMIT 100', + 'DELETE FROM %T WHERE dateCreated < %d AND status != %s LIMIT 100', $table->getTableName(), - time() - $ttl); + time() - $ttl, + PhabricatorDaemonLog::STATUS_RUNNING); return ($conn_w->getAffectedRows() == 100); }