1 # Batch EXecutor -- Jobs
2 # (c) 2011-2015 Martin Mares <mj@ucw.cz>
16 return $id =~ /^([0-9A-Za-z-]+)$/;
20 my ($class, $id) = @_;
24 check_id($id) or die "Invalid job ID";
28 my $dt = POSIX::strftime("%Y%m%d-%H%M%S", localtime);
29 my $hash = Digest::SHA::sha1_hex(join(":", $dt, $$, $job_cnt));
30 $job->{'ID'} = $dt . '-' . substr($hash, 0, 8);
32 $job->{'Subject'} = '';
36 sub new_from_file($$;$) {
37 my ($class, $file, $header_only) = @_;
39 open T, '<', $file or die "Cannot open $file: $!";
43 /^([A-Z][A-Za-z0-9-]*):\s*(.*)/ or die "Cannot load $file: Header syntax error";
44 !defined $job->{$1} or die "Cannot load $file: Header $1 re-defined";
49 $job->{'body'} = join("", @cmds);
52 $job->{'Subject'} //= '';
53 $job->{'ID'} or die "Cannot load $file: Missing ID";
54 check_id($job->{'ID'}) or die "Cannot load $file: Invalid ID syntax";
64 my $name = $job->{'ID'};
65 my $subj = $job->{'Subject'} // "";
66 $name .= " ($subj)" if $subj !~ /^\s*$/;
71 my ($job, $attr, $val) = @_;
72 $job->{$attr} = $val if defined $val;
78 for my $k (sort keys %$job) {
79 print "$k: ", $job->{$k}, "\n";
85 my $tmp = $BEX::Config::home . "/tmp";
86 -d $tmp or mkdir $tmp or die "Cannot create directory $tmp: $!";
87 $fn //= $tmp . '/' . $job->id;
88 open T, '>', $fn or die "Cannot create $fn: $!";
89 for my $k (sort grep { /^[A-Z]/ } keys %$job) {
90 print T "$k: ", $job->{$k}, "\n";
93 print T $job->{'body'} if defined $job->{'body'};