Skip to content

Commit

Permalink
Add more schecule options for tasks (#215)
Browse files Browse the repository at this point in the history
* Allow a task to be scheduled for manual, once, hourly, daily, weekly, monthly or cron execution. 
* Bump nexus version to 3.19.1-01 in pom.xml file
* Allow to enable or disable tasks
* Link to scheduled new tasks configuration doc in wiki
  • Loading branch information
zeitounator authored Nov 26, 2019
1 parent 7652607 commit 14cd6d4
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,10 @@ nexus_rut_auth_header: "CUSTOM_HEADER"
```

### Scheduled tasks

These are quick examples and instruction to setup scheduled tasks. For in depth information on available tasks types
and schedule types, please refer to [the specific section in the repo wiki](https://github.com/ansible-ThoTeam/nexus3-oss/wiki/Scheduled-tasks-configuration)

```yaml
nexus_scheduled_tasks: []
# # Example task to compact blobstore :
Expand Down Expand Up @@ -684,6 +688,7 @@ nexus_rut_auth_header: "CUSTOM_HEADER"
* `taskProperties` for all string properties (i.e. repository names, blobstore names, time periods...).
* `booleanTaskProperties` for all boolean properties (i.e. mainly checkboxes in nexus create task GUI).


### Backups
```yaml
nexus_backup_configure: false
Expand Down
90 changes: 84 additions & 6 deletions files/groovy/create_task.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import groovy.json.JsonSlurper
import org.sonatype.nexus.scheduling.TaskConfiguration
import org.sonatype.nexus.scheduling.TaskInfo
import org.sonatype.nexus.scheduling.TaskScheduler
import org.sonatype.nexus.scheduling.schedule.Monthly
import org.sonatype.nexus.scheduling.schedule.Schedule
import org.sonatype.nexus.scheduling.schedule.Weekly
import java.text.SimpleDateFormat

parsed_args = new JsonSlurper().parseText(args)

Expand All @@ -18,17 +21,92 @@ if (existingTask && existingTask.getCurrentState().getRunState() != null) {
}

TaskConfiguration taskConfiguration = taskScheduler.createTaskConfigurationInstance(parsed_args.typeId)
if (existingTask) { taskConfiguration.setId(existingTask.getId()) }
if (existingTask) {
taskConfiguration.setId(existingTask.getId())
}

taskConfiguration.setName(parsed_args.name)

parsed_args.taskProperties.each { key, value -> taskConfiguration.setString(key, value) }
taskConfiguration.setAlertEmail(parsed_args.get('task_alert_email', '') as String)

taskConfiguration.setEnabled(Boolean.valueOf(parsed_args.get('enabled', 'true') as String))

if (parsed_args.task_alert_email) {
taskConfiguration.setAlertEmail(parsed_args.task_alert_email)
parsed_args.taskProperties.each { key, value ->
taskConfiguration.setString(key, value)
}

parsed_args.booleanTaskProperties.each { key, value -> taskConfiguration.setBoolean(key, Boolean.valueOf(value)) }
parsed_args.booleanTaskProperties.each { key, value ->
taskConfiguration.setBoolean(key, Boolean.valueOf(value))
}

// Init empty/default vars from script call

// Type of schedule. Defaults to cron
type = parsed_args.get('schedule_type', 'cron')

Schedule schedule = taskScheduler.scheduleFactory.cron(new Date(), parsed_args.cron)
// Cron expression. Used only for cron schecule. Defaults to null
cron = parsed_args.get('cron', null)

// Start date time. Defaults to now. Unused for manual, now and cron schedules
// This is our expected date format
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
date_time_string = parsed_args.get('start_date_time', null)
Date start_date
if (date_time_string)
start_date = dateFormat.parse(date_time_string)
else
start_date = new Date()

// List of weekdays to run task. Used for weekly schedule. Need at least one
// defaults to null which will error if this schedule is chosen
weekly_days = parsed_args.get('weekly_days', null)

// List of calendar days to run task. Used for monthly schedule. Need at leas one
// defaults to null which will error if this schedule is chosen
monthly_days = parsed_args.get('monthly_days', null)

Schedule schedule
switch(type) {
case 'manual':
schedule = taskScheduler.scheduleFactory.manual()
break
case 'now':
schedule = taskScheduler.scheduleFactory.now()
break
case 'once':
schedule = taskScheduler.scheduleFactory.once(start_date)
break
case 'hourly':
schedule = taskScheduler.scheduleFactory.hourly(start_date)
break
case 'daily':
schedule = taskScheduler.scheduleFactory.daily(start_date)
break
case 'weekly':
if (!weekly_days)
throw new Exception('Weekly scehedule requires a weekly_days list parameter')
Set<Weekly.Weekday> weekdays = []
weekly_days.each { day ->
weekdays.add(Weekly.Weekday.valueOf(day))
}
schedule = taskScheduler.scheduleFactory.weekly(start_date, weekdays)
break
case 'monthly':
if (!monthly_days)
throw new Exception('Monthly scehedule requires a weekly_days list parameter')
Set<Monthly.CalendarDay> calendardays = []
monthly_days.each { day ->
calendardays.add(Monthly.CalendarDay.day(day as Integer))
}
schedule = taskScheduler.scheduleFactory.monthly(start_date, calendardays)
break
case 'cron':
schedule = taskScheduler.scheduleFactory.cron(new Date(), cron)
break
default:
/** @todo: dont crash, return error in json response **/
throw new Exception("Unknown schedule type: " + parsed_args.schedule_type.toString())
break
}

taskScheduler.scheduleTask(taskConfiguration, schedule)
60 changes: 58 additions & 2 deletions molecule/nexus_common_test_vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ nexus_repos_apt_proxy:
negative_cache_ttl: 60

nexus_scheduled_tasks:
# Example task to purge maven snapshots
- name: Purge-maven-snapshots
# Example task to purge maven snapshots with cron schedule
- name: Purge maven snapshots
schedule_type: cron
cron: '0 50 23 * * ?'
typeId: repository.maven.remove-snapshots
task_alert_email: [email protected] # optional
Expand All @@ -165,6 +166,61 @@ nexus_scheduled_tasks:
gracePeriodInDays: "2"
booleanTaskProperties:
removeIfReleased: true
# Example task to run manually, now, once, daily, and hourly
- name: Purge unused maven snapshots manual
schedule_type: manual
typeId: repository.maven.purge-unused-snapshots
taskProperties:
repositoryName: "*"
lastUsed: "1"
- name: Purge-unused-maven-snapshots-now
schedule_type: now
typeId: repository.maven.purge-unused-snapshots
taskProperties:
repositoryName: "*"
lastUsed: "2"
- name: Purge-unused-maven-snapshots-once
schedule_type: once
start_date_time: "2050-02-02T00:01:02"
typeId: repository.maven.purge-unused-snapshots
taskProperties:
repositoryName: "*"
lastUsed: "3"
- name: Purge-unused-maven-snapshots-monthly
schedule_type: monthly
start_date_time: "2050-03-03T00:01:02"
monthly_days:
- 1
- 15
- 999 # Last day of month
typeId: repository.maven.purge-unused-snapshots
taskProperties:
repositoryName: "*"
lastUsed: "4"
- name: Purge-unused-maven-snapshots-weekly
schedule_type: weekly
start_date_time: "2050-04-04T00:01:02"
weekly_days:
- MON
- WED
typeId: repository.maven.purge-unused-snapshots
taskProperties:
repositoryName: "*"
lastUsed: "5"
- name: Purge-unused-maven-snapshots-daily
schedule_type: daily
start_date_time: "2050-05-05T00:01:02"
typeId: repository.maven.purge-unused-snapshots
taskProperties:
repositoryName: "*"
lastUsed: "6"
- name: Purge-unused-maven-snapshots-hourly
schedule_type: hourly
start_date_time: "2050-06-06T00:01:02"
typeId: repository.maven.purge-unused-snapshots
taskProperties:
repositoryName: "*"
lastUsed: "7"

nexus_local_users:
- username: jenkins
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</description>

<properties>
<nx-version>3.15.2-01</nx-version>
<nx-version>3.19.1-01</nx-version>
</properties>

<build>
Expand Down

0 comments on commit 14cd6d4

Please sign in to comment.