Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 8ddd88c

Browse files
ashiquzzaman33sadlil
authored andcommitted
Fix reschedule isuues (#22)
* remove rescheduling job while running worker stopped * Seperate jobDone, jobFailed, jobFailedWithException Support CanDOTimeout * remove dependency from db & fix some error * Server and worker restart support - set timeout for every running job - monitor all running job & remove if timeout expire * report to client that job failed with timeout exception * generic db call for all. * DB test fixed * fix * Merge Pull Request mikespook/gearman-go#75, Add sync lock to create job functions Signed-off-by: sadlil <[email protected]>
1 parent e009f36 commit 8ddd88c

File tree

12 files changed

+563
-345
lines changed

12 files changed

+563
-345
lines changed

client/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ func (client *Client) do(funcname string, data []byte, flag rt.PT) (handle strin
222222
return "", ErrLostConn
223223
}
224224
var result = make(chan handleOrError, 1)
225+
client.Lock()
226+
defer client.Unlock()
225227
client.innerHandler.put("c", func(resp *Response) {
226228
if resp.DataType == rt.PT_Error {
227229
err = getError(resp.Data)

client/client_test.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package client
22

33
import (
4+
"fmt"
5+
"sync"
46
"testing"
57
"time"
68

@@ -48,7 +50,7 @@ func TestClientEcho(t *testing.T) {
4850
}
4951

5052
func TestClientDoBg(t *testing.T) {
51-
handle, err := client.DoBg("ToUpper", []byte("abcdef"), rt.JobNormal)
53+
handle, err := client.DoBg("scheduledJobTest", []byte("abcdef"), rt.JobNormal)
5254
if err != nil {
5355
t.Error(err)
5456
return
@@ -61,7 +63,7 @@ func TestClientDoBg(t *testing.T) {
6163
}
6264

6365
func TestClientDoCron(t *testing.T) {
64-
handle, err := client.DoCron("scheduledJobTest", "* * * * *", []byte("test data"))
66+
handle, err := client.DoCron("scheduledJobTest", "* * * * 5", []byte("test data"))
6567
if err != nil {
6668
t.Fatal(err)
6769
}
@@ -73,7 +75,7 @@ func TestClientDoCron(t *testing.T) {
7375
}
7476

7577
func TestClientDoAt(t *testing.T) {
76-
handle, err := client.DoAt("scheduledJobTest", time.Now().Add(10*time.Second).Unix(), []byte("test data"))
78+
handle, err := client.DoAt("scheduledJobTest", time.Now().Add(20*time.Second).Unix(), []byte("test data"))
7779
if err != nil {
7880
t.Fatal(err)
7981
}
@@ -85,17 +87,28 @@ func TestClientDoAt(t *testing.T) {
8587
}
8688

8789
func TestClientDo(t *testing.T) {
90+
var wg sync.WaitGroup = sync.WaitGroup{}
91+
wg.Add(1)
8892
jobHandler := func(job *Response) {
89-
str := string(job.Data)
90-
if str == "ABCDEF" {
91-
t.Log(str)
92-
} else {
93-
t.Errorf("Invalid data: %s", job.Data)
93+
switch job.DataType {
94+
case rt.PT_WorkComplete:
95+
t.Log("Work complete, handle ", job.Handle)
96+
wg.Done()
97+
case rt.PT_WorkException, rt.PT_WorkFail:
98+
t.Log("Work fail, handle ", job.Handle, " cause: ", string(job.Data))
99+
wg.Done()
100+
case rt.PT_WorkData:
101+
t.Logf("Work data: %+v", string(job.Data))
102+
case rt.PT_WorkStatus:
103+
status, err := job.Status()
104+
if err != nil {
105+
t.Error(err)
106+
}
107+
fmt.Println("Work status, num: %v, denom: %v", status.Numerator, status.Denominator)
94108
}
95-
return
96109
}
97-
handle, err := client.Do("ToUpper", []byte("abcdef"),
98-
rt.JobLow, jobHandler)
110+
handle, err := client.Do("scheduledJobTest", []byte("abcdef"),
111+
rt.JobHigh, jobHandler)
99112
if err != nil {
100113
t.Error(err)
101114
return
@@ -105,6 +118,8 @@ func TestClientDo(t *testing.T) {
105118
} else {
106119
t.Log(handle)
107120
}
121+
wg.Wait()
122+
108123
}
109124

110125
func TestClientStatus(t *testing.T) {

pkg/runtime/job.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const (
99
PRIORITY_HIGH = 1
1010

1111
JobPrefix = "H:"
12-
SchedJobPrefix = "S:"
12+
CronJobPrefix = "S:"
1313
EpochTimePrefix = "UTC-"
1414
)
1515

@@ -22,7 +22,7 @@ type Job struct {
2222
Denominator int `json:"denominator,omitempty"`
2323
CreateAt time.Time `json:"created_at,omitempty"`
2424
ProcessAt time.Time `json:"process_at,omitempty"`
25-
TimeoutSec int `json:"timeout_sec,omitempty"`
25+
TimeoutSec int32 `json:"timeout_sec,omitempty"`
2626
CreateBy int64 `json:"created_by,omitempty"` //client sessionId
2727
ProcessBy int64 `json:"process_by,omitempty"` //worker sessionId
2828
FuncName string `json:"function_name,omitempty"`
@@ -42,3 +42,19 @@ type CronJob struct {
4242
SuccessfulRun int `json:"successful_run,omitempty"`
4343
FailedRun int `json:"failed_run,omitempty"`
4444
}
45+
46+
func (c *Job) Key() string {
47+
return c.Handle
48+
}
49+
50+
func (c *Job) Prefix() string {
51+
return JobPrefix
52+
}
53+
54+
func (c *CronJob) Key() string {
55+
return c.Handle
56+
}
57+
58+
func (c *CronJob) Prefix() string {
59+
return CronJobPrefix
60+
}

pkg/runtime/protocol.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ const (
8282
Req = 5391697
8383
ReqStr = "\x00REQ"
8484
// \x00RES
85-
Res = 5391699
86-
ResStr = "\x00RES"
85+
Res = 5391699
86+
ResStr = "\x00RES"
87+
DefaultTimeout = 20 // 20 Seconds
8788

8889
HANDLE_SHAKE_HEADER_LENGTH = 12
8990
)

0 commit comments

Comments
 (0)