if ($self->settings->{article_number_policy} eq 'update_prices') {
if ($entry->{part}) {
- map { $entry->{part}->$_( $object->$_ ) } qw(sellprice listprice lastcost);
+ map { $entry->{part}->$_( $object->$_ ) if defined $object->$_ } qw(sellprice listprice lastcost);
# merge prices
my %prices_by_pricegroup_id = map { $_->pricegroup->id => $_ } $entry->{part}->prices, $object->prices;
# update parts table by setting lastcost to current price, don't allow negative values by using abs
$query = qq|UPDATE parts SET lastcost = ? WHERE id = ?|;
- @values = (abs($form->{"sellprice_$i"}), conv_i($form->{"id_$i"}));
+ @values = (abs($form->{"sellprice_$i"} / $basefactor), conv_i($form->{"id_$i"}));
do_query($form, $dbh, $query, @values);
# check if we sold the item already and
# update lastcost
$query = qq|UPDATE parts SET lastcost = ? WHERE id = ?|;
- do_query($form, $dbh, $query, $form->{"sellprice_$i"}, conv_i($form->{"id_$i"}));
+ do_query($form, $dbh, $query, $form->{"sellprice_$i"} / $basefactor, conv_i($form->{"id_$i"}));
}
next if $payments_only;
use SL::SessionFile;
# Create a session file named "customer.csv" (relative names only)
- my $sfile = SL::SessionFile->new("customer.csv", "w");
+ my $sfile = SL::SessionFile->new('customer.csv', mode => 'w');
$sfile->fh->print("col1;col2;col3\n" .
"value1;value2;value3\n");
$sfile->fh->close;
--- /dev/null
+package SL::System::Process;
+
+use strict;
+
+use parent qw(Rose::Object);
+
+use English qw(-no_match_vars);
+use File::Spec;
+use File::Basename;
+
+sub exe_dir {
+ my $dir = dirname(File::Spec->rel2abs($PROGRAM_NAME));
+ my $system_dir = File::Spec->catdir($dir, 'SL', 'System');
+ return $dir if -d $system_dir && -f File::Spec->catfile($system_dir, 'TaskServer.pm');
+
+ my @dirs = reverse File::Spec->splitdir($dir);
+ shift @dirs;
+ $dir = File::Spec->catdir(reverse @dirs);
+ $system_dir = File::Spec->catdir($dir, 'SL', 'System');
+ return File::Spec->curdir unless -d $system_dir && -f File::Spec->catfile($system_dir, 'TaskServer.pm');
+
+ return $dir;
+}
+
+1;
+__END__
+
+=pod
+
+=encoding utf8
+
+=head1 NAME
+
+SL::System::Process - assorted system-relevant functions
+
+=head1 SYNOPSIS
+
+ # Get base path to Kivitendo scripts
+ my $path = SL::System::Process->exe_dir;
+
+=head1 FUNCTIONS
+
+=over 4
+
+=item C<exe_dir>
+
+Returns the absolute path to the directory the Kivitendo executables
+(C<login.pl> etc.) and modules (sub-directory C<SL/> etc.) are located
+in.
+
+=back
+
+=head1 BUGS
+
+Nothing here yet.
+
+=head1 AUTHOR
+
+Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
+
+=cut
--- /dev/null
+package SL::System::TaskServer;
+
+use strict;
+
+use parent qw(Rose::Object);
+
+use Rose::Object::MakeMethods::Generic (
+ scalar => [ qw(last_command_output) ],
+);
+
+use File::Slurp;
+use File::Spec::Functions qw(:ALL);
+
+use SL::System::Process;
+
+use constant {
+ OK => 0,
+ ERR_PID_FILE => -1,
+ ERR_PROCESS => -2,
+};
+
+sub status {
+ my ($self) = @_;
+
+ my $pid = $self->_read_pid;
+ return ERR_PID_FILE unless $pid;
+
+ return kill(0, $pid) ? OK : ERR_PROCESS;
+}
+
+sub is_running {
+ my ($self) = @_;
+
+ return $self->status == OK;
+}
+
+sub start {
+ my ($self) = @_;
+
+ return $self->_run_script_command('start');
+}
+
+sub stop {
+ my ($self) = @_;
+
+ return $self->_run_script_command('stop');
+}
+
+sub wake_up {
+ my ($self) = @_;
+
+ my $pid = $self->_read_pid;
+ return undef unless $pid;
+ return kill('ALRM', $pid) ? 1 : undef;
+}
+
+#
+# private methods
+#
+
+sub _read_pid {
+ my ($self) = @_;
+
+ my $exe_dir = SL::System::Process->exe_dir;
+ my $pid_file_name = join '.', splitdir($exe_dir), 'config.lx_office.conf.pid';
+ my $pid_file_path = catfile(catdir($exe_dir, 'users', 'pid'), $pid_file_name);
+
+ return undef unless -f $pid_file_path;
+ return join('', read_file($pid_file_path)) * 1;
+}
+
+sub _run_script_command {
+ my ($self, $command) = @_;
+
+ my $exe = catfile(catdir(SL::System::Process->exe_dir, 'scripts'), 'task_server.pl');
+ $self->last_command_output(`${exe} ${command}`);
+
+ return $? == 0 ? 1 : undef;
+}
+
+1;
+__END__
+
+=pod
+
+=encoding utf8
+
+=head1 NAME
+
+SL::System::TaskServer - programmatic interface to the external task server component
+
+=head1 SYNOPSIS
+
+ # Create interface
+ my $task_server = SL->TaskServer->new;
+
+ # Start the server if it is not running
+ if (!$task_server->is_running) {
+ $task_server->start;
+ }
+
+ # Stop it if it is running
+ if ($task_server->is_running) {
+ $task_server->stop;
+ }
+
+=head1 FUNCTIONS
+
+=over 4
+
+=item C<is_running>
+
+Returns C<trueish> if the server is running. This is done by using
+Perl's C<kill> function with a "signal" of C<0> for the process ID
+which in turn is read from the daemon's PID file.
+
+If the PID file is not found or if C<kill> returns a non-successful
+value then a C<falsish> value is returned.
+
+=item C<last_command_output>
+
+Returns the output of the last C<system> command executed, e.g. from a
+call to L<start> or L<stop>.
+
+=item C<start>
+
+Starts the task server. Does not check whether or not it is running,
+neither before not after trying to start it.
+
+Returns C<1> if the system command C<./scripts/task_server.pl start>
+exits with an exit code of C<0> and C<undef> otherwise.
+
+The command's output can be queried with L<last_command_output>.
+
+=item C<status>
+
+Queries the task server status. Returns one of these values:
+
+=over 4
+
+=item *
+
+C<OK> or C<0>: the task server is running and signals can be sent to
+it.
+
+=item *
+
+C<ERR_PID_FILE> or C<-1>: the PID file could not be found or read
+
+=item *
+
+C<ERR_PROCESS> or C<-2>: the PID file could was found and read, but
+it's not possible to send signals to the process, e.g. because it is
+not running or owned by a different user ID.
+
+=back
+
+=item C<stop>
+
+Stops the task server. Does not check whether or not it is running,
+neither before not after trying to start it.
+
+Returns C<1> if the system command C<./scripts/task_server.pl stop>
+exits with an exit code of C<0> and C<undef> otherwise.
+
+The command's output can be queried with L<last_command_output>.
+
+=item C<wake_up>
+
+Sends a signal to the task server process causing it to wake up and
+process its job queue immediately.
+
+Returns C<1> if the signal could be sent and C<undef> otherwise.
+
+=back
+
+=head1 BUGS
+
+Nothing here yet.
+
+=head1 AUTHOR
+
+Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
+
+=cut
Core)</title>
<para>Kopieren Sie die Datei
- <filename>scripts/boot/system-v/lx-office-task-server</filename>
- nach <filename>/etc/init.d/lx-office-task-server</filename>. Passen
+ <filename>scripts/boot/system-v/kivitendo-server</filename>
+ nach <filename>/etc/init.d/kivitendo-server</filename>. Passen
Sie in der kopierten Datei den Pfad zum Task-Server an (Zeile
<literal>DAEMON=....</literal>). Binden Sie das Script in den
Boot-Prozess ein. Dies ist distributionsabhängig:</para>
<listitem>
<para>Debian-basierende Systeme:</para>
- <programlisting>update-rc.d lx-office-task-server defaults
+ <programlisting>update-rc.d kivitendo-task-server defaults
# Nur bei Debian Squeeze und neuer:
-insserv lx-office-task-server</programlisting>
+insserv kivitendo-task-server</programlisting>
</listitem>
<listitem>
<para>OpenSuSE und Fedora Core:</para>
- <programlisting>chkconfig --add lx-office-task-server</programlisting>
+ <programlisting>chkconfig --add kivitendo-task-server</programlisting>
</listitem>
</itemizedlist>
<para>Danach kann der Task-Server mit dem folgenden Befehl gestartet
- werden: <command>/etc/init.d/lx-office-task-server
+ werden: <command>/etc/init.d/kivitendo-task-server
start</command></para>
</sect3>
<title>Upstart-basierende Systeme (z.B. Ubuntu)</title>
<para>Kopieren Sie die Datei
- <filename>scripts/boot/upstart/lx-office-task-server.conf</filename>
- nach <filename>/etc/init/lx-office-task-server.conf</filename>.
+ <filename>scripts/boot/upstart/kivitendo-task-server.conf</filename>
+ nach <filename>/etc/init/kivitendo-task-server.conf</filename>.
Passen Sie in der kopierten Datei den Pfad zum Task-Server an (Zeile
<literal>exec ....</literal>).</para>
<para>Danach kann der Task-Server mit dem folgenden Befehl gestartet
- werden: <command>service lx-office-task-server
+ werden: <command>service kivitendo-task-server
start</command></para>
</sect3>
</sect2>
zwangsläufig funktioniert, werden auch Start-Scripte mitgeliefert, die
anstelle eines symbolischen Links verwendet werden können.</p><div class="sect3" title="2.6.2.1. SystemV-basierende Systeme (z.B. Debian, OpenSuSE, Fedora Core)"><div class="titlepage"><div><div><h4 class="title"><a name="d0e695"></a>2.6.2.1. SystemV-basierende Systeme (z.B. Debian, OpenSuSE, Fedora
Core)</h4></div></div></div><p>Kopieren Sie die Datei
- <code class="filename">scripts/boot/system-v/lx-office-task-server</code>
- nach <code class="filename">/etc/init.d/lx-office-task-server</code>. Passen
+ <code class="filename">scripts/boot/system-v/kivitendo-server</code>
+ nach <code class="filename">/etc/init.d/kivitendo-server</code>. Passen
Sie in der kopierten Datei den Pfad zum Task-Server an (Zeile
<code class="literal">DAEMON=....</code>). Binden Sie das Script in den
- Boot-Prozess ein. Dies ist distributionsabhängig:</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>Debian-basierende Systeme:</p><pre class="programlisting">update-rc.d lx-office-task-server defaults
+ Boot-Prozess ein. Dies ist distributionsabhängig:</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>Debian-basierende Systeme:</p><pre class="programlisting">update-rc.d kivitendo-task-server defaults
# Nur bei Debian Squeeze und neuer:
-insserv lx-office-task-server</pre></li><li class="listitem"><p>OpenSuSE und Fedora Core:</p><pre class="programlisting">chkconfig --add lx-office-task-server</pre></li></ul></div><p>Danach kann der Task-Server mit dem folgenden Befehl gestartet
- werden: <span class="command"><strong>/etc/init.d/lx-office-task-server
+insserv kivitendo-task-server</pre></li><li class="listitem"><p>OpenSuSE und Fedora Core:</p><pre class="programlisting">chkconfig --add kivitendo-task-server</pre></li></ul></div><p>Danach kann der Task-Server mit dem folgenden Befehl gestartet
+ werden: <span class="command"><strong>/etc/init.d/kivitendo-task-server
start</strong></span>
</p></div><div class="sect3" title="2.6.2.2. Upstart-basierende Systeme (z.B. Ubuntu)"><div class="titlepage"><div><div><h4 class="title"><a name="d0e725"></a>2.6.2.2. Upstart-basierende Systeme (z.B. Ubuntu)</h4></div></div></div><p>Kopieren Sie die Datei
- <code class="filename">scripts/boot/upstart/lx-office-task-server.conf</code>
- nach <code class="filename">/etc/init/lx-office-task-server.conf</code>.
+ <code class="filename">scripts/boot/upstart/kivitendo-task-server.conf</code>
+ nach <code class="filename">/etc/init/kivitendo-task-server.conf</code>.
Passen Sie in der kopierten Datei den Pfad zum Task-Server an (Zeile
<code class="literal">exec ....</code>).</p><p>Danach kann der Task-Server mit dem folgenden Befehl gestartet
- werden: <span class="command"><strong>service lx-office-task-server
+ werden: <span class="command"><strong>service kivitendo-task-server
start</strong></span>
</p></div></div><div class="sect2" title="2.6.3. Wie der Task-Server gestartet und beendet wird"><div class="titlepage"><div><div><h3 class="title"><a name="Prozesskontrolle"></a>2.6.3. Wie der Task-Server gestartet und beendet wird</h3></div></div></div><p>Der Task-Server wird wie folgt kontrolliert:</p><pre class="programlisting">./scripts/task_server.pl Befehl</pre><p>
<code class="literal">Befehl</code> ist dabei eine der folgenden
--- /dev/null
+#!/bin/sh
+
+### BEGIN INIT INFO
+# Provides: kivitendo-task-server
+# Required-Start: $local_fs $remote_fs $network $syslog $named
+# Required-Stop: $local_fs $remote_fs $network $syslog $named
+# Default-Start: 2 3 4 5
+# Default-Stop: 0 1 6
+# X-Interactive: true
+# Short-Description: Start/stop the Kivitendo task server
+### END INIT INFO
+
+set -e
+
+# Change this to point to the Kivitendo "task_server.pl" location.
+DAEMON="/opt/kivitendo/scripts/task_server.pl"
+TOPIC="Kivitendo task server"
+
+if [ ! -x $DAEMON ] ; then
+ echo "$TOPIC executable not found"
+ exit 1
+fi
+
+case $1 in
+ start)
+ echo "Starting the $TOPIC"
+ $DAEMON start
+ ;;
+ stop)
+ echo "Stopping the $TOPIC"
+ $DAEMON stop
+ ;;
+ restart)
+ echo "Restarting the $TOPIC"
+ $DAEMON restart
+ ;;
+ status)
+ if $DAEMON status > /dev/null 2> /dev/null; then
+ echo "The $TOPIC is running"
+ else
+ echo "The $TOPIC is not running"
+ exit 1
+ fi
+ ;;
+ *)
+ echo "Usage: /etc/init.d/kivitendo-task-server {start|stop|reload|status}"
+ exit 1
+ ;;
+esac
+++ /dev/null
-#!/bin/sh
-
-### BEGIN INIT INFO
-# Provides: lx-office-task-server
-# Required-Start: $local_fs $remote_fs $network $syslog $named
-# Required-Stop: $local_fs $remote_fs $network $syslog $named
-# Default-Start: 2 3 4 5
-# Default-Stop: 0 1 6
-# X-Interactive: true
-# Short-Description: Start/stop the Lx-Office task server
-### END INIT INFO
-
-set -e
-
-# Change this to point to the Lx-Office "task_server.pl" location.
-DAEMON="/opt/lx-office/scripts/task_server.pl"
-TOPIC="Lx-Office task server"
-
-if [ ! -x $DAEMON ] ; then
- echo "$TOPIC executable not found"
- exit 1
-fi
-
-case $1 in
- start)
- echo "Starting the $TOPIC"
- $DAEMON start
- ;;
- stop)
- echo "Stopping the $TOPIC"
- $DAEMON stop
- ;;
- restart)
- echo "Restarting the $TOPIC"
- $DAEMON restart
- ;;
- status)
- if $DAEMON status > /dev/null 2> /dev/null; then
- echo "The $TOPIC is running"
- else
- echo "The $TOPIC is not running"
- exit 1
- fi
- ;;
- *)
- echo "Usage: /etc/init.d/lx-office-task-server {start|stop|reload|status}"
- exit 1
- ;;
-esac
--- /dev/null
+# kivitendo-task-server - Task server for Kivitendo
+
+description "Kivitendo task server"
+
+start on runlevel [2345]
+stop on runlevel [!2345]
+
+respawn
+
+# Adjust path
+exec /opt/kivitendo/scripts/task_server.pl -f start
+++ /dev/null
-# lx-office-task-server - Task server for Lx-Office
-
-description "Lx-Office task server"
-
-start on runlevel [2345]
-stop on runlevel [!2345]
-
-respawn
-
-# Adjust path
-exec /opt/lx-office/scripts/task_server.pl -f start
-
}
my $seconds = 60 - (localtime)[0];
- sleep($seconds < 30 ? $seconds + 60 : $seconds);
+ if (!eval {
+ local $SIG{'ALRM'} = sub {
+ $::lxdebug->message(0, "Got woken up by SIGALRM") if $lx_office_conf{task_server}->{debug};
+ die "Alarm!\n"
+ };
+ sleep($seconds < 30 ? $seconds + 60 : $seconds);
+ 1;
+ }) {
+ die $@ unless $@ eq "Alarm!\n";
+ }
}
}
my $file = -f "${cwd}/config/lx_office.conf" ? "${cwd}/config/lx_office.conf" : "${cwd}/config/lx_office.conf.default";
newdaemon(configfile => $file,
- progname => 'lx-office-task-server',
+ progname => 'kivitendo-task-server',
pidbase => "${pidbase}/",
);