Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
NpgsqlRest/NpgsqlRestClient/WatchDbPoller.cs at master · NpgsqlRest/NpgsqlRest · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
NpgsqlRest
/
NpgsqlRest
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
11
Star
131
Code
Issues
0
Pull requests
4
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
NpgsqlRest
/
NpgsqlRestClient
/
WatchDbPoller.cs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
106 lines (101 loc) · 4 KB
master
Breadcrumbs
NpgsqlRest
/
NpgsqlRestClient
/
WatchDbPoller.cs
Copy path
Top
File metadata and controls
Code
Blame
106 lines (101 loc) · 4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using
System
.
Text
;
using
Microsoft
.
Extensions
.
Logging
;
using
Npgsql
;
using
NpgsqlRest
;
namespace
NpgsqlRestClient
;
/// <summary>
/// Database change detection for watch mode. There is no filesystem to watch for routine-source
/// endpoints — instead, the poller runs the SAME discovery query the routine source uses (same
/// configured filters — see <see cref="RoutineSource.CreateFingerprintCommand"/>), hashed server-side
/// into one scalar, on a dedicated non-pooled connection. If the hash changes, the discovered
/// endpoints changed — by definition: create/replace/drop/alter of functions and procedures, grants,
/// COMMENT ON (annotations), schema renames, and changes to the composite/table types their signatures
/// use. Anything the query does not read (an unrelated table, temp objects, data) can never trigger.
/// </summary>
public
sealed
class
WatchDbPoller
(
string
connectionString
,
TimeSpan
interval
,
Action
onChange
,
ILogger
?
logger
,
IReadOnlyList
<
RoutineSource
>
sources
)
{
private
readonly
string
_connString
=
new
NpgsqlConnectionStringBuilder
(
connectionString
)
{
Pooling
=
false
}
.
ConnectionString
;
private
NpgsqlConnection
?
_conn
;
private
string
?
_baseline
;
private
volatile
bool
_rebaseline
;
/// <summary>Fingerprint of one source's discovery result on an open connection (also used by tests).</summary>
public
static
string
?
GetFingerprint
(
NpgsqlConnection
connection
,
RoutineSource
source
)
{
using
var
cmd
=
source
.
CreateFingerprintCommand
(
connection
)
;
return
cmd
.
ExecuteScalar
(
)
as
string
;
}
/// <summary>
/// Forget the baseline so the next tick captures a fresh one WITHOUT firing. Called after work
/// that legitimately changes the database (a test rerun with committed fixtures, a restart) so
/// self-inflicted changes never trigger.
/// </summary>
public
void
Rebaseline
(
)
=>
_rebaseline
=
true
;
/// <summary>Poll loop; runs until cancelled. Connection failures skip the tick and retry.</summary>
public
async
Task
RunAsync
(
CancellationToken
ct
)
{
if
(
sources
.
Count
==
0
)
{
return
;
}
while
(
ct
.
IsCancellationRequested
is
false
)
{
try
{
await
Task
.
Delay
(
interval
,
ct
)
;
}
catch
(
OperationCanceledException
)
{
break
;
}
try
{
if
(
_conn
is
null
||
_conn
.
State
!=
System
.
Data
.
ConnectionState
.
Open
)
{
_conn
?
.
Dispose
(
)
;
_conn
=
new
NpgsqlConnection
(
_connString
)
;
await
_conn
.
OpenAsync
(
ct
)
;
}
var
sb
=
new
StringBuilder
(
)
;
foreach
(
var
source
in
sources
)
{
await
using
var
cmd
=
source
.
CreateFingerprintCommand
(
_conn
)
;
sb
.
Append
(
await
cmd
.
ExecuteScalarAsync
(
ct
)
as
string
)
.
Append
(
'|'
)
;
}
var
current
=
sb
.
ToString
(
)
;
if
(
_rebaseline
)
{
_rebaseline
=
false
;
_baseline
=
current
;
continue
;
}
if
(
_baseline
is
null
)
{
_baseline
=
current
;
continue
;
}
if
(
string
.
Equals
(
_baseline
,
current
,
StringComparison
.
Ordinal
)
is
false
)
{
_baseline
=
current
;
onChange
(
)
;
}
}
catch
(
OperationCanceledException
)
{
break
;
}
catch
(
Exception
ex
)
{
// DB down / restarting: skip this tick, reconnect on the next one.
logger
?
.
LogDebug
(
"watch: database poll failed ({Message}) — retrying"
,
ex
.
Message
)
;
try
{
_conn
?
.
Dispose
(
)
;
}
catch
{
}
_conn
=
null
;
}
}
try
{
_conn
?
.
Dispose
(
)
;
}
catch
{
}
}
}
You can’t perform that action at this time.