FredixBlog


Just my log about anything I could find enjoyable.



Répartir les tâches du crontab unix

author Posted by: admin on date Jan 24th, 2012 | filed Filed under: Computing, MS-Access

Lorsqu’on a beaucoup de tâches à planifier avec le cron unix, il devient ardu de calculer de tête la bonne heure pour planifier chaque job. Facile pour une ou deux tâches, quand on a des dizaines de jobs à lancer pour une journée, un bon départ est de les répartir uniformément. On pourra toujours réajuster par la suite selon leur durée, mais il nous faut bien avoir une bonne façon de calculer cette répartition.

Les deux fonctions qui suivent vont faire ce travail. En prime, la possibilité de répartir les tâches sur plusieurs jours, jusqu’à 30. On peut ainsi programmer un grand nombre de tâches réparties sur tout un mois.

On lance Repartir(<Nombre de Tâches>,<Nombre de jours>)

Par exemple, pour répartir 6 tâches dans une journée, on fait:

call Repartir(6,1)

Pour lancer 250 jobs à faire en un mois:

call Repartir(250,30)

On récupère ensuite le résultat dans “c:\tmp\crontab.txt” à compléter avec les commandes à lancer.

Le code:


Public Sub Repartir(nTaches, nJours)
Dim nMinutes As Long
Dim dIntervalle As Long

Dim Result As String
Dim i As Integer
Dim Time As Long

If nJours > 30 Then
    MsgBox "Calcul sur 30 jours seulement", vbCritical
    Exit Sub
End If

nMinutes = nJours * 24 * 60 ' Nombre de minutes de l'intervalle
dIntervalle = Int(nMinutes / nTaches) ' Duree de l'intervalle en minutes

Debug.Print "Durée de l'intervalle: " & dIntervalle & " minutes."

Open "c:\tmp\crontab.txt" For Output As 1

Time = 0
Print #1, CronTime(Time)
Debug.Print CronTime(Time)

For i = 1 To nTaches - 1
    Time = Time + dIntervalle
    If i < 20 Then
        Debug.Print CronTime(Time)
    Else
    If i = 20 Then
        Debug.Print "etc..."
    End If
    End If
    Print #1, CronTime(Time)
Next i

Debug.Print ""
Close 1
End Sub
Public Function CronTime(Time)
Dim rHeures As Long
Dim rJours As Integer
Dim rMois As Integer
Dim sHeures As String
Dim sJours As String
Dim sMois As String
Dim nMinutes As Long

rHeures = 0
rJours = 0
rMois = 0
nMinutes = Time

While nMinutes >= 60
    nMinutes = nMinutes - 60
    rHeures = rHeures + 1
Wend

While rHeures >= 24
    rHeures = rHeures - 24
    rJours = rJours + 1
Wend

While rJours >= 30
    rJours = rJours - 30
    rMois = rMois + 1
Wend

sHeures = Str(rHeures)
sJours = Str(rJours)

If rJours = 0 Then sJours = "*" Else sJours = Str(rJours)
sMois = "*"

CronTime = nMinutes & " " & Trim(sHeures) & " " & Trim(sJours) & " " & Trim(sMois) & " *"

End Function

Bon Jovi not dead opening a restaurant will be there for me

author Posted by: admin on date Jan 3rd, 2012 | filed Filed under: Music

Completing my three post a day challenge with a word or two about Bon Jovi. Could be quite for me for music what Mac Donalds is to food. I’m snob enough to dislike, even with a touch of contempt, but I can’t avoid to go for it anyway.

What is happening is that someone wrote somewhere that Bon Jovi was dead. It was wrong, but the rumor did its job. Everyone is talking about Bon Jovi. Even if he was dead musically. That’s maybe a sign of the trolls to give a push to his career.

Despite his lack of inspiration, the Bon is still having a brain in working order. Just opened a restaurant somewhat special because this is one where you don’t need to pay. Cool idea, and very opportunistic these days. Sure I’ll dare to buy a plane ticket to his place. I’ll then be sure not to go to bed starving.

I may also ask the man if he could pay my taxes. I start to be short in cash. Happy to learn he will be there for me ;)



La Serre 1 – Le trou

author Posted by: admin on date Apr 21st, 2009 | filed Filed under: Aquaponics

Cette année c’est décidé, je mets la serre en aquaponie.
Selon le procédé testé à petite échelle, je fais un système à ma sauce qui comporte 3 étages: De l’eau au fond pour les racines, éventuellement un bulleur. Ensuite un vide d’air où s’étirera la racine pour aller chercher l’eau le plus profond possible. Va falloir que je creuse. Enfin un étage de spport et de départ de la plante. 5 à 10 cm de billes d’argile seront irriguées.

Mais j’ai du job avant, c’est le moins qu’on puisse dire. Pour l’instant c’est l’excavation de la serre.

Excavation serre

Un bon début :)

La serre

Check if file exists

author Posted by: fredometro on date Jun 3rd, 2004 | filed Filed under: MS-Access

It is often needed to know wether or not a file exists.
This simple function will do the job.
For instance, if we need to delete a file if it exists:


If FileExists("c:\myfile.txt") Then
    Kill "c:\myfile.txt"
End If

Here is the code of the function:


Public Function FileExists(FileName)
    On Error Resume Next
    Open FileName For Input As 1
    If Err = 0 Then
        FileExists = True
        Close 1
    Else
        FileExists = False
    End If
End Function

Custom Autonumber Access 2000

author Posted by: fredometro on date Jan 6th, 2004 | filed Filed under: MS-Access

Quertion: I am trying to create a custom autonumber, due to an upgrade from a previous approach database that used a double as the autonumber and primary key.

Got to the first session, paused. Try to contimue with F8. The record has been locked by the other session, the record can’t anymore be updated with a pessimistic lock.

here is the code I used to generate the autonumber. The table I created contains only one field (autonum).

This will be used in a multi-user environment in Access 2000 SR-1 runtime
and I need advice on how I can ensure an error will not kick the user out.

I tried to simulate simultaneous use but did not get an error as I have
expected to.

Is their a better way to test it before finding out the hard
way, it don’t really work?

Is their a better way of doing this?

Any suggestions will be appreciated.

Here’s my code

Module Code:


Public Function GetAutoNum()
'Returns Job Number from autonum table in autonum field.
'then adds one to autonum.
Dim num As Double
Dim rst As ADODB.Recordset
    Set rst = New ADODB.Recordset
    rst.Open "autonum", CurrentProject.Connection, adOpenDynamic,
    adLockPessimistic
    rst.MoveFirst
    num = rst("autonum")
    rst("autonum") = num + 1
    rst.Update
    rst.Close
    Set rst = Nothing
    GetAutoNum = num
End Function

Form Code:

Private Sub Form_BeforeInsert(Cancel As Integer)
    Me.jobnum = work.GetAutoNum
End Sub

Thanks in advanced
-Adam

With a regular lock, this is the second session that will fail to update.

So, here the locking method doesn’t matter, but you have to add error trapping code according to your locking scheme. When failed, the rst.update should retry and the appropriate code should be added. Here the code for retries you will add shall work on both lock schemes.

Regards.

atrout77 asked this follow-up question on 6/5/2002:
Thank you, this is the type of answer I hoped to get. I knew I should have gotten an error when i tried to simulate the situation, but for some reason or another it didn’t
(I tried to have another user insert a record at the exact time I added it, probably didn’t do it at the exact same time so no error occured as I expected)

Fredix gave this response on 6/6/2002:
Yes, try it. Your code just need a few more lines. Tell me how it goes.

I will try this as it is a great suggestion, just wonder why I didn’t think of it, thanx again.

 

Also, in my previous post, replace pessimistic by optimistic.

The optimistic lock fires a lock for the user who first updates the record.

With your code, the lock method doesn’t makes any difference, as the transaction should be very quick.

atrout77 asked this follow-up question on 6/10/2002:
Ok, I tried what you suggested and recieved an error as i should have, so now I can trap it.

I however never really wrote code to perform a retry of something and was wondering if i should use a loop (To time the next retry) and retry then or it their a better way?

Can you give an example?

Thanks again,
-Adam

Fredix gave this response on 6/10/2002:
Sure,

Yes, this is just a small loop to write. Here is one, which will replace the rst.Update line:


Dim Retries as Integer
Dim OtherError as string
...
    Retries = 0
    On Error Resume Next ' No break when error occurs
    rst.Update
    While Err <> 0 and Retries < 10 ' We will retry 10 times
        Retries = Retries + 1
        rst.Update
        OtherError = Error$
    Wend
    On Error Goto 0 ' Back to normal error break
    If Retries = 10 then
        Msgbox "Update was unsuccessful.", OtherError
    EndIf

10 retries is a very large number. 3 could be enough, it all depends on the potential number of users.
I added the OtherError variable, in case you have another error (disk full, disk error, duplicate record, etc)


Warning: include(/home/www/web421/html/phpTraffic/write_logs.php) [function.include]: failed to open stream: No such file or directory in /home/websitef/public_html/fredometro.com/wp-content/themes/blueshadow/footer.php on line 15

Warning: include() [function.include]: Failed opening '/home/www/web421/html/phpTraffic/write_logs.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/websitef/public_html/fredometro.com/wp-content/themes/blueshadow/footer.php on line 15