Skip to content

Commit

Permalink
Allow bot exit before ig connection and scrapping process
Browse files Browse the repository at this point in the history
+ add information notification on bot stop or hot reload
  • Loading branch information
hbollon committed Mar 6, 2021
1 parent 80d7cff commit fa97d58
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
49 changes: 31 additions & 18 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ func LaunchBotTui() {
}
}

func checkBotChannels() bool {
select {
case <-BotStruct.hotReloadCallback:
if err := BotStruct.HotReload(); err != nil {
logrus.Errorf("Bot hot reload failed: %v", err)
BotStruct.hotReloadCallback <- false
} else {
logrus.Info("Bot hot reload successfully.")
BotStruct.hotReloadCallback <- true
}
break
case <-BotStruct.reloadCallback:
logrus.Info("Bot reload successfully.")
break
case <-BotStruct.exitCh:
logrus.Info("Bot process successfully stopped.")
return true
default:
break
}

return false
}

// Initialize client and bot configs, download dependencies,
// launch Selenium instance and finally run dm bot routine
func launchDmBot(ctx context.Context) {
Expand Down Expand Up @@ -159,35 +183,24 @@ func launchDmBot(ctx context.Context) {
go func() {
rand.Seed(time.Now().Unix())
if err = BotStruct.Scheduler.CheckTime(); err == nil {
if exit := checkBotChannels(); exit {
return
}
BotStruct.ConnectToInstagram()
for {
var users []string
if exit := checkBotChannels(); exit {
return
}
users, err = BotStruct.FetchUsersFromUserFollowers()
if err != nil {
BotStruct.crashCh <- fmt.Errorf("Failed users fetching: %v. Check logs tab for more details", err)
return
}
for _, username := range users {
select {
case <-BotStruct.hotReloadCallback:
if err = BotStruct.HotReload(); err != nil {
logrus.Errorf("Bot hot reload failed: %v", err)
BotStruct.hotReloadCallback <- false
} else {
logrus.Info("Bot hot reload successfully.")
BotStruct.hotReloadCallback <- true
}
break
case <-BotStruct.reloadCallback:
logrus.Info("Bot reload successfully.")
break
case <-BotStruct.exitCh:
logrus.Info("Bot process successfully stopped.")
if exit := checkBotChannels(); exit {
return
default:
break
}

var res bool
res, err = BotStruct.SendMessage(username, BotStruct.DmModule.DmTemplates[rand.Intn(len(BotStruct.DmModule.DmTemplates))])
if !res || err != nil {
Expand Down
2 changes: 2 additions & 0 deletions resources/static/app/assets/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ $(document).ready(function(){
});
} else {
$('#dmBotLaunchIcon').addClass('fa-spinner').addClass('fa-spin').removeClass('fa-skull-crossbones');
toastr.info("Stop procedure launched, the bot will stop once the current action is finished.")
astilectron.sendMessage({"msg":"stopDmBot"}, function(message) {
if(message.status === SUCCESS) {
toastr.success(message.msg);
Expand All @@ -87,6 +88,7 @@ $(document).ready(function(){

$("#dmBotHotReloadBtn").on("click", function(){
$('#dmBotHotReloadIcn').addClass('fa-spinner').addClass('fa-spin').removeClass('fa-fire');
toastr.info("Hot reload launched, the bot will update once the current action is finished.")
astilectron.sendMessage({"msg":"hotReloadBot"}, function(message) {
if(message.status === SUCCESS) {
toastr.success(message.msg);
Expand Down

0 comments on commit fa97d58

Please sign in to comment.