Skip to content

Commit

Permalink
fix: some minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
FMotalleb committed Jun 28, 2024
1 parent d1aa972 commit 6004c79
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 25 deletions.
3 changes: 1 addition & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"env": {},
"args": [
"parse",
"crontab.log",
"-u"
"crontab.example",
]
},
]
Expand Down
16 changes: 12 additions & 4 deletions cmd/parser/cron_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,18 @@ func initJob(jobName string, timing string, cfg *config.Config) {
job.Name = jobName
job.Description = "Imported from cron file"
job.Disabled = false
job.Events = []config.JobEvent{
{
Cron: timing,
},
if strings.Contains(timing, "@reboot") {
job.Events = []config.JobEvent{
{
OnInit: true,
},
}
} else {
job.Events = []config.JobEvent{
{
Cron: timing,
},
}
}
cfg.Jobs = append(cfg.Jobs, job)
}
5 changes: 4 additions & 1 deletion cmd/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var (
func run(cmd *cobra.Command, args []string) {
cfg.cronFile = cmd.Flags().Arg(0)
cron, err := readInCron(cfg)
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
})
if err != nil {
log.Panic(err)
}
Expand Down Expand Up @@ -86,5 +89,5 @@ func readInCron(cfg *parserConfig) (*CronString, error) {
func init() {
ParserCmd.PersistentFlags().StringVarP(&cfg.output, "output", "o", "", "output file to write configuration to")
ParserCmd.PersistentFlags().BoolVarP(&cfg.hasUser, "with-user", "u", false, "indicates that whether the given cron file has user field")
ParserCmd.PersistentFlags().StringVar(&cfg.cronMatcher, "matcher", `(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*|(\*\/\d)) ?){5,7})`, "matcher for cron")
ParserCmd.PersistentFlags().StringVar(&cfg.cronMatcher, "matcher", `(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*|(\*\/\d))\s*){5,7})`, "matcher for cron")
}
85 changes: 74 additions & 11 deletions config.local.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,81 @@
# yaml-language-server: $schema=schema.json
#TODO: unix/tcp socket controller
#TODO: prometheus exporter

jobs:
- name: Test Job
- description: Imported from cron file
events:
- cron: 0 * * * *
hooks: {}
name: "FromCron: 0 * * * *"
tasks:
- command: /usr/bin/wget -O - -q -t 1 http://localhost/cron.php
- description: Imported from cron file
events:
- cron: "* * * * *"
hooks: {}
name: "FromCron: * * * * *"
tasks:
- command: /var/www/devdaily.com/bin/check-apache.sh
- description: Imported from cron file
events:
- cron: 5 10,22 * * *
hooks: {}
name: "FromCron: 5 10,22 * * *"
tasks:
- command: /var/www/devdaily.com/bin/mk-new-links.php
- description: Imported from cron file
events:
- cron: 30 4 * * *
hooks: {}
name: "FromCron: 30 4 * * *"
tasks:
- command: /var/www/devdaily.com/bin/create-all-backups.sh
- description: Imported from cron file
events:
- cron: 5 0,4,10,16 * * *
hooks: {}
name: "FromCron: 5 0,4,10,16 * * *"
tasks:
- command: /var/www/devdaily.com/bin/create-cat-list.sh
- description: Imported from cron file
events:
- cron: 5 0 * * *
hooks: {}
name: "FromCron: 5 0 * * *"
tasks:
- command: /var/www/devdaily.com/bin/resetContactForm.sh
- description: Imported from cron file
events:
- cron: 0,20,40 * * * *
hooks: {}
name: "FromCron: 0,20,40 * * * *"
tasks:
- command: /var/www/bin/ads/freshMint.sh
- description: Imported from cron file
events:
- cron: 5,25,45 * * * *
hooks: {}
name: "FromCron: 5,25,45 * * * *"
tasks:
- command: echo 1
retry-delay: 5s
# connections:
# - image: docker-mysql-database-phpmyadmin
# volumes:
# - "/home/motalleb/Downloads:/var/local/test"
# env:
# SHELL: /bin/bash
- command: /var/www/bin/ads/greenTaffy.sh
- description: Imported from cron file
events:
- cron: 10,30,50 * * * *
hooks: {}
name: "FromCron: 10,30,50 * * * *"
tasks:
- command: /var/www/bin/ads/raspberry.sh
- description: Imported from cron file
events:
- cron: 15,35,55 * * * *
hooks: {}
name: "FromCron: 15,35,55 * * * *"
tasks:
- command: /var/www/bin/ads/robinsEgg.sh
- description: Imported from cron file
events:
- on-init: true
- web-event: test
hooks: {}
name: "FromCron: @reboot"
tasks:
- command: test
12 changes: 6 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type JobConfig struct {
type JobEvent struct {
Cron string `mapstructure:"cron" json:"cron,omitempty"`
Interval time.Duration `mapstructure:"interval" json:"interval,omitempty"`
OnInit bool `mapstructure:"on-init" json:"on_init,omitempty"`
WebEvent string `mapstructure:"web-event" json:"web_event,omitempty"`
OnInit bool `mapstructure:"on-init" json:"on-init,omitempty"`
WebEvent string `mapstructure:"web-event" json:"web-event,omitempty"`
}

// JobHooks represents the hooks configuration for a job.
Expand All @@ -64,20 +64,20 @@ type Task struct {

// Command params
Command string `mapstructure:"command" json:"command,omitempty"`
WorkingDirectory string `mapstructure:"working-dir" json:"working_directory,omitempty"`
WorkingDirectory string `mapstructure:"working-dir" json:"working-directory,omitempty"`
UserName string `mapstructure:"user" json:"user,omitempty"`
GroupName string `mapstructure:"group" json:"group,omitempty"`
Env map[string]string `mapstructure:"env" json:"env,omitempty"`
Connections []TaskConnection `mapstructure:"connections" json:"connections,omitempty"`

// Retry & Timeout config
Retries uint `mapstructure:"retries" json:"retries,omitempty"`
RetryDelay time.Duration `mapstructure:"retry-delay" json:"retry_delay,omitempty"`
RetryDelay time.Duration `mapstructure:"retry-delay" json:"retry-delay,omitempty"`
Timeout time.Duration `mapstructure:"timeout" json:"timeout,omitempty"`

// Hooks
OnDone []Task `mapstructure:"on-done" json:"on_done,omitempty"`
OnFail []Task `mapstructure:"on-fail" json:"on_fail,omitempty"`
OnDone []Task `mapstructure:"on-done" json:"on-done,omitempty"`
OnFail []Task `mapstructure:"on-fail" json:"on-fail,omitempty"`
}

// TaskConnection represents the connection configuration for a task.
Expand Down
41 changes: 41 additions & 0 deletions crontab.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#--------------------------------------------------
# example unix/linux crontab file format:
#--------------------------------------------------
# min,hour,dayOfMonth,month,dayOfWeek command
#
# field allowed values
# ----- --------------
# minute 0-59
# hour 0-23
# day of month 1-31
# month 1-12 (or names, see below)
# day of week 0-7 (0 or 7 is Sun, or use names)
#
#--------------------------------------------------

# run the drupal cron process every hour of every day
0 * * * * /usr/bin/wget -O - -q -t 1 http://localhost/cron.php

# run this apache kludge every minute of every day
* * * * * /var/www/devdaily.com/bin/check-apache.sh

# generate links to new blog posts twice a day
5 10,22 * * * /var/www/devdaily.com/bin/mk-new-links.php

# run the backup scripts at 4:30am
30 4 * * * /var/www/devdaily.com/bin/create-all-backups.sh

# re-generate the blog "categories" list (four times a day)
5 0,4,10,16 * * * /var/www/devdaily.com/bin/create-cat-list.sh

# reset the contact form just after midnight
5 0 * * * /var/www/devdaily.com/bin/resetContactForm.sh

# rotate the ad banners every five minutes

0,20,40 * * * * /var/www/bin/ads/freshMint.sh
5,25,45 * * * * /var/www/bin/ads/greenTaffy.sh
10,30,50 * * * * /var/www/bin/ads/raspberry.sh
15,35,55 * * * * /var/www/bin/ads/robinsEgg.sh

@reboot test
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"

"github.com/robfig/cron/v3"
"github.com/sirupsen/logrus"

"github.com/FMotalleb/crontab-go/cmd"
"github.com/FMotalleb/crontab-go/core/global"
Expand All @@ -31,10 +32,13 @@ import (
)

func main() {
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
})
defer func() {
if err := recover(); err != nil {
log.Printf(
"recovering from a panic if you think this is an error from application please report at: %s",
"an error stopped application from working, if you think this is an error in application side please report to %s",
meta.Issues(),
)
os.Exit(1)
Expand Down

0 comments on commit 6004c79

Please sign in to comment.