oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package conversation
2
3import (
4 "amadan.net/oficina/equipo/internal/equipo/kernel"
5 "amadan.net/oficina/equipo/internal/equipo/team"
6 "context"
7 "database/sql"
8 "time"
9)
10
11func JobDeadlines(ctx context.Context, tx *sql.Tx, deadlines map[string][]time.Time, teamID string, ids []team.JobID) error {
12 if ids == nil {
13 rows, err := tx.QueryContext(ctx, "SELECT id FROM jobs WHERE account=? AND state NOT IN ('succeeded','failed','cancelled')", teamID)
14 if err != nil {
15 return err
16 }
17 for rows.Next() {
18 var id team.JobID
19 if err = rows.Scan(&id); err != nil {
20 break
21 }
22 ids = append(ids, id)
23 }
24 if err == nil {
25 err = rows.Err()
26 }
27 rows.Close()
28 if err != nil {
29 return err
30 }
31 }
32 collection := kernel.JobTopic(teamID, 0)
33 deadlines[collection] = append(deadlines[collection], kernel.WeekStart(time.Now()).AddDate(0, 0, 7))
34 for _, schedule := range []struct {
35 query string
36 shift time.Duration
37 }{
38 {"SELECT t.job_id,q.expires_at FROM questions q JOIN tasks t ON t.id=q.task_id WHERE q.state='pending' AND t.team_id=?", 0},
39 {"SELECT 0,c.last_seen FROM connections c JOIN runners r ON r.id=c.runner_id WHERE r.instance_id=?", 90 * time.Second},
40 {"SELECT job_id,lease_deadline FROM runner_assignments WHERE state='claimed' AND team_id=?", 0},
41 {"SELECT a.job_id,a.lease_deadline FROM attempts a JOIN jobs j ON j.id=a.job_id WHERE a.containment='pending' AND j.account=?", 0},
42 } {
43 rows, err := tx.QueryContext(ctx, schedule.query, teamID)
44 if err != nil {
45 return err
46 }
47 for rows.Next() {
48 var id team.JobID
49 var raw any
50 if err = rows.Scan(&id, &raw); err != nil {
51 break
52 }
53 at := kernel.ReadTime(raw).Add(schedule.shift)
54 deadlines[collection] = append(deadlines[collection], at)
55 if id != 0 {
56 deadlines[kernel.JobTopic(teamID, int64(id))] = append(deadlines[kernel.JobTopic(teamID, int64(id))], at)
57 } else {
58 for _, job := range ids {
59 if job != 0 {
60 topic := kernel.JobTopic(teamID, int64(job))
61 deadlines[topic] = append(deadlines[topic], at)
62 }
63 }
64 }
65 }
66 if err == nil {
67 err = rows.Err()
68 }
69 rows.Close()
70 if err != nil {
71 return err
72 }
73 }
74 return nil
75}
76