golang开发轻量级定时任务,goticker的实战案例,干货满满,【强烈建议收藏】

序言

采集数据要用到定时任务,处理汇总数据也要用到定时任务。要无人值守自动化处理任务也需要定时任务。定时任务在各行各业都应用广泛。

golang代码如下:

package main

import (
   "fmt"
   "github.com/aWildProgrammer/goticker"
   "os"
   "os/exec"
   "time"
)

func main() {

   task := goticker.New(100, false)
   ch2 := make(chan bool, 0)
   ch02 := make(chan bool, 0)
   ch03 := make(chan bool, 0)
   ch04 := make(chan bool, 0)
   ch05 := make(chan bool, 0)
   ch06 := make(chan bool, 0)
   //每隔1800秒执行
   //task1 := task.AddTaskCallBackFunc(taskone, 300, "proxy-h5循环采集")
   task1 := task.AddTaskCallBackFunc(taskone, 1, "proxy-h5循环采集")
   task2 := task.AddTaskCallBackChannel(ch2, 3000)
   task02 := task.AddCycleTaskCallBackChannel(ch02, "02:18:55")
   task03 := task.AddCycleTaskCallBackChannel(ch03, "03:18:55")
   task04 := task.AddCycleTaskCallBackChannel(ch04, "04:18:55")
   task05 := task.AddCycleTaskCallBackChannel(ch05, "05:18:55")
   task06 := task.AddCycleTaskCallBackChannel(ch06, "06:18:55")
   fmt.Println(task1, task2, task02, task03, task04, task05, task06)
   for {
      select {
      case <-ch2:
         fmt.Println("每间隔3000秒任务task2")
         rebootRouterNow()
         exeonlyH5()
      case <-ch02:
         fmt.Println("每天 02:18:55任务task02")
         rebootRouterNow()
         exeonlyH5()
      case <-ch03:
         fmt.Println("每天 03:18:55任务task03")
         rebootRouterNow()
         exeonlyH5()
      case <-ch04:
         fmt.Println("每天 04:18:55任务task04")
         rebootRouterNow()
         exeonlyH5()
      case <-ch05:
         fmt.Println("每天 05:18:55任务task05")
         rebootRouterNow()
         exeonlyH5()
      case <-ch06:
         fmt.Println("每天 06:18:55任务task06")
         rebootRouterNow()
         exeonlyH5()

      }

   }
}
func rebootRouterNow() {
   cmdLine1 := "selenium_chrome/onlyH5Root.exe"

   if PathExists(cmdLine1) {
      fmt.Println(cmdLine1)
      cmd1 := exec.Command("cmd.exe", "/c", "start "+cmdLine1)
      err1 := cmd1.Run()
      fmt.Printf("%s, error:%v \n", cmdLine1, err1)
      time.Sleep(2 * time.Second)
   }

}
func exeonlyH5() {

   pythonFile := "ProxyH5NoLimit"

   cmdLine1 := "fetch_log/douyinproxy.exe"
   fmt.Println(cmdLine1)
   cmd1 := exec.Command("cmd.exe", "/c", "start "+cmdLine1)
   err1 := cmd1.Run()
   fmt.Printf("%s, error:%v \n", cmdLine1, err1)

   cmdLine := "python ./" + pythonFile + ".py"
   fmt.Println(cmdLine)
   cmd := exec.Command("cmd.exe", "/c", "start "+cmdLine)
   err := cmd.Run()
   fmt.Printf("%s, error:%v \n", cmdLine, err)
   time.Sleep(time.Duration(2) * time.Second)
}

func taskone(args interface{}) {
   fmt.Println(args)
   fmt.Println("存在selenium_chrome/onlyH5Root.exe,就会采集前重启路由器")
}
func test(args interface{}) {
   fmt.Println(args)
}

func PathExists(path string) bool {

   _, err := os.Stat(path)

   if err == nil {

      return true

   }

   if os.IsNotExist(err) {

      return false

   }

   return false

}

总结

开发各种可视化计划任务,开发各种采集模型,让数据服务于全人类,欢迎私聊。

你可能感兴趣的:(golang,开发语言,后端)