mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)
Mark Smith ([staff profile] mark) wrote in [site community profile] changelog2010-05-03 05:31 am

[dw-free] expire carts after 30 days

[commit: http://hg.dwscoalition.org/dw-free/rev/f808bb62f298]

http://bugs.dwscoalition.org/show_bug.cgi?id=2274

Expire carts that are untouched for 30 days, and also update the time on a
cart when it gets items added or removed. Should help alleviate 'my cart is
from weeks ago' confusion.

Patch by [staff profile] mark.

Files modified:
  • bin/worker/paidstatus
  • cgi-bin/DW/Shop/Cart.pm
--------------------------------------------------------------------------------
diff -r e7287f847215 -r f808bb62f298 bin/worker/paidstatus
--- a/bin/worker/paidstatus	Sun May 02 07:32:12 2010 +0000
+++ b/bin/worker/paidstatus	Mon May 03 05:31:11 2010 +0000
@@ -100,6 +100,14 @@ sub main_loop {
     # now get a db or die
     my $dbh = LJ::get_db_writer()
         or return $log->( 'Unable to get database writer handle.' );
+
+## PHASE 0) REMOVE DEAD CARTS (unused for more than 30 days)
+
+    my $ct = $dbh->do( 'UPDATE shop_carts SET state = ? WHERE state = ? AND starttime < UNIX_TIMESTAMP() - 86400 * 30',
+                       undef, $DW::Shop::STATE_CLOSED, $DW::Shop::STATE_OPEN );
+    return $log->( 'Database error cleaning carts: %s', $dbh->errstr )
+        if $dbh->err;
+    $log->( 'Cleaned %d carts that were unused for more than 30 days.', $ct+0 );
 
 ## PHASE 1) PROCESS PAYMENTS
 
diff -r e7287f847215 -r f808bb62f298 cgi-bin/DW/Shop/Cart.pm
--- a/cgi-bin/DW/Shop/Cart.pm	Sun May 02 07:32:12 2010 +0000
+++ b/cgi-bin/DW/Shop/Cart.pm	Mon May 03 05:31:11 2010 +0000
@@ -303,6 +303,7 @@ sub add_item {
     LJ::Hooks::run_hooks( 'shop_cart_added_item', $self, $item );
 
     # save to db and return
+    $self->_touch;
     $self->save || return( 0, 'Unable to save cart.' );
     return 1;
 }
@@ -337,6 +338,7 @@ sub remove_item {
 
     # now recalculate the costs and save
     $self->recalculate_costs;
+    $self->_touch;
     $self->save;
 
     # now run the hook, this is later so that we've updated the cart already
@@ -478,7 +480,7 @@ sub email {
 ################################################################################
 
 
-sub id       { $_[0]->{cartid}             } 
+sub id       { $_[0]->{cartid}             }
 sub userid   { $_[0]->{userid}             }
 sub starttime{ $_[0]->{starttime}          }
 sub age      { time() - $_[0]->{starttime} }
@@ -531,5 +533,11 @@ sub _build {
     return bless $cart, $class;
 }
 
+# called to update our access time, this is mostly an internal method, but anybody
+# that has reason to can call it;  note that this needs to be called before a save
+sub _touch {
+    $_[0]->{starttime} = time;
+}
+
 
 1;
--------------------------------------------------------------------------------
denise: Image: Me, facing away from camera, on top of the Castel Sant'Angelo in Rome (Default)

[staff profile] denise 2010-05-03 05:34 am (UTC)(link)
omg thank you.
rydra_wong: "dreamwidth" on red/pink clouds (dreamwidth -- clouds)

[personal profile] rydra_wong 2010-05-03 07:08 am (UTC)(link)
\o/ Thank you!