forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
152 lines (142 loc) · 5.57 KB
/
plugins.go
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
149
150
151
152
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
// Package grpc provides a gRPC service to access the store for scheduler job definitions.
package grpc
import (
"context"
"path"
"github.com/pydio/cells/common/proto/sync"
"github.com/micro/go-micro"
"go.uber.org/zap"
"github.com/pydio/cells/broker/log"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config"
log3 "github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/plugins"
proto "github.com/pydio/cells/common/proto/jobs"
log2 "github.com/pydio/cells/common/proto/log"
"github.com/pydio/cells/common/service"
"github.com/pydio/cells/scheduler/jobs"
)
var (
Migration140 = false
Migration150 = false
)
func init() {
plugins.Register(func() {
service.NewService(
service.Name(common.SERVICE_GRPC_NAMESPACE_+common.SERVICE_JOBS),
service.Tag(common.SERVICE_TAG_SCHEDULER),
service.Description("Store for scheduler jobs description"),
service.Unique(true),
service.Fork(true),
service.Migrations([]*service.Migration{
{
TargetVersion: service.ValidVersion("1.4.0"),
Up: func(ctx context.Context) error {
// Set flag for migration script to be run AfterStart (see below, handler cannot be shared)
Migration140 = true
return nil
},
},
{
TargetVersion: service.ValidVersion("1.5.0"),
Up: func(ctx context.Context) error {
// Set flag for migration script to be run AfterStart (see below, handler cannot be shared)
Migration150 = true
return nil
},
},
}),
service.WithMicro(func(m micro.Service) error {
serviceDir, e := config.ServiceDataDir(common.SERVICE_GRPC_NAMESPACE_ + common.SERVICE_JOBS)
if e != nil {
return e
}
store, err := jobs.NewBoltStore(path.Join(serviceDir, "jobs.db"))
if err != nil {
return err
}
logStore, err := log.NewSyslogServer(path.Join(serviceDir, "tasklogs.bleve"), "tasksLog")
if err != nil {
return err
}
handler := NewJobsHandler(store, logStore)
proto.RegisterJobServiceHandler(m.Options().Server, handler)
log2.RegisterLogRecorderHandler(m.Options().Server, handler)
sync.RegisterSyncEndpointHandler(m.Options().Server, handler)
m.Init(
micro.BeforeStop(func() error {
handler.Close()
logStore.Close()
store.Close()
return nil
}),
micro.AfterStart(func() error {
for _, j := range getDefaultJobs() {
if e := handler.GetJob(m.Options().Context, &proto.GetJobRequest{JobID: j.ID}, &proto.GetJobResponse{}); e != nil {
handler.PutJob(m.Options().Context, &proto.PutJobRequest{Job: j}, &proto.PutJobResponse{})
}
}
// Clean tasks stuck in "Running" status
handler.CleanStuckTasks(m.Options().Context)
if Migration140 {
response := &proto.DeleteTasksResponse{}
if e = handler.DeleteTasks(m.Options().Context, &proto.DeleteTasksRequest{
JobId: "users-activity-digest",
Status: []proto.TaskStatus{proto.TaskStatus_Any},
PruneLimit: 1,
}, response); e == nil {
log3.Logger(m.Options().Context).Info("Migration 1.4.0: removed tasks on job users-activity-digest that could fill up the scheduler", zap.Any("number", len(response.Deleted)))
} else {
log3.Logger(m.Options().Context).Error("Error while trying to prune tasks for job users-activity-digest", zap.Error(e))
}
if e = handler.DeleteTasks(m.Options().Context, &proto.DeleteTasksRequest{
JobId: "resync-changes-job",
Status: []proto.TaskStatus{proto.TaskStatus_Any},
PruneLimit: 1,
}, response); e == nil {
log3.Logger(m.Options().Context).Info("Migration 1.4.0: removed tasks on job resync-changes-job that could fill up the scheduler", zap.Any("number", len(response.Deleted)))
} else {
log3.Logger(m.Options().Context).Error("Error while trying to prune tasks for job resync-changes-job", zap.Error(e))
}
}
if Migration150 {
// Remove archive-changes-job
if e := handler.DeleteJob(m.Options().Context, &proto.DeleteJobRequest{JobID: "archive-changes-job"}, &proto.DeleteJobResponse{}); e != nil {
log3.Logger(m.Options().Context).Error("Could not remove archive-changes-job", zap.Error(e))
} else {
log3.Logger(m.Options().Context).Info("[Migration] Removed archive-changes-job")
}
// Remove archive-changes-job
if e := handler.DeleteJob(m.Options().Context, &proto.DeleteJobRequest{JobID: "resync-changes-job"}, &proto.DeleteJobResponse{}); e != nil {
log3.Logger(m.Options().Context).Error("Could not remove resync-changes-job", zap.Error(e))
} else {
log3.Logger(m.Options().Context).Info("[Migration] Removed resync-changes-job")
}
}
return nil
}),
)
return nil
}),
)
})
}