| 40 | |
| 41 | |
| 42 | def enqueue(db, project, row): |
| 43 | # Counter allocation and enqueue are one SQLite transaction: parallel hooks |
| 44 | # cannot assign the same run/sequence pair and silently discard an event. |
| 45 | with db: |
| 46 | db.execute('BEGIN IMMEDIATE') |
| 47 | db.execute('INSERT INTO counters VALUES(?,1) ON CONFLICT(run) DO UPDATE SET seq=seq+1', |
| 48 | (row['run'],)) |
| 49 | row['seq'] = db.execute('SELECT seq FROM counters WHERE run=?', (row['run'],)).fetchone()[0] |
| 50 | db.execute('INSERT INTO pending(project,payload) VALUES(?,?)', |
| 51 | (project, json.dumps(row, ensure_ascii=False))) |
| 52 | # A prolonged daemon outage cannot fill the disk without bound. Sequence |
| 53 | # gaps remain visible when older undelivered metadata expires. |
| 54 | db.execute('DELETE FROM pending WHERE id IN (SELECT id FROM pending ORDER BY id DESC LIMIT -1 OFFSET 10000)') |
| 55 | |
| 56 | |
| 57 | def flush(db): |