-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathdriver_database.go
More file actions
148 lines (120 loc) · 3.92 KB
/
Copy pathdriver_database.go
File metadata and controls
148 lines (120 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package queue
import (
"fmt"
"time"
contractscache "github.com/goravel/framework/contracts/cache"
contractsdb "github.com/goravel/framework/contracts/database/db"
contractsfoundation "github.com/goravel/framework/contracts/foundation"
contractsqueue "github.com/goravel/framework/contracts/queue"
"github.com/goravel/framework/errors"
"github.com/goravel/framework/queue/models"
"github.com/goravel/framework/queue/utils"
"github.com/goravel/framework/support/carbon"
)
var (
_ contractsqueue.Driver = &Database{}
)
type Database struct {
cache contractscache.Cache
db contractsdb.DB
jobStorer contractsqueue.JobStorer
json contractsfoundation.Json
jobsTable string
retryAfter int
}
func NewDatabase(
config contractsqueue.Config,
cache contractscache.Cache,
db contractsdb.DB,
jobStorer contractsqueue.JobStorer,
json contractsfoundation.Json,
connection string) (*Database, error) {
if cache == nil {
return nil, errors.CacheFacadeNotSet.SetModule(errors.ModuleQueue)
}
dbConnection := config.GetString(fmt.Sprintf("queue.connections.%s.connection", connection))
if dbConnection == "" {
return nil, errors.QueueInvalidDatabaseConnection.Args(connection)
}
return &Database{
cache: cache,
db: db.Connection(dbConnection),
jobStorer: jobStorer,
json: json,
jobsTable: config.GetString(fmt.Sprintf("queue.connections.%s.table", connection), "jobs"),
retryAfter: config.GetInt(fmt.Sprintf("queue.connections.%s.retry_after", connection), 60),
}, nil
}
func (r *Database) Driver() string {
return contractsqueue.DriverDatabase
}
func (r *Database) Pop(queue string) (contractsqueue.ReservedJob, error) {
var job models.Job
cacheLock := fmt.Sprintf("goravel:queue-database-%s:lock", queue)
lock := r.cache.Lock(cacheLock, 1*time.Minute)
if !lock.Block(1 * time.Minute) {
return nil, errors.QueuePopIsLocked.Args(queue, cacheLock)
}
defer lock.Release()
if err := r.db.Transaction(func(tx contractsdb.Tx) error {
if err := tx.Table(r.jobsTable).LockForUpdate().Where("queue", queue).Where(func(q contractsdb.Query) contractsdb.Query {
return q.Where(func(q1 contractsdb.Query) contractsdb.Query {
return r.isAvailable(q1)
})
// TODO: Add the retry logic in another PR
// .OrWhere(func(q1 contractsdb.Query) contractsdb.Query {
// return r.isReservedButExpired(q1)
// })
}).OrderBy("id").First(&job); err != nil {
return err
}
if job.ID == 0 {
return errors.QueueDriverNoJobFound.Args(queue)
}
job.Increment()
job.Touch()
_, err := tx.Table(r.jobsTable).Where("id", job.ID).Update(map[string]any{
"attempts": job.Attempts,
"reserved_at": job.ReservedAt,
})
if err != nil {
return errors.QueueFailedToReserveJob.Args(job, err)
}
return nil
}); err != nil {
return nil, err
}
return NewDatabaseReservedJob(&job, r.db, r.jobStorer, r.json, r.jobsTable)
}
func (r *Database) Push(task contractsqueue.Task, queue string) error {
now := carbon.Now()
availableAt := carbon.NewDateTime(now)
if !task.Delay.IsZero() {
availableAt = carbon.NewDateTime(carbon.FromStdTime(task.Delay))
}
payload, err := utils.TaskToJson(task, r.json)
if err != nil {
return err
}
job := models.Job{
Queue: queue,
Payload: payload,
AvailableAt: availableAt,
CreatedAt: carbon.NewDateTime(now.Copy()),
}
result, err := r.db.Table(r.jobsTable).Insert(&job)
if err != nil {
return errors.QueueFailedToInsertJobToDatabase.Args(job, err)
}
if result.RowsAffected == 0 {
return errors.QueueFailedToInsertJobToDatabase.Args(job, nil)
}
return nil
}
func (r *Database) isAvailable(query contractsdb.Query) contractsdb.Query {
return query.WhereNull("reserved_at").Where("available_at <= ?", carbon.Now())
}
// TODO: Add the retry logic in another PR
// func (r *Database) isReservedButExpired(query contractsdb.Query) contractsdb.Query {
// return query.Where("reserved_at", "<=", carbon.Now().AddSeconds(r.retryAfter))
// }