fix(scheduler): implement deadline-based matching with 5-run limit and fix security issues

Security fixes:
- Replace $queryRawUnsafe with parameterized $queryRaw in admin.js to prevent SQL injection
- Use PostgreSQL ANY() operator for safe array parameter handling

Scheduler improvements:
- Add registrationDeadline support - scheduler now waits until deadline before running
- Implement 5-run limit after deadline (runs exactly 5 times with 5-minute intervals)
- Add countScheduledRunsAfterDeadline() to track post-deadline runs
- Add environment variable validation with sensible min/max ranges
- Fix Prisma query syntax (remove invalid endDate null check for non-nullable field)

UI improvements:
- Fix colspan mismatch in MatchingRunsSection (6 → 8 columns)
- Remove duplicate "Uruchom Matching" button, keep only "Run now" with audit tracking
- Simplify MatchingConfigSection to focus on deadline configuration

Logging enhancements:
- Add detailed scheduler logs showing run progress (e.g., "Running post-deadline matching (3/5)")
- Log wait times before deadline and between runs
- Show completion status after 5 runs
This commit is contained in:
Radosław Gierwiało
2025-11-30 14:42:08 +01:00
parent 752d65035a
commit 560ff1edc1
4 changed files with 184 additions and 106 deletions

View File

@@ -4,7 +4,7 @@ import { matchingAPI } from '../../services/api';
/**
* Auto-matching configuration section
* Allows setting registration deadline and running matching algorithm
* Allows setting registration deadline
*/
const MatchingConfigSection = ({ slug, event, onRefresh }) => {
const [deadlineInput, setDeadlineInput] = useState(() => {
@@ -15,7 +15,6 @@ const MatchingConfigSection = ({ slug, event, onRefresh }) => {
return '';
});
const [savingDeadline, setSavingDeadline] = useState(false);
const [runningMatching, setRunningMatching] = useState(false);
const handleSaveDeadline = async () => {
try {
@@ -31,20 +30,6 @@ const MatchingConfigSection = ({ slug, event, onRefresh }) => {
}
};
const handleRunMatching = async () => {
try {
setRunningMatching(true);
const result = await matchingAPI.runMatching(slug);
alert(`Matching zakonczony! Dopasowano: ${result.matched}, Nie znaleziono: ${result.notFound}`);
onRefresh?.();
} catch (err) {
console.error('Failed to run matching:', err);
alert('Nie udalo sie uruchomic matchingu');
} finally {
setRunningMatching(false);
}
};
return (
<div className="bg-white rounded-lg shadow-md p-6">
<h2 className="text-xl font-semibold mb-4 flex items-center gap-2">
@@ -52,79 +37,45 @@ const MatchingConfigSection = ({ slug, event, onRefresh }) => {
Auto-Matching (Nagrywanie)
</h2>
<div className="grid md:grid-cols-2 gap-6">
{/* Registration Deadline */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
<Clock size={16} className="inline mr-1" />
Deadline rejestracji
</label>
<p className="text-xs text-gray-500 mb-2">
Matching uruchomi sie 30 min po tym terminie
</p>
<div className="flex gap-2">
<input
type="datetime-local"
value={deadlineInput}
onChange={(e) => setDeadlineInput(e.target.value)}
className="flex-1 px-3 py-2 border border-gray-300 rounded-md text-sm"
/>
<button
onClick={handleSaveDeadline}
disabled={savingDeadline}
className="px-4 py-2 bg-primary-600 text-white rounded-md hover:bg-primary-700 disabled:opacity-50 flex items-center gap-2"
>
{savingDeadline ? (
<RefreshCw size={16} className="animate-spin" />
) : (
<Save size={16} />
)}
Zapisz
</button>
</div>
{event?.registrationDeadline && (
<p className="text-sm text-green-600 mt-2">
Aktualny deadline: {new Date(event.registrationDeadline).toLocaleString('pl-PL')}
</p>
)}
</div>
{/* Matching Status & Run */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
Status matchingu
</label>
{event?.matchingRunAt ? (
<div className="bg-green-50 border border-green-200 rounded-lg p-3 mb-3">
<p className="text-green-800 text-sm">
Ostatnie uruchomienie: {new Date(event.matchingRunAt).toLocaleString('pl-PL')}
</p>
</div>
) : (
<div className="bg-amber-50 border border-amber-200 rounded-lg p-3 mb-3">
<p className="text-amber-800 text-sm">
Matching nie byl jeszcze uruchomiony
</p>
</div>
)}
{/* Registration Deadline */}
<div>
<label className="block text-sm font-medium text-gray-700 mb-2">
<Clock size={16} className="inline mr-1" />
Deadline rejestracji
</label>
<p className="text-xs text-gray-500 mb-2">
Scheduler automatycznie uruchomi matching po tym terminie
</p>
<div className="flex gap-2">
<input
type="datetime-local"
value={deadlineInput}
onChange={(e) => setDeadlineInput(e.target.value)}
className="flex-1 px-3 py-2 border border-gray-300 rounded-md text-sm"
/>
<button
onClick={handleRunMatching}
disabled={runningMatching}
className="w-full px-4 py-2 bg-green-600 text-white rounded-md hover:bg-green-700 disabled:opacity-50 flex items-center justify-center gap-2"
onClick={handleSaveDeadline}
disabled={savingDeadline}
className="px-4 py-2 bg-primary-600 text-white rounded-md hover:bg-primary-700 disabled:opacity-50 flex items-center gap-2"
>
{runningMatching ? (
<>
<RefreshCw size={16} className="animate-spin" />
Trwa matching...
</>
{savingDeadline ? (
<RefreshCw size={16} className="animate-spin" />
) : (
<>
<Video size={16} />
Uruchom Matching
</>
<Save size={16} />
)}
Zapisz
</button>
</div>
{event?.registrationDeadline && (
<p className="text-sm text-green-600 mt-2">
Aktualny deadline: {new Date(event.registrationDeadline).toLocaleString('pl-PL')}
</p>
)}
{event?.matchingRunAt && (
<p className="text-sm text-gray-600 mt-2">
Ostatnie uruchomienie: {new Date(event.matchingRunAt).toLocaleString('pl-PL')}
</p>
)}
</div>
</div>
);

View File

@@ -109,11 +109,11 @@ export default function MatchingRunsSection({ slug }) {
<tbody className="bg-white divide-y divide-gray-200">
{loading ? (
<tr>
<td colSpan="6" className="px-3 py-3 text-center text-gray-500">Loading...</td>
<td colSpan="8" className="px-3 py-3 text-center text-gray-500">Loading...</td>
</tr>
) : runs.length === 0 ? (
<tr>
<td colSpan="6" className="px-3 py-3 text-center text-gray-500">No runs yet</td>
<td colSpan="8" className="px-3 py-3 text-center text-gray-500">No runs yet</td>
</tr>
) : (
runs.map((run) => (