jjzjj

go - 通过 http 启动/停止和处理自定义调度程序

coder 2024-07-10 原文

我想创建一个调度程序,例如它每秒执行一个任务,但也希望有一个 http 接口(interface)来停止/启动调度程序并获取更多统计信息/信息,阅读更多关于 timers & tickers 的信息后, channelsgorutines我想出了这个:

https://gist.github.com/nbari/483c5b382c795bf290b5

package main

import (
    "fmt"
    "log"
    "net/http"
    "time"
)

var timer *time.Ticker

func scheduler(seconds time.Duration) *time.Ticker {
    ticker := time.NewTicker(seconds * time.Second)
    go func() {
        for t := range ticker.C {
            // do stuff
            fmt.Println(t)
        }
    }()
    return ticker
}

func Start(timer *time.Ticker) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        timer = scheduler(1)
        w.Write([]byte("Starting scheduler"))
    })
}

func Stop(timer *time.Ticker) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        timer.Stop()
        w.Write([]byte("Stoping scheduler"))
    })
}

func main() {
    timer = scheduler(1)
    http.Handle("/start", Start(timer))
    http.Handle("/stop", Stop(timer))
    log.Fatal(http.ListenAndServe(":8080", nil))
}

上面的代码有效,但我有一个全局“定时器”变量,我想知道是否有更好的方法来实现它以及处理多个调度程序的方法,目前正在考虑可能实现的一种所有调度程序的容器,但希望有一些反馈可以帮助我找到聪明的解决方案。

最佳答案

是的,有更好的方法:

您应该在此处使用 channel ,并且如您所建议的那样,使用一些数据结构来容纳更多调度程序。

我想到了这个,这是我要做的最基本的工作示例:

package main

import (
    "errors"
    "fmt"
    "sync"
    "time"
)

// a scheduler runs f on every receive on t and exits when receiving from quit is non-blocking
type scheduler struct {
    t    <-chan time.Time
    quit chan struct{}
    f    func()
}

// a schedulerPool holds multiple schedulers
type schedulerPool struct {
    schedulers map[int]scheduler //I used a map here so you can use more clever keys
    counter    int
    mut        sync.Mutex
}

func newPool() *schedulerPool {
    return &schedulerPool{
        schedulers: make(map[int]scheduler),
    }
}

// start adds and starts a new scheduler that will execute f every interval.
// It returns the generated ID for the scheduler, or an error.
func (p *schedulerPool) start(interval time.Duration, f func()) (int, error) {
    p.mut.Lock()
    defer p.mut.Unlock()
    index := p.counter
    p.counter++

    if _, ok := p.schedulers[index]; ok {
        return 0, errors.New("key already in use")
    }

    sched := scheduler{
        t:    time.NewTicker(interval).C,
        quit: make(chan struct{}),
        f:    f,
    }

    p.schedulers[index] = sched
    go func() {
        for {
            select {
            case <-sched.t:
                sched.f()
            case <-sched.quit:
                return
            }
        }
    }()
    return index, nil
}

// stop stops the scheduler with the given ID.
func (p *schedulerPool) stop(index int) error {
    p.mut.Lock()
    defer p.mut.Unlock()

    sched, ok := p.schedulers[index]
    if !ok {
        return errors.New("does not exist")
    }

    close(sched.quit)
    return nil
}

func main() {
    // get a new pool
    pool := newPool()

    // start a scheduler
    idx1, err := pool.start(time.Second, func() { fmt.Println("hello 1") })
    if err != nil {
        panic(err)
    }

    // start another scheduler
    idx2, err := pool.start(time.Second, func() { fmt.Println("hello 2") })
    if err != nil {
        panic(err)
    }

    // wait some time
    time.Sleep(3 * time.Second)

    // stop the second scheduler
    err = pool.stop(idx2)
    if err != nil {
        panic(err)
    }

    // wait some more
    time.Sleep(3 * time.Second)

    // stop the first scheduler
    err = pool.stop(idx1)
    if err != nil {
        panic(err)
    }
}

playground 上查看.

请注意,不能保证第一个调度程序总共运行六次,而第二个调度程序总共运行三次。

另请注意我所做的其他一些事情:

  • 必须同步 map 访问
  • 生成的 ID 必须同步
  • 您应该检查 key 是否确实在 map 中
  • 使用 channel 启动、停止和控制调度器(例如,添加 channel beep 让调度器将某些内容转储到控制台(如 “beep”))
  • 为要执行的函数的确切签名定义一个类型,例如 type schedFunc func(int, int)

显然,您需要将这些东西包装在 http 处理程序中,可能从查询字符串中获取 ID 或参数,或者其他一些奇特的东西。这只是为了演示。

关于go - 通过 http 启动/停止和处理自定义调度程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33413105/

有关go - 通过 http 启动/停止和处理自定义调度程序的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  3. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  4. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  5. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  6. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  7. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  8. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  9. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  10. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

随机推荐