Gossamer Forum
Home : Products : Others : Gossamer Community :

GCommunity maintenance script

Quote Reply
GCommunity maintenance script
GCommunity requires a daily maintenance script to take care of orphan sessions, automated user notifications for non validated accounts, birthday notices, automated deletion of non validated accounts for more than x days etc etc etc. I've been working on a simple maintenance script to take care of all this. The script is NOT ready, but as a skeleton/starting point will be of use to some of you. Because of the nature of mass notifications, deletetion of records and so on the actual functions are there, they report stats on changes but NOTHING is done to your instalation. As I said, is not finished but some of you might find some value in it.

Code:
#!/usr/local/bin/perl
# ==================================================================================
# GComm General Maintenance script
#
# eMail templates used (manually create if they don't exist):
# user_birthdate_reminder.eml => Template to remind users of birthday
# user_validate_reminder.eml => template to remind users to validate their accounts
#
# ==================================================================================

use strict;
use lib "$ENV{HOME}/libcommon/private/lib";
use Community qw/comm_init comm_config comm_fatal $DB $CFG/;
use Community::Web::User;
use GT::Date;
use vars qw /$SES_CREATE_DAYS $SES_ACCESS_DAYS $SESSIONS $DATABASE $DEBUG $DAYS $DAYS_TO_DELETE/;

Community::init("$ENV{HOME}/libcommon/private");

$SES_CREATE_DAYS = 30; # If a sessions was created more than this days ago DELETE it
$SES_ACCESS_DAYS = 1; # If a sessions was accesed more than this days ago DELETE it
$DATABASE = $DB->table('comm_users');
$SESSIONS = $DB->table('comm_sessions');
$DAYS = 3; # Wait how many days before reminding users?
$DAYS_TO_DELETE = 30; # Wait this many days before deleting accounts
$DEBUG = (@ARGV and $ARGV[0] eq "-d") ? 1 : 0;

# Each function returns how many where true conditions
my $validate = validate_reminder($DAYS);
my $bday = birthday();
my $delete = delete_users($DAYS_TO_DELETE);
my $ses_acc = delete_sessions($SES_ACCESS_DAYS,'sess_accessed'); # Delete sessions after this many day(s) after being ACCESED
my $ses_cre = delete_sessions($SES_CREATE_DAYS,'sess_created'); # Delete sessions after this many day(s) after being CREATED

print "
stats for GComm maintenance script:
$validate user(s) were reminded to validate their accounts today
$bday user(s) were sent (a) birthday email(s) today
$delete user(s) were deleted today for not validating their account(s) for more than $DAYS_TO_DELETE days
$ses_acc session(s) were deleted for accesing more than $SES_ACCESS_DAYS day(s) ago
$ses_cre session(s) were deleted for being created more than $SES_CREATE_DAYS day(s) ago
\n";

sub delete_sessions {
# -------------------------------------------------------------------
# Delete sessions
#
my ($days,$type) = @_;
my ($data,$cond);
my $daysago = (time - (86400 * $days));
$cond = GT::SQL::Condition->new($type, '>',$daysago);
$data = $SESSIONS->select ($cond);
return _delete_session($data);
}

sub delete_users {
# -------------------------------------------------------------------
# Delete Users created and not validated for more than 24 hours
#
my $days = shift;
my ($data,$cond);
my $daysago = (time - (86400 * $days));
$cond = GT::SQL::Condition->new('comm_email_val', '=','0','comm_created', '<',$daysago);
$data = $DATABASE->select ($cond);
return _delete_user($data);
}

sub validate_reminder {
# -------------------------------------------------------------------
# Remind users they need to validate their accounts.... (Users had to be created AT least ($DAYS) days before today's date)
# And users whose accounts were created more than $DAYS_TO_DELETE will not be notified since they will be deleted
#
my $days = shift;
my ($data,$cond);
my $daysago = (time - (86400 * $days));
my $notmorethan = (time - (86400 * $DAYS_TO_DELETE));
$cond = GT::SQL::Condition->new('comm_email_val', '=','0','comm_created', '<',$daysago,'comm_created', '>',$notmorethan);
$data = $DATABASE->select ($cond);
return _remind($data,'user_validate_reminder.eml');
}

sub birthday {
# ------------------------------------------------------------------------
# If user's B-date then send postcard or email or whatever... nice touch!!
#
my ($data,$cond,$cond2);
my $today_month = GT::Date::date_get(time,'%mm%');
my $today_day = GT::Date::date_get(time,'%dd%');
my $today_year = GT::Date::date_get(time,'%yyyy%');

$cond = GT::SQL::Condition->new(
'MONTH(prof_birthday)', '=', $today_month,
'DAYOFMONTH(prof_birthday)', '=', $today_day,
'prof_ReceiveMail', '=', 'Yes'
);

$cond2 = GT::SQL::Condition->new(
'MONTH(prof_birthday)', '=', '01',
'DAYOFMONTH(prof_birthday)', '=', '01',
'YEAR(prof_birthday)', '=', '1970'
)->not;

$data = $DATABASE->select ($cond,$cond2);
return _remind($data,'user_birthdate_reminder.eml');
}

sub _remind {
# ------------------------------------------------------------------
# Notify/remind users of important things
#
my ($sth,$tmpl) = @_;
my $howmany;
while (my $user = $sth->fetchrow_hashref) {
# Community::Web::User::send_email($tmpl, $user);
print $user->{comm_username} . " $user->{prof_birthday} " . $tmpl . "\n" if $DEBUG;
$howmany++;
}
return $howmany;
}

sub _delete_user {
# ------------------------------------------------------------------
# Delete users not validated for more that $DAYS_TO_DELETE days
#
my $sth = shift;
my $howmany;
while (my $user = $sth->fetchrow_hashref) {
# DELETE USER HERE....;
print $user->{comm_username} . " DELETED USER \n" if $DEBUG;
$howmany++;
}
return $howmany;
}

sub _delete_session {
# ------------------------------------------------------------------
# Delete sessions
#
my $sth = shift;
my $howmany;
while (my $user = $sth->fetchrow_hashref) {
# DELETE SESSIONS HERE....;
print $user->{sess_id} . " DELETED SESSION \n" if $DEBUG;
$howmany++;
}
return $howmany;
}
Quote Reply
Re: [jaltuve] GCommunity maintenance script In reply to
Hi,

Interesting start! Careful about the session part as that will delete the Remember Me sessions.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [jaltuve] GCommunity maintenance script In reply to
Hi Jaltuve,
did you ever finish this script?
Matthias

Matthias
gpaed.de