Skip to content

Commit 2c68f00

Browse files
1daidai1RileyWen
andauthored
feat: Add cbatch --open-mode option (#258)
* Add cbatch ----open-mode function * Sync proto from backend. * Refactor. --------- Co-authored-by: RileyWen <wrllrwwrllrw@gmail.com>
1 parent 600c784 commit 2c68f00

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

internal/cbatch/CmdArgParser.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var (
4848
FlagExport string
4949
FlagStdoutPath string
5050
FlagStderrPath string
51+
FlagOpenMode string
5152

5253
FlagWrappedScript string
5354

@@ -158,4 +159,5 @@ func init() {
158159
RootCmd.Flags().StringVar(&FlagMailUser, "mail-user", "", "Mail address of the notification receiver")
159160
RootCmd.Flags().StringVar(&FlagComment, "comment", "", "Comment of the job")
160161
RootCmd.Flags().BoolVar(&FlagJson, "json", false, "Output in JSON format")
162+
RootCmd.Flags().StringVar(&FlagOpenMode, "open-mode", "", "Set the mode for opening output and error files, supported values: append, truncate (default is truncate) ")
161163
}

internal/cbatch/cbatch.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ import (
3030

3131
log "github.com/sirupsen/logrus"
3232
"github.com/spf13/cobra"
33+
"google.golang.org/protobuf/proto"
3334
)
3435

3536
type CbatchArg struct {
3637
name string
3738
val string
3839
}
3940

40-
// Merge and validate arguments from the file and the command line,
41+
// ProcessCbatchArgs Merge and validate arguments from the file and the command line,
4142
// then return the constructed TaskToCtld.
4243
func ProcessCbatchArgs(cmd *cobra.Command, args []CbatchArg) (bool, *protos.TaskToCtld) {
4344
task := new(protos.TaskToCtld)
@@ -143,6 +144,15 @@ func ProcessCbatchArgs(cmd *cobra.Command, args []CbatchArg) (bool, *protos.Task
143144
structExtraFromScript.MailUser = arg.val
144145
case "--comment":
145146
structExtraFromScript.Comment = arg.val
147+
case "--open-mode":
148+
if arg.val == util.OpenModeAppend {
149+
task.GetBatchMeta().OpenModeAppend = proto.Bool(true)
150+
} else if arg.val == util.OpenModeTruncate {
151+
task.GetBatchMeta().OpenModeAppend = proto.Bool(false)
152+
} else {
153+
log.Errorf("--open-mode must be either '%s' or '%s'", util.OpenModeAppend, util.OpenModeTruncate)
154+
return false, nil
155+
}
146156
default:
147157
log.Errorf("Invalid argument: unrecognized '%s' is given in the script", arg.name)
148158
return false, nil
@@ -235,6 +245,16 @@ func ProcessCbatchArgs(cmd *cobra.Command, args []CbatchArg) (bool, *protos.Task
235245
if FlagComment != "" {
236246
structExtraFromCli.Comment = FlagComment
237247
}
248+
if FlagOpenMode != "" {
249+
if FlagOpenMode == util.OpenModeAppend {
250+
task.GetBatchMeta().OpenModeAppend = proto.Bool(true)
251+
} else if FlagOpenMode == util.OpenModeTruncate {
252+
task.GetBatchMeta().OpenModeAppend = proto.Bool(false)
253+
} else {
254+
log.Errorf("--open-mode must be either '%s' or '%s'", util.OpenModeAppend, util.OpenModeTruncate)
255+
return false, nil
256+
}
257+
}
238258

239259
// Set and check the extra attributes
240260
var extraFromCli string
@@ -327,7 +347,7 @@ func SendMultipleRequests(task *protos.TaskToCtld, count uint32) util.CraneCmdEr
327347
return util.ErrorSuccess
328348
}
329349

330-
// Split the job script into two parts: the arguments and the shell script.
350+
// ParseCbatchScript Split the job script into two parts: the arguments and the shell script.
331351
func ParseCbatchScript(path string, args *[]CbatchArg, sh *[]string) util.CraneCmdError {
332352
file, err := os.Open(path)
333353
if err != nil {

internal/util/paramConstraint.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package util
2+
3+
const (
4+
OpenModeAppend = "append"
5+
OpenModeTruncate = "truncate"
6+
)

protos/Crane.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,4 +872,4 @@ service CraneForeD {
872872
rpc CrunStream(stream StreamCrunRequest) returns(stream StreamCrunReply);
873873
rpc TaskIOStream(stream StreamTaskIORequest) returns(stream StreamTaskIOReply);
874874
rpc QueryTaskIdFromPort(QueryTaskIdFromPortRequest) returns (QueryTaskIdFromPortReply);
875-
}
875+
}

protos/PublicDefs.proto

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ message TaskToD {
217217

218218
message BatchTaskAdditionalMeta {
219219
string sh_script = 1;
220+
optional bool open_mode_append = 2;
220221
string output_file_pattern = 3;
221222
string error_file_pattern = 4;
222223
}
@@ -337,7 +338,7 @@ message TrimmedPartitionInfo {
337338

338339
message RichError {
339340
ErrCode code = 1;
340-
string description = 2;
341+
string description = 2;
341342
}
342343

343344
enum ErrCode {
@@ -524,4 +525,4 @@ message CranedRemoteMeta {
524525
string craned_version = 3;
525526
google.protobuf.Timestamp craned_start_time = 4;
526527
google.protobuf.Timestamp system_boot_time = 5;
527-
}
528+
}

0 commit comments

Comments
 (0)