NullPointerExceptions after adding TimerTrigger via groovy script.

Visto 261 veces
Saltar al primer mensaje no leído

AWeber - Jason Schadel

no leída,
7 mar 2011, 10:24:297/3/11
a jenkins...@googlegroups.com
I used the following script to add a TimerTrigger to all the jobs in a view but when the time came, all the modified jobs never ran.

---Begin Script---
import hudson.model.*
import hudson.triggers.*

TriggerDescriptor TIMER_TRIGGER_DESCRIPTOR = Hudson.instance.getDescriptorOrDie(TimerTrigger.class)
assert TIMER_TRIGGER_DESCRIPTOR != null;

view = Hudson.instance.getView("Trunk Jobs")
for(item in view.getItems()) {
println(item.name)
AbstractProject project = (AbstractProject) item;

newSpecString = new String("0 2 * * 1-5")

def trigger = item.getTriggers().get(TIMER_TRIGGER_DESCRIPTOR)
if(trigger != null && trigger instanceof TimerTrigger) {
println("> Removing existing Timer Trigger => $trigger.spec")
project.removeTrigger(TIMER_TRIGGER_DESCRIPTOR)
}

def newTrigger = new TimerTrigger(newSpecString)
println("> Adding new Timer Trigger => $newTrigger.spec")
project.addTrigger(newTrigger)
}

---End Script---

The log showed the following error for all the jobs modified:

---Begin Error---
Mar 7, 2011 2:01:03 AM hudson.triggers.Trigger checkTriggers
WARNING: hudson.triggers.TimerTrigger.run() failed for affiliate-cookier
java.lang.NullPointerException
at hudson.triggers.TimerTrigger.run(TimerTrigger.java:53)
at hudson.triggers.Trigger.checkTriggers(Trigger.java:249)
at hudson.triggers.Trigger$Cron.doRun(Trigger.java:197)
at hudson.triggers.SafeTimerTask.run(SafeTimerTask.java:54)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
---End Error---

Did I miss something in the groovy script? Should the configurations be reloaded after a change like this?

Jason Schadel
AWeber Communications, Inc.
http://www.aweber.com


Colin McIntyre

no leída,
29 oct 2014, 4:57:4729/10/14
a jenkins...@googlegroups.com,jas...@aweber.com

Old post, I know, but I just came across this issue so thought I'd add my findings.  https://issues.jenkins-ci.org/browse/JENKINS-14771 looks to be the same issue, the reason for the NPE is that while you've added the trigger to the job you haven't told the trigger about the job.  To work around it I used this code:


def job = hudson.model.Hudson.instance.getJob("My Job");
def spec = "0 0 1 * *";
hudson.triggers.TimerTrigger newCron = new hudson.triggers.TimerTrigger(spec);
newCron.start(job, true);
job.addTrigger(newCron);
job.save(); 

It's the call to TimerTrigger.start that sorted it for me.


kumar naresh

no leída,
29 ago 2016, 9:12:1529/8/16
a Jenkins Users,jas...@aweber.com

HI Colin,

this groovy is updating/adding build perodic section in build triggers section.

can you please share the script to update poll scm section in build triger section via groovy script.

Regards,
Raju.

Baptiste Mathus

no leída,
30 ago 2016, 16:55:0630/8/16
a jenkins...@googlegroups.com
Hello,

There's a bunch of examples there: https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Script+Console (see the end of the page).

Cheers

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/8e179ac5-a7ea-44ae-b0b8-3fef0fda9ec6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos