Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
tkdeng committed Dec 26, 2024
1 parent 1e447e9 commit c267faf
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 35 deletions.
126 changes: 126 additions & 0 deletions cron/cron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package cron

import (
"io"
"sync"
"time"

"github.com/tkdeng/goutil"
)

type cronJob struct {
interval int64
last *int64
cb func() bool
}

var cron map[string]cronJob = map[string]cronJob{}
var cronMU sync.Mutex

func init() {
go func() {
for {
time.Sleep(1 * time.Second)

now := time.Now().UnixMilli()

cronMU.Lock()

for key, c := range cron {
if now > *c.last+c.interval {
*cron[key].last = now
if !c.cb() {
delete(cron, key)
}
}
}

cronMU.Unlock()
}
}()
}

// NewCron adds a new, unnamed cron job to the queue
//
// minimum interval: 1 minute
//
// in the callback, return true to keep the job running,
// and return false to end the job
func New(interval time.Duration, cb func() bool) error {
intrv := interval.Milliseconds()
if intrv < 60000 {
intrv = 60000
}

now := time.Now().UnixMilli()

cronMU.Lock()
defer cronMU.Unlock()

name := "+job:" + string(goutil.URandBytes(16))

loops := 1000
for loops > 0 {
if _, ok := cron[name]; !ok {
break
}
loops--
name += string(goutil.URandBytes(16))
}

if _, ok := cron[name]; ok {
return io.EOF
}

cron[name] = cronJob{
interval: intrv,
last: &now,
cb: cb,
}

return nil
}

// SetCron adds or overwrites a named cron job
func Set(name string, interval time.Duration, cb func() bool) {
name = "#job:" + name

intrv := interval.Milliseconds()
if intrv < 60000 {
intrv = 60000
}

now := time.Now().UnixMilli()

cronMU.Lock()
defer cronMU.Unlock()

cron[name] = cronJob{
interval: intrv,
last: &now,
cb: cb,
}
}

// HasCron checks if a named cron job exists
func Has(name string) bool {
name = "#job:" + name

cronMU.Lock()
defer cronMU.Unlock()

if _, ok := cron[name]; ok {
return true
}
return false
}

// DelCron removes a named cron job
func Del(name string) {
name = "#job:" + name

cronMU.Lock()
defer cronMU.Unlock()

delete(cron, name)
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module chat-app
module github.com/tkdeng/simplewebserver

go 1.22.9

Expand All @@ -8,7 +8,6 @@ require (
github.com/tkdeng/goregex v0.1.0
github.com/tkdeng/goutil v0.7.2
github.com/tkdeng/staticweb v0.0.4
github.com/tkdeng/webserver v0.0.0-20241106144902-3a4c8d2149f0
)

require (
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ github.com/tkdeng/goutil v0.7.2 h1:ywA9iSLFc3Ad+64XUZ/hF/Y5DOpi7zckJIVKjHVU8qI=
github.com/tkdeng/goutil v0.7.2/go.mod h1:3kDx00BW5bQx/wSl5L5XAwYScNRYX0nxTdrlcWxBXSw=
github.com/tkdeng/staticweb v0.0.4 h1:LgmByRuSBeNR7jEuK57ESZc7aDrTgfdnYLoD4DuiznY=
github.com/tkdeng/staticweb v0.0.4/go.mod h1:J3HHg2ltiSB1VcPqlzA1f5AmcVQXNSgFYJXh3XHhcao=
github.com/tkdeng/webserver v0.0.0-20241106144902-3a4c8d2149f0 h1:N+fnOiUns0UvihcSiFJuQgZOVZ+4AcRNafBqBDCi7oQ=
github.com/tkdeng/webserver v0.0.0-20241106144902-3a4c8d2149f0/go.mod h1:MtMC2RDtHimlfm8yiQ5kl6BrU1krwuexkkyXqs85y20=
github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU=
github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY=
github.com/tklauser/numcpus v0.9.0 h1:lmyCHtANi8aRUgkckBgoDk1nHCux3n2cgkJLXdQGPDo=
Expand Down
29 changes: 0 additions & 29 deletions render.go

This file was deleted.

2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package server

import (
"chat-app/render"
"github.com/tkdeng/simplewebserver/render"
"os"
"path/filepath"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion server_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/gofiber/fiber/v3"
regex "github.com/tkdeng/goregex"
"github.com/tkdeng/goutil"
"github.com/tkdeng/webserver/cron"
"github.com/tkdeng/simplewebserver/cron"
)

var hasFailedSSL bool
Expand Down

0 comments on commit c267faf

Please sign in to comment.