Hi,
I made up a new type of ticket number,
it is composed this way: year.month.day.Counter
The Counter must reset to zero every midnight, so I added a cron to the
crontab.
But my problem is:
What happend if someone store a new ticket at 23:59:59 of 11/12/02
and it takes to form the ticket number this date.
The cron runs at 00 of 12/12/02. and the Counter is taken after the cron
changes it to 0 then i'll have the ticket number 20021211001, and not the
last ticket of 20021211.
Its a litle confused.
There is a critical region between the program takes the date until the
program takes the counter. So the file where the Counter is saved, must be
blocked inside that critical region. And the cron, must retry if the file
is blocked.
Here are the files
I create
/opt/OpenTRS/var/log/cero.log with 0
I create
/opt/OpenTRS/var/cron/reset/reset_ticket_counter (look botom of this mail)
#crontab -e -u otrs
:1000 #to go to the end of th file.
:r /opt/OpenTRS/var/cron/reset/reset_ticket_counter
:wq
# --
# Ticket/Number/DateCounter.pm - a date ticket number generator
# Copyright (C) 2002 Diego Fernandez
# --
# $Id: DateCounter.pm,v 1.1 2002/12/12 10:41:24 diego Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --
# Note:
# available objects are: ConfigObject, LogObject and DBObject
#
# Generates ticket numbers like yyyymmddss.... (e. g. 200206231010138)
# --
package Kernel::System::Ticket::Number::DateCounter;
use strict;
use vars qw($VERSION);
$VERSION = '$Revision: 1.2 $';
$VERSION =~ s/^.*:\s(\d+\.\d+)\s.*$/$1/;
sub CreateTicketNr {
my $Self = shift;
my $JumpCounter = shift || 0;
# --
# get needed config options
# --
$Self->{CounterLog} = $Self->{ConfigObject}->Get('CounterLog');
$Self->{SystemID} = $Self->{ConfigObject}->Get('SystemID');
my ($Sec, $Min, $Hour, $Day, $Month, $Year) = localtime(time);
$Year = $Year+1900;
$Month = $Month+1;
$Month = "0$Month" if ($Month <10);
$Day = "0$Day" if ($Day <10);
# --
# read count
# --
open (COUNTER, "< $Self->{CounterLog}") || die "Can't open
$Self->{CounterLog}";
my $Line = <COUNTER>;
my ($Count) = split(/;/, $Line);
close (COUNTER);
if ($Self->{Debug} > 0) {
$Self->{LogObject}->Log(
Priority => 'debug',
MSG => "Read counter: $Count",
);
}
# --
# count auto increment ($Count++)
# --
$Count++;
$Count = $Count + $JumpCounter;
# --
# write new count
# --
if (open (COUNTER, "> $Self->{CounterLog}")) {
flock (COUNTER, 2) || warn "Can't set file lock
($Self->{CounterLog}): $!";
print COUNTER $Count . "\n";
close (COUNTER);
if ($Self->{Debug} > 0) {
$Self->{LogObject}->Log(
Priority => 'debug',
MSG => "Write counter: $Count",
);
}
}
else {
$Self->{LogObject}->Log(
Priority => 'error',
MSG => "Can't open $Self->{CounterLog}: $!",
);
die;
}
# --
# new ticket number
# --
$Count = "0$Count" if ($Count <100 && $Count >9);
$Count = "00$Count" if ($Count <10);
my $Tn = $Year.$Month.$Day.$Count;
# --
# Check ticket number. If exists generate new one!
# --
if ($Self->CheckTicketNr(Tn=>$Tn)) {
$Self->{LoopProtectionCounter}++;
if ($Self->{LoopProtectionCounter} >= 1000) {
# loop protection
$Self->{LogObject}->Log(
Priority => 'error',
MSG => "CounterLoopProtection is now
$Self->{LoopProtectionCounter}!".
" Stoped CreateTicketNr()!",
);
return;
}
# --
# create new ticket number again
# --
$Self->{LogObject}->Log(
Priority => 'notice',
MSG => "Tn ($Tn) exists! Creating new one.",
);
$Tn = $Self->CreateTicketNr($Self->{LoopProtectionCounter});
}
return $Tn;
}
# --
sub GetTNByString {
my $Self = shift;
my $String = shift || return;
# --
# get needed config options
# --
my $TicketHook = $Self->{ConfigObject}->Get('TicketHook');
# --
# check ticket number
# --
if ($String =~ /$TicketHook:+.{0,1}(\d{8,40})/i) {
return $1;
}
else {
return;
}
}
# --
# --
# cron/reset_ticket_counter - Resets ticket Counter to 0 for OTRS
# Copyright (C) 2002 Diego Fernandez
# --
# $Id: reset_ticket_counter,v 1.1 2002/12/12 11:22:38 diego Exp $
# This software comes with ABSOLUTELY NO WARRANTY.
# --
# just every day
0 0 * * * cp $HOME/var/log/cero.log $HOME/var/log/TicketCounter.log
Thanks,
Diego.