-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
921 lines (814 loc) · 29.7 KB
/
main.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
// Copyright 2023 Paolo Fabio Zaino
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package main (TheCROWler) is the application.
// It's responsible for starting the crawler and kickstart the configuration
// reading and the database connection.
// Actual crawling is performed by the pkg/crawler package.
// The database connection is handled by the pkg/database package.
// The configuration is handled by the pkg/config package.
// Page info extraction is handled by the pkg/scrapper package.
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
cmn "github.com/pzaino/thecrowler/pkg/common"
cfg "github.com/pzaino/thecrowler/pkg/config"
crowler "github.com/pzaino/thecrowler/pkg/crawler"
cdb "github.com/pzaino/thecrowler/pkg/database"
rules "github.com/pzaino/thecrowler/pkg/ruleset"
"golang.org/x/time/rate"
_ "github.com/lib/pq"
)
const (
sleepTime = 30 * time.Second // Time to sleep when no URLs are found
)
var (
limiter *rate.Limiter // Rate limiter
configFile *string // Configuration file path
config cfg.Config // Configuration "object"
configMutex sync.Mutex // Mutex to protect the configuration
// GRulesEngine Global rules engine
GRulesEngine rules.RuleEngine // GRulesEngine Global rules engine
// Prometheus metrics
totalPages = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "crowler_total_pages",
Help: "Total number of pages crawled.",
},
[]string{"pipeline_id", "source"},
)
totalLinks = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "crowler_total_links",
Help: "Total number of links collected.",
},
[]string{"pipeline_id", "source"},
)
totalErrors = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "crowler_total_errors",
Help: "Total number of errors encountered.",
},
[]string{"pipeline_id", "source"},
)
// TODO: Define more prometheus metrics here...
)
// WorkBlock is a struct that holds all the necessary information to instantiate a new
// crawling job on the pipeline. It's used to pass the information to the goroutines
// that will perform the actual crawling.
type WorkBlock struct {
db cdb.Handler
sel *chan crowler.SeleniumInstance
sources *[]cdb.Source
RulesEngine *rules.RuleEngine
PipelineStatus *[]crowler.Status
Config *cfg.Config
}
// HealthCheck is a struct that holds the health status of the application.
type HealthCheck struct {
Status string `json:"status"`
}
// This function is responsible for performing database maintenance
// to keep it lean and fast. Note: it's specific for PostgreSQL.
func performDBMaintenance(db cdb.Handler) error {
if db.DBMS() == cdb.DBSQLiteStr {
return nil
}
// Define the maintenance commands
var maintenanceCommands []string
if db.DBMS() == cdb.DBPostgresStr {
maintenanceCommands = []string{
"VACUUM Keywords",
"VACUUM MetaTags",
"VACUUM WebObjects",
"VACUUM SearchIndex",
"VACUUM KeywordIndex",
"VACUUM MetaTagsIndex",
"VACUUM WebObjectsIndex",
"REINDEX TABLE WebObjects",
"REINDEX TABLE SearchIndex",
"REINDEX TABLE KeywordIndex",
"REINDEX TABLE WebObjectsIndex",
"REINDEX TABLE MetaTagsIndex",
"REINDEX TABLE NetInfoIndex",
"REINDEX TABLE HTTPInfoIndex",
"REINDEX TABLE SourceInformationSeedIndex",
"REINDEX TABLE SourceOwnerIndex",
"REINDEX TABLE SourceSearchIndex",
}
}
for _, cmd := range maintenanceCommands {
_, err := db.Exec(cmd)
if err != nil {
return fmt.Errorf("error executing maintenance command (%s): %w", cmd, err)
}
}
return nil
}
// This function simply query the database for URLs that need to be crawled
func retrieveAvailableSources(db cdb.Handler) ([]cdb.Source, error) {
// Check DB connection:
if err := db.CheckConnection(config); err != nil {
return nil, fmt.Errorf("error pinging the database: %w", err)
}
// Start a transaction
tx, err := db.Begin()
if err != nil {
return nil, err
}
// Update the SQL query to fetch all necessary fields
query := `
SELECT
l.source_id,
l.url,
l.restricted,
l.flags,
l.config
FROM
update_sources($1,$2,$3,$4,$5,$6) AS l
ORDER BY l.last_updated_at ASC;`
// Execute the query within the transaction
// TODO: Add the intervals to the query to allow a user to decide how often to crawl a source etc.
// replace the empty strings here with: last_ok_update, last_error, regular_crawling, processing_timeout
rows, err := tx.Query(query, config.Crawler.MaxSources, cmn.GetEngineID(), config.Crawler.CrawlingIfOk, config.Crawler.CrawlingIfError, config.Crawler.CrawlingInterval, config.Crawler.ProcessingTimeout)
if err != nil {
err2 := tx.Rollback()
if err2 != nil {
cmn.DebugMsg(cmn.DbgLvlError, "rolling back transaction: %v", err2)
}
return nil, err
}
// Iterate over the results and store them in a slice
var sourcesToCrawl []cdb.Source
for rows.Next() {
var src cdb.Source
if err := rows.Scan(&src.ID, &src.URL, &src.Restricted, &src.Flags, &src.Config); err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "scanning rows: %v", err)
err2 := rows.Close()
if err2 != nil {
cmn.DebugMsg(cmn.DbgLvlError, "closing rows iterator: %v", err2)
}
err2 = tx.Rollback()
if err2 != nil {
cmn.DebugMsg(cmn.DbgLvlError, "rolling back transaction: %v", err2)
}
return nil, err
}
// Check if Config is nil and assign a default configuration if so
if src.Config == nil {
src.Config = new(json.RawMessage)
*src.Config = cdb.DefaultSourceCfgJSON
}
// Append the source to the slice
sourcesToCrawl = append(sourcesToCrawl, src)
src = cdb.Source{} // Reset the source
}
err = rows.Close() // Close the rows iterator
if err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "closing rows iterator: %v", err)
}
// Commit the transaction if everything is successful
if err := tx.Commit(); err != nil {
return nil, err
}
return sourcesToCrawl, nil
}
// This function is responsible for checking the database for URLs that need to be crawled
// and kickstart the crawling process for each of them
func checkSources(db *cdb.Handler, sel *chan crowler.SeleniumInstance, RulesEngine *rules.RuleEngine) {
cmn.DebugMsg(cmn.DbgLvlInfo, "Checking sources...")
// Initialize the pipeline status
PipelineStatus := make([]crowler.Status, config.Crawler.MaxSources)
// Set the maintenance time
maintenanceTime := time.Now().Add(time.Duration(config.Crawler.Maintenance) * time.Minute)
// Set the resource release time
resourceReleaseTime := time.Now().Add(time.Duration(5) * time.Minute)
// Start the main loop
defer configMutex.Unlock()
for {
configMutex.Lock()
// Retrieve the sources to crawl
sourcesToCrawl, err := retrieveAvailableSources(*db)
if err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "retrieving sources: %v", err)
// We are about to go to sleep, so we can handle signals for reloading the configuration
configMutex.Unlock()
time.Sleep(sleepTime)
continue
}
cmn.DebugMsg(cmn.DbgLvlDebug2, "Sources to crawl: %d", len(sourcesToCrawl))
// Check if there are sources to crawl
if len(sourcesToCrawl) == 0 {
cmn.DebugMsg(cmn.DbgLvlDebug, "No sources to crawl, sleeping...")
// Perform database maintenance if it's time
if time.Now().After(maintenanceTime) {
performDatabaseMaintenance(*db)
maintenanceTime = time.Now().Add(time.Duration(config.Crawler.Maintenance) * time.Minute)
cmn.DebugMsg(cmn.DbgLvlDebug2, "Database maintenance every: %d", config.Crawler.Maintenance)
}
// We are about to go to sleep, so we can handle signals for reloading the configuration
configMutex.Unlock()
if time.Now().After(resourceReleaseTime) {
// Release unneeded resources:
runtime.GC() // Run the garbage collector
debug.FreeOSMemory() // Force release of unused memory to the OS
resourceReleaseTime = time.Now().Add(time.Duration(5) * time.Minute)
}
time.Sleep(sleepTime)
continue
}
// Crawl each source
workBlock := WorkBlock{
db: *db,
sel: sel,
sources: &sourcesToCrawl,
RulesEngine: RulesEngine,
PipelineStatus: &PipelineStatus,
Config: &config,
}
crawlSources(&workBlock)
// We have completed all jobs, so we can handle signals for reloading the configuration
configMutex.Unlock()
sourcesToCrawl = []cdb.Source{} // Reset the sources
}
}
func performDatabaseMaintenance(db cdb.Handler) {
cmn.DebugMsg(cmn.DbgLvlInfo, "Performing database maintenance...")
if err := performDBMaintenance(db); err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "performing database maintenance: %v", err)
} else {
cmn.DebugMsg(cmn.DbgLvlInfo, "Database maintenance completed successfully.")
}
}
func crawlSources(wb *WorkBlock) {
// Start a goroutine to log the status periodically
go func(plStatus *[]crowler.Status) {
ticker := time.NewTicker(time.Duration(wb.Config.Crawler.ReportInterval) * time.Minute)
defer ticker.Stop()
for range ticker.C {
// Check if all the pipelines have completed
pipelinesRunning := false
for _, status := range *plStatus {
if status.PipelineRunning == 1 {
pipelinesRunning = true
break
}
}
logStatus(plStatus)
if !pipelinesRunning {
// All pipelines have completed
// Stop the ticker
break
}
}
}(wb.PipelineStatus)
// Start the crawling process for each source
var wg sync.WaitGroup // WaitGroup to wait for all goroutines to finish
selIdx := 0 // Selenium instance index
sourceIdx := 0 // Source index
var maxSrc uint64 = uint64(wb.Config.Crawler.MaxSources) //nolint:gosec // DIsable G115 (integer overflow, given the MaxSources value is fully tested)
for idx := uint64(0); idx < maxSrc; idx++ {
// Check if the pipeline is already running
if (*wb.PipelineStatus)[idx].PipelineRunning == 1 {
continue
}
// Get the source to crawl
source := (*wb.sources)[sourceIdx]
wg.Add(1)
// Initialize the status
(*wb.PipelineStatus)[idx] = crowler.Status{
PipelineID: idx,
Source: source.URL,
SourceID: source.ID,
PipelineRunning: 0,
CrawlingRunning: 0,
NetInfoRunning: 0,
HTTPInfoRunning: 0,
TotalPages: 0,
TotalErrors: 0,
TotalLinks: 0,
TotalSkipped: 0,
TotalDuplicates: 0,
TotalScraped: 0,
TotalActions: 0,
LastWait: 0,
LastDelay: 0,
}
// Start a goroutine to crawl the website
startCrawling(wb, &wg, selIdx, source, idx)
// Increment the Source index to get the next source
sourceIdx++
if sourceIdx >= len(*wb.sources) {
break // We have reached the end of the sources
}
}
wg.Wait() // Block until all goroutines have decremented the counter
}
func startCrawling(wb *WorkBlock, wg *sync.WaitGroup, selIdx int, source cdb.Source, idx uint64) {
// Prepare the go routine parameters
args := crowler.Pars{
WG: wg,
DB: wb.db,
Src: source,
Sel: wb.sel,
SelIdx: selIdx,
RE: wb.RulesEngine,
Sources: wb.sources,
Index: idx,
Status: &((*wb.PipelineStatus)[idx]), // Pointer to a single status element
}
// Start a goroutine to crawl the website
go func(args crowler.Pars) {
//defer wg.Done()
// Acquire a Selenium instance
seleniumInstance := <-*args.Sel
// Channel to release the Selenium instance
releaseSelenium := make(chan crowler.SeleniumInstance)
// Start crawling the website synchronously
go crowler.CrawlWebsite(args, seleniumInstance, releaseSelenium)
// Release the Selenium instance when done
*args.Sel <- <-releaseSelenium
}(args)
}
func logStatus(PipelineStatus *[]crowler.Status) {
// Log the status of the pipelines
const (
sepRLine = "====================================="
sepPLine = "-------------------------------------"
)
report := "Pipelines status report\n"
report += sepRLine + "\n"
runningPipelines := 0
for idx := 0; idx < len(*PipelineStatus); idx++ {
status := (*PipelineStatus)[idx]
if status.PipelineRunning == 0 {
continue
}
runningPipelines++
var totalRunningTime time.Duration
if status.EndTime.IsZero() {
totalRunningTime = time.Since(status.StartTime)
} else {
totalRunningTime = status.EndTime.Sub(status.StartTime)
}
totalLinksToGo := status.TotalLinks - (status.TotalPages + status.TotalSkipped + status.TotalDuplicates)
if totalLinksToGo < 0 {
totalLinksToGo = 0
}
report += fmt.Sprintf(" Pipeline: %d\n", status.PipelineID)
report += fmt.Sprintf(" Source: %s\n", status.Source)
report += fmt.Sprintf(" Pipeline status: %s\n", StatusStr(status.PipelineRunning))
report += fmt.Sprintf(" Crawling status: %s\n", StatusStr(status.CrawlingRunning))
report += fmt.Sprintf(" NetInfo status: %s\n", StatusStr(status.NetInfoRunning))
report += fmt.Sprintf(" HTTPInfo status: %s\n", StatusStr(status.HTTPInfoRunning))
report += fmt.Sprintf(" Running Time: %s\n", totalRunningTime)
report += fmt.Sprintf(" Total Crawled Pages: %d\n", status.TotalPages)
report += fmt.Sprintf(" Total Errors: %d\n", status.TotalErrors)
report += fmt.Sprintf(" Total Collected Links: %d\n", status.TotalLinks)
report += fmt.Sprintf(" Total Skipped Links: %d\n", status.TotalSkipped)
report += fmt.Sprintf(" Total Duplicated Links: %d\n", status.TotalDuplicates)
report += fmt.Sprintf("Total Links to complete: %d\n", totalLinksToGo)
report += fmt.Sprintf(" Total Scrapes: %d\n", status.TotalScraped)
report += fmt.Sprintf(" Total Actions: %d\n", status.TotalActions)
report += fmt.Sprintf(" Last Page Wait: %f\n", status.LastWait)
report += fmt.Sprintf(" Last Page Delay: %f\n", status.LastDelay)
report += sepPLine + "\n"
// Update the metrics
updateMetrics(status)
// Reset the status if the pipeline has completed (display only the last report)
if status.PipelineRunning == 2 || status.PipelineRunning == 3 {
status.PipelineRunning = 0
}
}
report += sepRLine + "\n"
if runningPipelines > 0 {
cmn.DebugMsg(cmn.DbgLvlInfo, report)
}
}
func updateMetrics(status crowler.Status) {
if !config.Prometheus.Enabled {
return
}
// Update the metrics
labels := prometheus.Labels{
"pipeline_id": fmt.Sprintf("%d", status.PipelineID),
"source": status.Source,
}
totalPages.With(labels).Set(float64(status.TotalPages))
totalLinks.With(labels).Set(float64(status.TotalLinks))
totalErrors.With(labels).Set(float64(status.TotalErrors))
// Push metrics
if err := push.New("http://"+config.Prometheus.Host+":"+strconv.Itoa(config.Prometheus.Port), "crowler_engine").
Collector(totalPages).
// Add other collectors...
Grouping("pipeline_id", fmt.Sprintf("%d", status.PipelineID)).
Push(); err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "Could not push metrics: %v", err)
}
// Delete metrics if pipeline is complete
if status.PipelineRunning == 2 || status.PipelineRunning == 3 {
// Use the configured pushgateway URL
if err := push.New("http://"+config.Prometheus.Host+":"+strconv.Itoa(config.Prometheus.Port), "crowler_engine").
Grouping("pipeline_id", fmt.Sprintf("%d", status.PipelineID)).
Delete(); err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "Could not delete metrics: %v", err)
}
}
}
// StatusStr returns a string representation of the status
func StatusStr(condition int) string {
switch condition {
case 0:
return "Not started yet"
case 1:
return "Running"
case 2:
return "Completed"
case 3:
return "Completed with errors"
default:
return "Unknown"
}
}
func initAll(configFile *string, config *cfg.Config,
db *cdb.Handler, seleniumInstances *chan crowler.SeleniumInstance,
RulesEngine *rules.RuleEngine, lmt **rate.Limiter) error {
var err error
// Reload the configuration file
*config, err = cfg.LoadConfig(*configFile)
if err != nil {
return fmt.Errorf("loading configuration file: %s", err)
}
// Reset Key-Value Store
cmn.KVStore = nil
cmn.KVStore = cmn.NewKeyValueStore()
// Reconnect to the database
*db, err = cdb.NewHandler(*config)
if err != nil {
return fmt.Errorf("creating database handler: %s", err)
}
// Set the rate limiter
var rl, bl int
if strings.TrimSpace(config.Crawler.Control.RateLimit) == "" {
config.Crawler.Control.RateLimit = "10,10"
}
if !strings.Contains(config.Crawler.Control.RateLimit, ",") {
config.Crawler.Control.RateLimit = config.API.RateLimit + ",10"
}
rlStr := strings.Split(config.Crawler.Control.RateLimit, ",")[0]
if rlStr == "" {
rlStr = "10"
}
rl, err = strconv.Atoi(rlStr)
if err != nil {
rl = 10
}
blStr := strings.Split(config.Crawler.Control.RateLimit, ",")[1]
if blStr == "" {
blStr = "10"
}
bl, err = strconv.Atoi(blStr)
if err != nil {
bl = 10
}
*lmt = rate.NewLimiter(rate.Limit(rl), bl)
// Reinitialize the Selenium services
*seleniumInstances = make(chan crowler.SeleniumInstance, len(config.Selenium))
for _, seleniumConfig := range config.Selenium {
selService, err := crowler.NewSeleniumService(seleniumConfig)
if err != nil {
return fmt.Errorf("creating Selenium Instances: %s", err)
}
*seleniumInstances <- crowler.SeleniumInstance{
Service: selService,
Config: seleniumConfig,
}
}
// Initialize the rules engine
*RulesEngine = rules.NewEmptyRuleEngine(config.RulesetsSchemaPath)
err = RulesEngine.LoadRulesFromConfig(config)
if err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "loading rules from configuration: %v", err)
}
cmn.DebugMsg(cmn.DbgLvlInfo, "Rulesets loaded: %d", RulesEngine.CountRulesets())
cmn.DebugMsg(cmn.DbgLvlInfo, "Detection rules loaded: %d", RulesEngine.CountDetectionRules())
cmn.DebugMsg(cmn.DbgLvlInfo, " Detection Noise Threshold: %f", RulesEngine.DetectionConfig.NoiseThreshold)
cmn.DebugMsg(cmn.DbgLvlInfo, " Detection Maybe Threshold: %f", RulesEngine.DetectionConfig.MaybeThreshold)
cmn.DebugMsg(cmn.DbgLvlInfo, " Detection Detected Threshold: %f", RulesEngine.DetectionConfig.DetectedThreshold)
cmn.DebugMsg(cmn.DbgLvlInfo, "Action rules loaded: %d", RulesEngine.CountActionRules())
cmn.DebugMsg(cmn.DbgLvlInfo, "Scraping rules loaded: %d", RulesEngine.CountScrapingRules())
cmn.DebugMsg(cmn.DbgLvlInfo, "Crawling rules loaded: %d", RulesEngine.CountCrawlingRules())
cmn.DebugMsg(cmn.DbgLvlInfo, "Plugins loaded: %d", RulesEngine.CountPlugins())
// Initialize the prometheus metrics
if config.Prometheus.Enabled {
prometheus.MustRegister(totalPages)
prometheus.MustRegister(totalLinks)
prometheus.MustRegister(totalErrors)
}
// Start the crawler
crowler.StartCrawler(*config)
return nil
}
func main() {
// Reading command line arguments
configFile = flag.String("config", "./config.yaml", "Path to the configuration file")
flag.Parse()
// Initialize the logger
cmn.InitLogger("TheCROWler")
cmn.DebugMsg(cmn.DbgLvlInfo, "The CROWler is starting...")
// Define db before we set signal handlers
var db cdb.Handler
// Define sel before we set signal handlers
seleniumInstances := make(chan crowler.SeleniumInstance)
// Setting up a channel to listen for termination signals
cmn.DebugMsg(cmn.DbgLvlInfo, "Setting up termination signals listener...")
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
// Use a select statement to block until a signal is received
go func() {
for {
sig := <-signals
switch sig {
case syscall.SIGINT:
// Handle SIGINT (Ctrl+C)
cmn.DebugMsg(cmn.DbgLvlInfo, "SIGINT received, shutting down...")
closeResources(db, seleniumInstances) // Release resources
os.Exit(0)
case syscall.SIGTERM:
// Handle SIGTERM
cmn.DebugMsg(cmn.DbgLvlInfo, "SIGTERM received, shutting down...")
closeResources(db, seleniumInstances) // Release resources
os.Exit(0)
case syscall.SIGQUIT:
// Handle SIGQUIT
cmn.DebugMsg(cmn.DbgLvlInfo, "SIGQUIT received, shutting down...")
closeResources(db, seleniumInstances) // Release resources
os.Exit(0)
case syscall.SIGHUP:
// Handle SIGHUP
cmn.DebugMsg(cmn.DbgLvlInfo, "SIGHUP received, will reload configuration as soon as all pending jobs are completed...")
configMutex.Lock()
err := initAll(configFile, &config, &db, &seleniumInstances, &GRulesEngine, &limiter)
if err != nil {
configMutex.Unlock()
cmn.DebugMsg(cmn.DbgLvlFatal, "initializing the crawler: %v", err)
}
// Connect to the database
err = db.Connect(config)
if err != nil {
configMutex.Unlock()
closeResources(db, seleniumInstances) // Release resources
cmn.DebugMsg(cmn.DbgLvlFatal, "connecting to the database: %v", err)
}
cmn.DebugMsg(cmn.DbgLvlInfo, "Database connection re-established.")
configMutex.Unlock()
cmn.DebugMsg(cmn.DbgLvlInfo, "Configuration reloaded.")
//go checkSources(&db, seleniumInstances)
}
}
}()
// Initialize the crawler
err := initAll(configFile, &config, &db, &seleniumInstances, &GRulesEngine, &limiter)
if err != nil {
cmn.DebugMsg(cmn.DbgLvlFatal, "initializing the crawler: %v", err)
}
// Connect to the database
err = db.Connect(config)
if err != nil {
closeResources(db, seleniumInstances) // Release resources
cmn.DebugMsg(cmn.DbgLvlFatal, "connecting to the database: %v", err)
}
cmn.DebugMsg(cmn.DbgLvlInfo, "Database connection established.")
defer closeResources(db, seleniumInstances)
// Start events listener
go cdb.ListenForEvents(&db, handleNotification)
// Start the checkSources function in a goroutine
cmn.DebugMsg(cmn.DbgLvlInfo, "Starting processing data (if any)...")
go checkSources(&db, &seleniumInstances, &GRulesEngine)
// Start the internal/control API server
srv := &http.Server{
Addr: config.Crawler.Control.Host + ":" + fmt.Sprintf("%d", config.Crawler.Control.Port),
// ReadHeaderTimeout is the amount of time allowed to read
// request headers. The connection's read deadline is reset
// after reading the headers and the Handler can decide what
// is considered too slow for the body. If ReadHeaderTimeout
// is zero, the value of ReadTimeout is used. If both are
// zero, there is no timeout.
ReadHeaderTimeout: time.Duration(config.Crawler.Control.ReadHeaderTimeout) * time.Second,
// ReadTimeout is the maximum duration for reading the entire
// request, including the body. A zero or negative value means
// there will be no timeout.
//
// Because ReadTimeout does not let Handlers make per-request
// decisions on each request body's acceptable deadline or
// upload rate, most users will prefer to use
// ReadHeaderTimeout. It is valid to use them both.
ReadTimeout: time.Duration(config.Crawler.Control.ReadTimeout) * time.Second,
// WriteTimeout is the maximum duration before timing out
// writes of the response. It is reset whenever a new
// request's header is read. Like ReadTimeout, it does not
// let Handlers make decisions on a per-request basis.
// A zero or negative value means there will be no timeout.
WriteTimeout: time.Duration(config.Crawler.Control.WriteTimeout) * time.Second,
// IdleTimeout is the maximum amount of time to wait for the
// next request when keep-alive are enabled. If IdleTimeout
// is zero, the value of ReadTimeout is used. If both are
// zero, there is no timeout.
IdleTimeout: time.Duration(config.Crawler.Control.Timeout) * time.Second,
}
// Set the handlers
initAPIv1()
cmn.DebugMsg(cmn.DbgLvlInfo, "Starting server on %s:%d", config.Crawler.Control.Host, config.Crawler.Control.Port)
var rStatus error
if strings.ToLower(strings.TrimSpace(config.Crawler.Control.SSLMode)) == cmn.EnableStr {
rStatus = srv.ListenAndServeTLS(config.API.CertFile, config.API.KeyFile)
} else {
rStatus = srv.ListenAndServe()
}
statusMsg := "Server stopped."
if rStatus != nil {
statusMsg = fmt.Sprintf("Server stopped with error: %v", rStatus)
}
cmn.DebugMsg(cmn.DbgLvlFatal, statusMsg)
}
func handleNotification(payload string) {
var event cdb.Event
err := json.Unmarshal([]byte(payload), &event)
if err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "Failed to decode notification payload: %v", err)
return
}
// Log the event for debug purposes
cmn.DebugMsg(cmn.DbgLvlDebug, "New Event Received: %+v", event)
// Process the Event
processEvent(event)
}
func processEvent(event cdb.Event) {
switch strings.ToLower(strings.TrimSpace(event.Type)) {
case "system_event":
// System event
processSystemEvent(event)
default:
// Ignore event
cmn.DebugMsg(cmn.DbgLvlDebug5, "Ignoring event, not interested in this type: %s", event.Type)
}
}
func processSystemEvent(event cdb.Event) {
// check if event.Details contains a tag "action" and process it
if event.Details["action"] == nil {
cmn.DebugMsg(cmn.DbgLvlDebug5, "Ignoring event, no action specified.")
return
}
action := event.Details["action"].(string)
switch strings.ToLower(strings.TrimSpace(action)) {
case "update_debug_level":
// Check if there is a tag "level" and process it
if event.Details["level"] == nil {
cmn.DebugMsg(cmn.DbgLvlDebug5, "Ignoring event, no level specified.")
return
}
// Update the debug level
newLevel := event.Details["level"].(string)
// Convert newLevel to a DebugLevel
newLevel = strings.ToLower(newLevel)
go updateDebugLevel(newLevel)
default:
// Ignore event
cmn.DebugMsg(cmn.DbgLvlDebug5, "Ignoring event, not interested in this action: %s", action)
}
}
func updateDebugLevel(newLevel string) {
// Get configuration lock
configMutex.Lock()
defer configMutex.Unlock()
var dbgLvl cmn.DbgLevel
switch newLevel {
case "debug":
config.DebugLevel = 1
case "debug1":
config.DebugLevel = 1
case "debug2":
config.DebugLevel = 2
case "debug3":
config.DebugLevel = 3
case "debug4":
config.DebugLevel = 4
case "debug5":
config.DebugLevel = 5
case "info":
config.DebugLevel = 0
default:
cmn.DebugMsg(cmn.DbgLvlDebug5, "Ignoring event, invalid debug level specified: %s", newLevel)
return
}
// Update the debug level
dbgLvl = cmn.DbgLevel(config.DebugLevel)
cmn.SetDebugLevel(dbgLvl)
cmn.DebugMsg(cmn.DbgLvlInfo, "Debug level updated to: %d", cmn.GetDebugLevel())
}
// initAPIv1 initializes the API v1 handlers
func initAPIv1() {
// Health check
healthCheckWithMiddlewares := SecurityHeadersMiddleware(RateLimitMiddleware(http.HandlerFunc(healthCheckHandler)))
http.Handle("/v1/health", healthCheckWithMiddlewares)
// Config Check
configCheckWithMiddlewares := SecurityHeadersMiddleware(RateLimitMiddleware(http.HandlerFunc(configCheckHandler)))
http.Handle("/v1/config", configCheckWithMiddlewares)
}
// RateLimitMiddleware is a middleware for rate limiting
func RateLimitMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !limiter.Allow() {
cmn.DebugMsg(cmn.DbgLvlDebug, "Rate limit exceeded")
http.Error(w, "Too Many Requests", http.StatusTooManyRequests)
return
}
next.ServeHTTP(w, r)
})
}
// SecurityHeadersMiddleware adds security-related headers to responses
func SecurityHeadersMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Add various security headers here
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("X-Frame-Options", "DENY")
w.Header().Set("Content-Security-Policy", "default-src 'self'")
next.ServeHTTP(w, r)
})
}
// handleErrorAndRespond encapsulates common error handling and JSON response logic.
func handleErrorAndRespond(w http.ResponseWriter, err error, results interface{}, errMsg string, errCode int, successCode int) {
if err != nil {
cmn.DebugMsg(cmn.DbgLvlDebug3, errMsg, err)
http.Error(w, err.Error(), errCode)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(successCode) // Explicitly set the success status code
if err := json.NewEncoder(w).Encode(results); err != nil {
// Log the error and send a generic error message to the client
cmn.DebugMsg(cmn.DbgLvlDebug3, "Error encoding JSON response: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}
func healthCheckHandler(w http.ResponseWriter, _ *http.Request) {
// Create a JSON document with the health status
healthStatus := HealthCheck{
Status: "OK",
}
// Respond with the health status
handleErrorAndRespond(w, nil, healthStatus, "Error in health Check: ", http.StatusInternalServerError, http.StatusOK)
}
func configCheckHandler(w http.ResponseWriter, _ *http.Request) {
// Make a copy of the configuration and remove the sensitive data
configCopy := config
configCopy.Database.Password = "********"
// Respond with the configuration
handleErrorAndRespond(w, nil, configCopy, "Error in configuration Check: ", http.StatusInternalServerError, http.StatusOK)
}
func closeResources(db cdb.Handler, sel chan crowler.SeleniumInstance) {
// Close the database connection
if db != nil {
err := db.Close()
if err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "closing database connection: %v", err)
} else {
cmn.DebugMsg(cmn.DbgLvlInfo, "Database connection closed.")
}
}
// Stop the Selenium services
close(sel)
for seleniumInstance := range sel {
if seleniumInstance.Service != nil {
err := seleniumInstance.Service.Stop()
if err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "stopping Selenium instance: %v", err)
}
}
}
cmn.DebugMsg(cmn.DbgLvlInfo, "All services stopped.")
}