Commit e1e58370 authored by Martin Blix Grydeland's avatar Martin Blix Grydeland

Implement -t timeout option in VUT

parent 726b7926
......@@ -40,6 +40,8 @@ struct VUT {
int D_opt;
int g_arg;
int k_arg;
char *n_arg;
char *N_arg;
char *P_arg;
char *q_arg;
char *r_arg;
......
......@@ -136,7 +136,6 @@ VUT_Arg(int opt, const char *arg)
{
int i;
char *p;
double d;
switch (opt) {
case 'd':
......@@ -157,21 +156,12 @@ VUT_Arg(int opt, const char *arg)
VUT_Error(1, "-k: Invalid number '%s'", arg);
return (1);
case 'n':
/* Varnish instance */
if (VUT.vsm == NULL)
VUT.vsm = VSM_New();
AN(VUT.vsm);
if (VSM_n_Arg(VUT.vsm, arg) <= 0)
VUT_Error(1, "%s", VSM_Error(VUT.vsm));
/* Varnish instance name */
REPLACE(VUT.n_arg, arg);
return (1);
case 'N':
/* Varnish stale VSM file */
if (VUT.vsm == NULL)
VUT.vsm = VSM_New();
AN(VUT.vsm);
if (VSM_N_Arg(VUT.vsm, arg) <= 0)
VUT_Error(1, "%s", VSM_Error(VUT.vsm));
VUT.d_opt = 1; /* Enforces -d */
REPLACE(VUT.N_arg, arg);
return (1);
case 'P':
/* PID file */
......@@ -187,10 +177,15 @@ VUT_Arg(int opt, const char *arg)
return (1);
case 't':
/* VSM connect timeout */
d = VNUM(arg);
if (isnan(d))
VUT_Error(1, "-t: Syntax error");
VUT.t_arg = d;
if (!strcasecmp("off", arg))
VUT.t_arg = -1.;
else {
VUT.t_arg = VNUM(arg);
if (isnan(VUT.t_arg))
VUT_Error(1, "-t: Syntax error");
if (VUT.t_arg < 0.)
VUT_Error(1, "-t: Range error");
}
return (1);
case 'V':
/* Print version number and exit */
......@@ -222,36 +217,80 @@ void
VUT_Setup(void)
{
struct VSL_cursor *c;
double t_start;
int i;
AN(VUT.vsl);
if (VUT.r_arg && VUT.vsm)
VUT_Error(1, "Can't have both -n and -r options");
AZ(VUT.vsm);
AZ(VUT.vslq);
/* Check input arguments */
if ((VUT.n_arg == NULL ? 0 : 1) +
(VUT.N_arg == NULL ? 0 : 1) +
(VUT.r_arg == NULL ? 0 : 1) > 1)
VUT_Error(1, "Only one of -n, -N and -r options may be used");
/* Create query */
/* Create and validate the query expression */
VUT.vslq = VSLQ_New(VUT.vsl, NULL, VUT.g_arg, VUT.q_arg);
if (VUT.vslq == NULL)
VUT_Error(1, "Query expression error:\n%s", VSL_Error(VUT.vsl));
/* Input cursor */
/* Setup input */
if (VUT.r_arg) {
REPLACE(VUT.name, VUT.r_arg);
c = VSL_CursorFile(VUT.vsl, VUT.r_arg, 0);
if (c == NULL)
VUT_Error(1, "Can't open log file (%s)",
VSL_Error(VUT.vsl));
} else {
if (VUT.vsm == NULL)
/* Default uses VSM with n=hostname */
VUT.vsm = VSM_New();
VUT.vsm = VSM_New();
AN(VUT.vsm);
if (VSM_Open(VUT.vsm))
VUT_Error(1, "Can't open VSM file (%s)",
VSM_Error(VUT.vsm));
if (VUT.n_arg && VSM_n_Arg(VUT.vsm, VUT.n_arg) <= 0)
VUT_Error(1, "%s", VSM_Error(VUT.vsm));
if (VUT.N_arg && VSM_N_Arg(VUT.vsm, VUT.N_arg) <= 0)
VUT_Error(1, "%s", VSM_Error(VUT.vsm));
REPLACE(VUT.name, VSM_Name(VUT.vsm));
c = VSL_CursorVSM(VUT.vsl, VUT.vsm,
(VUT.d_opt ? VSL_COPT_TAILSTOP : VSL_COPT_TAIL)
| VSL_COPT_BATCH);
t_start = NAN;
c = NULL;
while (1) {
i = VSM_Open(VUT.vsm);
if (!i)
c = VSL_CursorVSM(VUT.vsl, VUT.vsm,
(VUT.d_opt ? VSL_COPT_TAILSTOP :
VSL_COPT_TAIL)
| VSL_COPT_BATCH);
if (c)
break;
if (isnan(t_start) && VUT.t_arg > 0.) {
VUT_Error(0, "Can't open log -"
" retrying for %.0f seconds", VUT.t_arg);
t_start = VTIM_real();
}
VSM_Close(VUT.vsm);
if (VUT.t_arg <= 0.)
break;
if (VTIM_real() - t_start > VUT.t_arg)
break;
VSM_ResetError(VUT.vsm);
VSL_ResetError(VUT.vsl);
VTIM_sleep(0.5);
}
if (VUT.t_arg >= 0. && (i || !c)) {
if (i)
VUT_Error(1, "Can't open VSM file (%s)",
VSM_Error(VUT.vsm));
else
VUT_Error(1, "Can't open log (%s)",
VSL_Error(VUT.vsl));
} else if (!isnan(t_start))
VUT_Error(0, "Log opened");
}
if (c == NULL)
VUT_Error(1, "Can't open log (%s)", VSL_Error(VUT.vsl));
VSLQ_SetCursor(VUT.vslq, &c);
if (c)
VSLQ_SetCursor(VUT.vslq, &c);
AZ(c);
/* Signal handlers */
......@@ -282,6 +321,8 @@ VUT_Setup(void)
void
VUT_Fini(void)
{
free(VUT.n_arg);
free(VUT.N_arg);
free(VUT.r_arg);
free(VUT.P_arg);
free(VUT.name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment