]> git.neil.brown.name Git - TABS.git/blob - manager/check_default.c
Terminal Alocation and Booking System - June 2006
[TABS.git] / manager / check_default.c
1
2 #include        <time.h>
3 #include        "helper.h"
4
5
6 /*
7  * check if anyone appears to have defaulted on a booking
8  * just now.
9  * for each workstation
10  *   if    it is allocated/tentative
11  *     and it's allocation time is before now, but after last check
12  *     and user allocated is not on
13  *   then
14  *     record default for allocated user in this lab for this period
15  *
16  */
17
18 void check_default(table *t)
19 {
20     static time_t last_check = 0;
21     time_t now = time(0);
22     int lab;
23     int grace = get_configint("grace");
24     time_t alloctime;
25     int ind;
26
27     if (grace==0) grace=7*60;
28     
29     if (last_check == 0)
30     {
31         last_check = now;
32         return;
33     }
34
35     for (ind=0 ; ind < t->table_len ; ind++)
36     {
37         if (Cluster_type(t,ind)== ALLOCCLUSTER)
38         {
39             lab = Cluster_id(t,ind);
40             alloctime = Cluster_modtime(t,ind);
41         }
42         if (Cluster_type(t,ind) == WSRESCLUSTER
43             && (Res_status(t,ind) == NODEALLOCATED
44                 ||Res_status(t,ind) == NODETENTATIVE)
45             && Res_user(t,ind) < get_class_min()
46             && alloctime +grace <= now
47             && alloctime +grace > last_check
48             && Cluster_type(t,ind-1) == WSSTATUSCLUSTER
49             && Cluster_id(t,ind-1) == Cluster_id(t,ind)
50 /*          && Ws_user(t,ind-1) != Res_user(t,ind)   */
51             )
52         {
53             /* ok, a rescluster which might be for us.
54              * must check the ws belong to us, and that
55              * who-on is correct
56              */
57             workstation_state *ws = get_wsstate(Cluster_id(t,ind),0);
58             if (ws
59                 && (ws->host == my.hostid
60                     || (ws->host <0 && Ws_whereon(t,ind-1)==my.hostid)
61                     || (ws->host <0 && Ws_whereon(t,ind-1)<0 && am_master)
62                     )
63                 )
64             {
65                 int doy, pod;
66                 book_set_claim *cp;
67                 book_change ch;
68                 time_t whenon;
69                 ch.chng = CLAIM_BOOKING;
70                 cp = & ch.book_change_u.aclaim;
71
72                 get_doypod(&alloctime, &doy, &pod);
73                 cp->slot = make_bookkey(doy, pod, lab);
74                 cp->user = Res_user(t,ind);
75                 cp->when = 0;
76                 cp->where = -1;
77                 if (check_whoison(Cluster_id(t,ind),  &whenon) ==
78                     Res_user(t,ind))
79                 {
80                     int delta_whenon = whenon - alloctime;
81                     if (delta_whenon < -10*60)
82                         delta_whenon = grace;
83                     cp->claimtime = DID_LOGIN | ((delta_whenon+30*60) <<4);
84                     cp->where = Cluster_id(t,ind);
85                 }
86                 else if (Cluster_modtime(t,ind) > alloctime + 30)
87                 {
88                     /* looks like they claimed, but haven't logged on yet, give them the benefit of the doubt */
89                     cp->claimtime = 0;
90                 }
91                 else
92                 {
93                     /* Ok, looks like a defaulter to me!! */
94                     cp->claimtime = DID_DEFAULT;
95                 }
96                 {
97                     char buf[100];
98                     sprintf(buf, "check_default set %x for user %d on %d (d%d,p%d,l%d)\n",
99                             cp->claimtime, (int)cp->user, cp->where, doy, pod, lab);
100                     shout(buf);
101                 }
102                 send_change(&ch);
103             }
104         }
105     }
106     last_check = now;
107 }