Commit edfb6893 authored by Geoff Simmons's avatar Geoff Simmons

Add the .check() method.

parent fa595a34
...@@ -377,6 +377,34 @@ Example:: ...@@ -377,6 +377,34 @@ Example::
.. _reader.error(): .. _reader.error():
.. _xreader.check():
VOID xreader.check()
--------------------
Run a synchronous check of the file, and update the cached contents if
the file has been changed. This is the same check that runs
periodically in the background after the reader object is created.
Since the file check does not scale well, this method should be used
judiciously as a part of request processing. A reasonable use case is
an administrative request with restricted access, to be called as a
part of an automated process when the underlying file is changed, so
that the changes are reflected promptly.
Example::
if (req.url == "/update-files") {
# Assume that this ACL defines internal admin networks.
# Return error status 403 Forbidden if the client IP doesn't
# match.
if (client.ip !~ admin_network) {
return (synth(403));
}
# Internal admins may run a synchronous file check.
rdr.check();
}
.. _xreader.error(): .. _xreader.error():
BOOL xreader.error() BOOL xreader.error()
......
...@@ -33,7 +33,8 @@ TESTS = \ ...@@ -33,7 +33,8 @@ TESTS = \
vtc/info.vtc \ vtc/info.vtc \
vtc/path.vtc \ vtc/path.vtc \
vtc/synth.vtc \ vtc/synth.vtc \
vtc/temperature.vtc vtc/temperature.vtc \
vtc/check.vtc
# Documentation # Documentation
......
...@@ -650,6 +650,18 @@ vmod_reader_blob(VRT_CTX, struct VPFX(file_reader) *rdr) ...@@ -650,6 +650,18 @@ vmod_reader_blob(VRT_CTX, struct VPFX(file_reader) *rdr)
return (blob); return (blob);
} }
VCL_VOID
vmod_reader_check(VRT_CTX, struct VPFX(file_reader) *rdr)
{
union sigval val;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(rdr, FILE_READER_MAGIC);
val.sival_ptr = rdr;
check(val);
}
VCL_BOOL VCL_BOOL
vmod_reader_error(VRT_CTX, struct VPFX(file_reader) *rdr) vmod_reader_error(VRT_CTX, struct VPFX(file_reader) *rdr)
{ {
......
...@@ -354,6 +354,31 @@ Example:: ...@@ -354,6 +354,31 @@ Example::
.. _reader.error(): .. _reader.error():
$Method VOID .check()
Run a synchronous check of the file, and update the cached contents if
the file has been changed. This is the same check that runs
periodically in the background after the reader object is created.
Since the file check does not scale well, this method should be used
judiciously as a part of request processing. A reasonable use case is
an administrative request with restricted access, to be called as a
part of an automated process when the underlying file is changed, so
that the changes are reflected promptly.
Example::
if (req.url == "/update-files") {
# Assume that this ACL defines internal admin networks.
# Return error status 403 Forbidden if the client IP doesn't
# match.
if (client.ip !~ admin_network) {
return (synth(403));
}
# Internal admins may run a synchronous file check.
rdr.check();
}
$Method BOOL .error() $Method BOOL .error()
Return true if and only if an error condition was determined the last Return true if and only if an error condition was determined the last
......
# looks like -*- vcl -*-
varnishtest ".check() method"
shell {echo -n "foo bar baz quux" > ${tmpdir}/file}
varnish v1 -vcl {
import file;
backend b None;
sub vcl_init {
# Set a long TTL so that the background check
# doesn't run during the test.
new rdr = file.reader("${tmpdir}/file", ttl=1y);
}
sub vcl_recv {
if (req.url == "/check") {
rdr.check();
}
return (synth(200));
}
sub vcl_synth {
set resp.http.Get = rdr.get();
return (deliver);
}
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.Get == "foo bar baz quux"
} -run
shell {echo -n "quux baz bar foo" > ${tmpdir}/file}
client c1 {
txreq -url "/check"
rxresp
expect resp.status == 200
expect resp.http.Get == "quux baz bar foo"
} -run
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