Flexible schedule system

This is not a Support area! Discuss about the Server here. Non-Server related discussion goes in Off-Topic Discussion.
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
User avatar
VlLight
L2j Veteran
L2j Veteran
Posts: 577
Joined: Fri Dec 14, 2007 11:58 am
Location: Russia

Flexible schedule system

Post by VlLight »

It seems to be handy for me, so maybe it would useful for someone.
I'm often facing with task of scheduling some events. It isn't big problem to create (and make it configurable) shedule for one launche or several launches at predefined time per day, like TVT Event (howewer, there is still no universal method for handling schedule string like "3:00,9:00,15:00,21:00" in server).
But it becomes much harder to make universal configurable definitions for "3:00,9:00 in every n days", "n times in month" and so on.
I think, many people know about cron, and it looks like suitable solution for this case. After some time, spendig in search of java implementation of cron , i've found quartz library (actual version is quartz-2.0.2), that contains one class, that is able to calculate nearest launch time from cron-like string.
So, in general it looks like this:

Code: Select all

import com.l2jserver.gameserver.model.Quest; import java.util.Calendar;import org.quartz.impl.triggers.CronTriggerImpl; public class SampleEvent extends Quest{    private static final String cronDef = "0 0 3,9,15,21 ? * *";        public SampleEvent(int questId, String name, String descr)    {        super(questId, name, descr);                long timeToStart = getNextRunInterval("cronDef");                if (timeToStart >= 0)            startQuestTimer("start_event", timeToStart, null, null);            }     private void endEvent()    {        long timeToStart = getNextRunInterval("cronDef");        if (timeToStart >= 0)            startQuestTimer("start_event", timeToStart, null, null);            }     /**     * @param cronString - cron-like string     * @return interval to next run for timer     */    private long getNextRunInterval(String cronString)    {        long ret;        Date currTime = Calendar.getInstance().getTime();        CronTriggerImpl tr = new CronTriggerImpl();        try        {            tr.setCronExpression(cronString);            Date nextFireAt = tr.getFireTimeAfter(currTime);            ret = nextFireAt.getTime() - System.currentTimeMillis();         }        catch(Exception e)        {            ret = -1;        }                return ret;    }     public static void main(String[] args)    {        new SampleEvent(-1, "SampleEvent", "events");    }} 
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

Re: Flexible schedule system

Post by jurchiks »

Nice find! Unless some devs are against flexibility, this might even get used in l2j (but ofc, testing first).
Good thing it is simple to use and open-source.
SPOILER:
official site: here
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
UnAfraid
L2j Veteran
L2j Veteran
Posts: 4199
Joined: Mon Jul 23, 2007 4:25 pm
Location: Bulgaria
Contact:

Re: Flexible schedule system

Post by UnAfraid »

hmm.. sounds interesting
Image
Nik
L2j Veteran
L2j Veteran
Posts: 629
Joined: Fri Apr 18, 2008 9:09 pm

Re: Flexible schedule system

Post by Nik »

seems pretty interesting :)
Playing a game where you know how every single mechanism works is quite satisfying.
Its the main perk that a gamer-developer has :D
User avatar
nonom
L2j Veteran
L2j Veteran
Posts: 649
Joined: Wed Mar 11, 2009 10:34 pm
Location: Magmeld

Re: Flexible schedule system

Post by nonom »

jurchiks wrote:Nice find! Unless some devs are against flexibility, this might even get used in l2j (but ofc, testing first).
Good thing it is simple to use and open-source.
SPOILER:
official site: here
@jurchiks, but cron is not multi-platform :) bye flexibility and simplicity

Anyways is an useful tool, thanks for share
Image
"There are three kinds of people in this world, those who can count and those who can't"
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

Re: Flexible schedule system

Post by jurchiks »

where did you read this tool uses linux cron?
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
nonom
L2j Veteran
L2j Veteran
Posts: 649
Joined: Wed Mar 11, 2009 10:34 pm
Location: Magmeld

Re: Flexible schedule system

Post by nonom »

jurchiks wrote:where did you read this tool uses linux cron?
Sorry jurchiks I was wrong
Image
"There are three kinds of people in this world, those who can count and those who can't"
Post Reply