-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
458 lines (412 loc) · 11.5 KB
/
session.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package main
import (
"database/sql"
"fmt"
"log"
"strconv"
"sync"
"time"
)
const DBACTION_INSERT = 1
const DBACTION_UPDATE = 2
const DBACTION_DELETE = 3
type session struct {
db *sql.DB
selectSiteStmt *sql.Stmt
selectFeedStmt *sql.Stmt
insertProductStmt *sql.Stmt
selectCategoryStmt *sql.Stmt
insertCategoryStmt *sql.Stmt
selectFeedProductsStmt *sql.Stmt
selectFeedNetworkStmt *sql.Stmt
deleteProductStmt *sql.Stmt
selectCategoryProductStmt *sql.Stmt
selectCategoryProductsByCategoryIDStmt *sql.Stmt
selectCategoryProductByProductIDAndCategoryIDStmt *sql.Stmt
selectCategoryProductByCategoryProductIDStmt *sql.Stmt
selectCategoryCountByProductIDStmt *sql.Stmt
insertCategoryProductStmt *sql.Stmt
searchCategoryProductsStmt *sql.Stmt
deleteCategoryProductStmt *sql.Stmt
site *site
feeds []*feed
categories []categoryinterface
DBOperation chan message
FeedDone chan feedmessage
FeedError chan feedmessage
CategoryDone chan categorymessage
}
func (s *session) init(subdomain string) error {
var err error
// This does not really open a new connection.
var DSN = fmt.Sprintf("%v:%v@tcp(%v:%v)/%v", *dbUser, *dbPassword, *dbAddr, *dbPort, *database)
s.db, err = sql.Open("mysql", DSN)
if err != nil {
log.Println("Error on initializing database connection: %s",
err.Error())
}
s.db.SetMaxOpenConns(5)
// This DOES open a connection if necessary.
// This makes sure the database is accessible.
err = s.db.Ping()
if err != nil {
log.Println("Error on opening database connection: %s",
err.Error())
} else {
s.prepareSelectSiteStmt()
s.prepareSelectFeedsStmt()
s.prepareSelectCategoryStmt()
s.prepareSearchCategoryProductsStmt()
s.prepareSelectFeedProductsStmt()
s.prepareSelectFeedNetworkStmt()
s.prepareSelectCategoryProductStmt()
s.prepareSelectCategoryCountByProductIDStmt()
s.prepareSelectCategoryProductByProductIDAndCategoryIDStmt()
s.prepareSelectCategoryProductsByCategoryIDStmt()
s.prepareSelectCategoryProductByCategoryProductIDStmt()
}
s.selectSite(subdomain)
return err
}
func (s *session) prepareSelectSiteStmt() {
var err error
s.selectSiteStmt, err = s.db.Prepare(
"SELECT id, name, subdomain FROM sites WHERE subdomain = ?")
if err != nil {
log.Println(err)
}
}
func (s *session) prepareSelectFeedsStmt() {
var err error
s.selectFeedStmt, err = s.db.Prepare(
"SELECT f.id, f.site_id, f.name, f.url, f.network_id, " +
"f.allow_empty_description " +
"FROM feeds as f " +
"WHERE f.site_id = ?")
if err != nil {
log.Println(err)
}
}
func (s *session) prepareSelectCategoryStmt() {
var err error
s.selectCategoryStmt, err = s.db.Prepare("SELECT id, name, slug, " +
"search, description FROM categories " +
"WHERE site_id = ?")
if err != nil {
log.Println(err)
}
}
func (s *session) prepareSelectCategoryCountByProductIDStmt() {
var err error
s.selectCategoryCountByProductIDStmt, err = s.db.Prepare("SELECT COUNT(*) " +
"FROM category_product " +
"WHERE product_id = ?")
if err != nil {
log.Println(err)
}
}
func (s *session) prepareSearchCategoryProductsStmt() {
var err error
s.searchCategoryProductsStmt, err = s.db.Prepare("SELECT * FROM products " +
"WHERE site_id = ? " +
"AND MATCH(`name`,`description`) " +
"AGAINST (? IN BOOLEAN MODE)")
if err != nil {
log.Println(err)
}
}
func (s *session) prepareSelectFeedProductsStmt() {
var err error
s.selectFeedProductsStmt, err = s.db.Prepare(
"SELECT id, site_id, feed_id, name, name_by_user, identifier, price, " +
"regular_price, description, description_by_user, " +
"currency, url, graphic_url, shipping_price, in_stock, " +
"points, has_categories, active, deleted_at " +
"FROM products WHERE feed_id = ?")
if err != nil {
log.Println(err)
}
}
func (s *session) prepareSelectFeedNetworkStmt() {
var err error
s.selectFeedNetworkStmt, err = s.db.Prepare(
"SELECT id, name FROM networks WHERE id = ? LIMIT 1")
if err != nil {
log.Println(err)
}
}
func (s *session) prepareSelectCategoryProductsByCategoryIDStmt() {
var err error
s.selectCategoryProductsByCategoryIDStmt, err = s.db.Prepare(
"SELECT cp.id, p.site_id, p.feed_id, p.name, p.name_by_user, p.identifier, p.price, " +
"p.regular_price, p.description, p.description_by_user, " +
"p.currency, p.url, p.graphic_url, p.shipping_price, p.in_stock, " +
"p.points, p.has_categories, p.active, p.deleted_at, " +
"cp.category_id, cp.product_id, cp.forced " +
"FROM products p " +
"INNER JOIN category_product cp " +
"ON p.id = cp.product_id " +
"WHERE cp.category_id = ?")
if err != nil {
log.Println(err)
}
}
func (s *session) prepareSelectCategoryProductByCategoryProductIDStmt() {
var err error
s.selectCategoryProductByCategoryProductIDStmt, err = s.db.Prepare(
"SELECT cp.id, p.site_id, p.feed_id, p.name, p.name_by_user, p.identifier, p.price, " +
"p.regular_price, p.description, p.description_by_user, " +
"p.currency, p.url, p.graphic_url, p.shipping_price, p.in_stock, " +
"p.points, p.has_categories, p.active, " +
"cp.category_id, cp.product_id, cp.forced " +
"FROM products p " +
"INNER JOIN category_product cp " +
"ON p.id = cp.product_id " +
"WHERE cp.id = ?")
if err != nil {
log.Println(err)
}
}
func (s *session) prepareSelectCategoryProductStmt() {
var err error
s.selectCategoryProductStmt, err = s.db.Prepare(
"SELECT cp.id, c.name, c.search, c.description, " +
"cp.category_id, cp.forced " +
"FROM categories c INNER JOIN category_product AS cp " +
"ON c.`id` = cp.`category_id` " +
"WHERE cp.`product_id` = ?")
if err != nil {
log.Println(err)
}
}
func (s *session) prepareSelectCategoryProductByProductIDAndCategoryIDStmt() {
var err error
s.selectCategoryProductByProductIDAndCategoryIDStmt, err = s.db.Prepare(
"SELECT cp.id, p.site_id, p.feed_id, p.name, p.name_by_user, p.identifier, p.price, " +
"p.regular_price, p.description, p.description_by_user, " +
"p.currency, p.url, p.graphic_url, p.shipping_price, p.in_stock, " +
"p.points, p.has_categories, p.active, " +
"cp.category_id, cp.product_id, cp.forced " +
"FROM products p " +
"INNER JOIN category_product cp " +
"ON p.id = cp.product_id " +
"WHERE p.id = ? AND cp.category_id = ?")
if err != nil {
log.Println(err)
}
}
func (s *session) selectFeeds() error {
s.feeds = []*feed{}
rows, err := s.selectFeedStmt.Query(s.site.ID)
if err != nil {
log.Println(err)
}
defer rows.Close()
for rows.Next() {
f := &feed{}
err = rows.Scan(
&f.ID,
&f.SiteID,
&f.Name,
&f.URL,
&f.NetworkID,
&f.AllowEmptyDescription,
)
if err != nil {
log.Println(err)
} else {
s.feeds = append(s.feeds, f)
}
}
if err := rows.Err(); err != nil {
log.Println(err)
}
return err
}
func (s *session) prepare() {
s.FeedDone = make(chan feedmessage, len(s.feeds))
s.FeedError = make(chan feedmessage, len(s.feeds))
s.DBOperation = make(chan message)
// Run 5 worker instances for db actions
for i := 0; i < 5; i++ {
go s.worker()
}
}
func (s *session) waitForResult() {
for i := 1; i < len(s.feeds)+1; i++ {
select {
case m := <-s.FeedDone:
log.Println(m.feed.Name + " " + m.action + " completed.")
case m := <-s.FeedError:
log.Println("Errors in "+m.feed.Name+" "+m.action, m.err)
<-SessionQueue
}
log.Println("WaitForResult: " + strconv.Itoa(i) + "/" + strconv.Itoa(len(s.feeds)))
if i == len(s.feeds) {
s.syncProductCategories()
s.waitForRefreshResult()
}
}
}
func (s *session) waitForRefreshResult() {
for i := 1; i < len(s.categories)+1; i++ {
select {
case m := <-s.CategoryDone:
log.Println(m.category.Name + " completed.")
}
log.Println("WaitForRefreshResult: " + strconv.Itoa(i) + "/" + strconv.Itoa(len(s.categories)))
if i == len(s.categories) {
log.Println("Session done: " + s.site.Name)
<-SessionQueue
}
}
}
func (s *session) syncProductCategories() {
var err error
s.categories, err = s.selectCategories()
s.CategoryDone = make(chan categorymessage, len(s.categories))
if err != nil {
log.Print(err)
}
for _, c := range s.categories {
log.Println("Syncing category " + c.getName())
err = c.syncProducts(s)
if err != nil {
log.Print(err)
}
}
}
func (s *session) update() {
defer s.db.Close()
for _, f := range s.feeds {
var err error
f.Network, err = f.selectNetwork(s)
if err != nil {
log.Println(err)
} else {
go f.update(s)
}
}
s.waitForResult()
}
func (s *session) refresh() {
defer s.db.Close()
s.syncProductCategories()
s.waitForRefreshResult()
}
func (s *session) worker() {
var wg sync.WaitGroup
for {
var err error
select {
case message := <-s.DBOperation:
switch message.product.getDBAction() {
case DBACTION_INSERT:
wg.Add(1)
go func() {
defer wg.Done()
err = message.product.insert(s)
if err != nil {
log.Println(err)
message.feed.DBOperationError <- err
} else {
message.feed.DBOperationDone <- fmt.Sprintf(
"Inserted %s: '%s'.",
message.product.getEntityType(),
message.product.getName())
}
}()
case DBACTION_UPDATE:
wg.Add(1)
go func() {
defer wg.Done()
err = message.product.update(s)
if err != nil {
log.Println(err)
message.feed.DBOperationError <- err
} else {
message.feed.DBOperationDone <- fmt.Sprintf(
"Updated %s: '%s'.",
message.product.getEntityType(),
message.product.getName())
}
}()
case DBACTION_DELETE:
wg.Add(1)
go func() {
defer wg.Done()
err = message.product.delete(s)
if err != nil {
log.Println(err)
message.feed.DBOperationError <- err
} else {
message.feed.DBOperationDone <- fmt.Sprintf(
"Deleted %s: '%s'.",
message.product.getEntityType(),
message.product.getName())
}
}()
default:
time.Sleep(1 * time.Millisecond)
}
default:
time.Sleep(1 * time.Millisecond)
}
wg.Wait()
}
}
func (s *session) selectSite(subdomain string) (site, error) {
var si site
rows, err := s.selectSiteStmt.Query(subdomain)
if err != nil {
log.Println(err)
return si, err
}
defer rows.Close()
for rows.Next() {
si = site{}
err := rows.Scan(&si.ID, &si.Name, &si.Subdomain)
if err != nil {
log.Println(err)
}
}
err = rows.Err()
if err != nil {
log.Println(err)
}
s.site = &si
return si, err
}
func (s *session) selectCategories() ([]categoryinterface, error) {
if len(s.categories) > 0 {
return s.categories, nil
}
categories := []categoryinterface{}
rows, err := s.selectCategoryStmt.Query(s.site.ID)
if err != nil {
log.Println(err)
return categories, err
}
defer rows.Close()
for rows.Next() {
c := category{}
err := rows.Scan(
&c.ID,
&c.Name,
&c.Slug,
&c.Search,
&c.Description,
)
if err != nil {
log.Println(err)
} else {
categories = append(categories, &c)
}
}
err = rows.Err()
if err != nil {
log.Println(err)
}
s.categories = categories
return categories, err
}