NAS cleanup code

girish.joshigirish.joshi Member Posts: 407
Is there anyway to run some code when the Nas running as service is stopped?

Answers

  • WaldoWaldo Member Posts: 3,412
    No, afaik there isn't.

    Note that you can set up the service to restart automatically ... .

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • girish.joshigirish.joshi Member Posts: 407
    yeah, that's not the problem. I want the service to be able to be stopped -- it 's just that I want it to clean up after itself when it does.
  • ara3nara3n Member Posts: 9,256
    well you can write your code to do the clean up when it starts the next time.
    If your code leaves some bad data if it is stoped, then something is wrong with your approach. Are you using commits?
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • girish.joshigirish.joshi Member Posts: 407
    yes, I am using commits.

    I've made a modification to allow the job scheduler to work without a client.

    Onrun, tick the Job scheduler running flag.

    A timer runs the neccesary code.

    It handles errors appropriately -- when neccesary unticking the job scheduler running flag.

    However, it can't detect when the Nas is stopped manually, therby unticking the flag.

    What's wrong with the approach?
  • ara3nara3n Member Posts: 9,256
    I guess we solved this offline.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • DenSterDenSter Member Posts: 8,305
  • WaldoWaldo Member Posts: 3,412
    Well, we made our own NAS scheduler ... . Unfortunately I don't exactly understand your problem.

    We didn't start with the existing Job Scheduler. We made our own scheduling mechanism, so that may be the reason that we didn't encounter the problem you are dealing with.

    We don't stop our NAS Services either. With any error, the nas keeps running, the errors are handled well, the job is marked as "error" and the next jobs can start running... .

    May be you can clarify a bit more what your problem is?

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • girish.joshigirish.joshi Member Posts: 407
    Waldo: I have no problem with errors either. I just wanted to make sure the Nas service could be stopped without leaving the data in an inconsistent state.

    The problem with using the job scheduler, is the "Job Scheduler is running" flag. It's mostly there to make sure that multiple users aren't using the job scheduler at the same time.

    Initially, when starting up, and in order to be in line with the way job scheduling worked in Navision, I would have my Nas service tick this flag.

    The problem, as ara3n put it, was that I was basically tying a system flag to the state of service, which was difficult (impossible?) to do.

    The solution that ara3n came up with was this: Don't tick the flag when starting. Instead, have the service poll the flag so that its not used by any user. Therfore, users using the job scheduler get standard functionality. The Nas service will only "act like" the job scheduler when no other users are using it, and the the service is turned on.

    No more danger of inconsistent data.

    Thanks to ara3n for this insight.
  • girish.joshigirish.joshi Member Posts: 407
    Just curious, how did you deal with this problem Denster? Earlier you mentioned that you had also hooked the Job Scheduler up to the NAS?
  • WaldoWaldo Member Posts: 3,412
    Try this:

    May be this can help you ...

    In codeunit 1 (ApplicationManagement):
    CompanyClose()
    IF GUIALLOWED THEN
      LogInEnd
    // *** Start
    ELSE
      fctNASShutdown;
    // *** Stop
    

    and in fctNASShutdown do your thing ... .

    Note: when starting the nas, you can write the session id from session table ... when it stops, you can check if session id is the same ... .

    Although, I doubt that when the NAS errors out (causes the nas to stop), then you have no process that is able to run any code, which will result into nothing (no OnCompanyClose either). Then Ara3n's solution seems the only way to go... .

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • ta5ta5 Member Posts: 1,164
    Have a look to Codeunit 5065 "E-Mail Dispatcher". Isn't this what we are looking for?
  • girish.joshigirish.joshi Member Posts: 407
    Waldo:

    That's just what I was looking for. I wanted to find out what code run when the NAS was shut down as a service --- CompanyClose appears to be the last function called.

    :oops: I should have checked there :oops: Didn't think that the company actually got closed when the NAS exited.

    Everything you said is correct. The errors I can handle are errors in Navision code. If the application server otherwise error's out, I think I'm out of luck. Therefore, I agree with you, and still think ara3n's solution is the best.

    ha5:

    No, unfortunately, that isn't quite what I was looking for. In that codeunit, the Semaphore table is used to log whether or not a user (NAS or otherwise) is working with the Email dispatcher. Should the NAS get stopped, it leaves the old data laying around. Whenever a new user tries to start the Dispatcher, it checks to see if any of the data belongs to a closed session and deletes it.

    In this case, that solution could work -- but it would be a little weird. The problem is that there is flag somewhere called "Job Scheduler Is Running" that would be set to true when the Scheduler is, in fact, not running. Granted, with same sort of book keeping code added to the Job Scheduler mgt. form, we could make sure it behaved properly, but still the data would be in an inconsistent state sometimes, and it would require writing more code/modification to standard navision.

    Thanks to all.
  • WaldoWaldo Member Posts: 3,412
    no prob ...

    about the possibility that "nas errors out" ... this shouldn't be possible. Any error in navision doesn't cause the NAS to stop ... it just gives you a message in the event viewer, and the single instance codeunit keeps on running... . It is when the nas(sql).exe-process is stopped (e.g. in the task manager - or some kind of other nasty error) that the OnCompanyClose will not be triggered ... .

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • DenSterDenSter Member Posts: 8,305
    NAS won't leave your data inconsistent. It is a regular window-less Navision client, and if there's an error it will roll back. As for the Scheduler Running issue, I would find a way not to need that flag for it to run right.

    I don't remember exactly how I did it. I haven't actually implemented it for a production, it was a demo for a customer years and years ago and it was a quick thing that I did late at night in a hotel room in Scotland for a design session I was in.
  • DenSterDenSter Member Posts: 8,305
    NAS won't leave your data inconsistent. It is a regular window-less Navision client, and if there's an error it will roll back. As for the Scheduler Running issue, I would find a way not to need that flag for it to run right.

    I don't remember exactly how I did it. I haven't actually implemented it for a production, it was a demo for a customer years and years ago and it was a quick thing that I did late at night in a hotel room in Scotland for a design session I was in.
  • Joe_MathisJoe_Mathis Member Posts: 173
    Hi Everyone, :D

    I have tinkered around with NAS and Job Scheduler, but have not been too concerned about getting them to work properly or together until very recently. :mrgreen:
    I have been tasked specifically with getting two reports to run automatically off hours.
    I am using 4.0SP1 on SQL.
    The reports are ACIE (Report 795) and Post Cost To G/L (Report 1002).

    After reading this and other posts, I have come up with the following routine to get this done:

    1. Copy and/or modify the reports so they do not require any user input in order to run properly.

    2. Create a codeunit that mimics the Job Scheduler without it's form.

    3. Schedule the new/modified reports to run using Job Scheduler.

    4. Start NAS

    5. Start a timer codeunit (single instance right?)

    6. Use the timer to check if the Job Scheduler is being used by other users and start the codeunit (Job Scheduler Mimic) if it is not in use.

    7. Stop NAS (if desired)

    I am using the instructions from the nastest.zip download by Lars Lohndorf-Larsen to Start the NAS (thank you!)
    I couldn't find any "best practices" for modifying the reports/codeunits/forms for use with NAS though.
    Do you find it better to use a setup table and modify the report to call for those values and save it as a new report? :-k
    Should I mimic the reports in a codeunit and call that instead? :-k
    Would a better approach be copying all of the Job Scheduler objects and running those (not caring if a user starts Job Scheduler)?
    I personally don't like to modify functionality on reports/code/forms but rather copy them and modify my copy (making reference to the original in the documentation trigger).

    I feel as though I may be missing something though. Any thoughts?

    Thanks,

    Joe
  • DenSterDenSter Member Posts: 8,305
    I've added fields to setup tables to get the values from. You'' have to add functions to the report to have a codeunit set those values in C/AL, or you can get default ones from the setup table. I have got MRP to run automatically unattended for a customer a while ago like that.
  • Joe_MathisJoe_Mathis Member Posts: 173
    Hi Denster,

    Thanks for the quick reply.

    Should I be worried about the progress dialog box from the report?

    I have modified the ACIE report so it uses TODAY as a default date and it runs without error (so far) in the Job Scheduler, but it shows the progress indicator.
    Would this show as an error in the event log?

    Joe
  • DenSterDenSter Member Posts: 8,305
    yes anything visual will be a problem. You can use the GUIALLOWED property to catch that:
    IF GUIALLOWED THEN
      Message('now it is ok to show a message');
    
    In that case when NAS runs the object it will not cause a problem.
  • Joe_MathisJoe_Mathis Member Posts: 173
    edited 2006-04-05
    I used the GUIALLOWED and it works great. :D I did put in some messages to let myself know that it was done (and to see what would happen) they just appear in the eventviewer, so I used it as a way to check that the code is running. :-k

    Things worth mentioning on getting the NAS to run...
    1. Make sure that you can login using Windows authentication.
    ( I was using a SQL developer database and just updated the Navision database from 3.7b to 40SP1 without updating the xp_ndo.dll, Windows authentication did not work until I installed the correct updated version from the Product CD.)
    2. Put your parameter in the right place.
    (The write up says you need to create a function but there is already one in the newer versions called NASHandler. Insert your function after the case statement with your startup parameter.)
      IF CGNASStartedinLoop = FALSE THEN
        CASE Parameter OF
          'MAILLOG':
            CODEUNIT.RUN(CODEUNIT::"E-Mail Dispatcher");
          'SMARTTAG':
            STPublisher.StartTimer;
          'MyParameter':
            BEGIN;
              MESSAGE('NAS Has logged in.');
              CODEUNIT.RUN(CODEUNIT::"My Code");
            END;
    
    It may sound simple but fixing this showed that the service had started with the correct parameter when checking the Event Viewer, before it was giving an error and attempting a restart.

    Thanks Again,

    Joe
  • DenSterDenSter Member Posts: 8,305
    I usually put the message at the end of the OnRun trigger of the single instance codeunit, so that I know that all startup code ran before the messages comes through in the event log. By the way, if you have more than one line of code in your CASE, you will need a BEGIN and END statement.
  • Joe_MathisJoe_Mathis Member Posts: 173
    Yeah, at the end makes sense.

    I did have a begin and end,
        CASE Parameter OF
          'MAILLOG':
            CODEUNIT.RUN(CODEUNIT::"E-Mail Dispatcher");
          'SMARTTAG':
            STPublisher.StartTimer;
          'EDI' : 
            BEGIN
              MESSAGE('NAS Has logged in.');
              CODEUNIT.RUN(CODEUNIT::"EDI Automation Timer");
            END;
    

    but wanted to leave an example that could be followed if someone else has a problem. So I used the MyParameter instead of my actual parameter, then added the message as an after thought. :oops: I will edit that post to reflect the change correctly. :mrgreen:

    Joe
  • mrQQmrQQ Member Posts: 239
    ok, can someone please repeat how did they solve that issue with "scheduller is running" flag? :-)
  • girish.joshigirish.joshi Member Posts: 407
    The solution was to not tick it for the NAS. Since the field exists for users using the Job Scheduler normally, by not ticking it with the NAS, you maintain standard functionality.

    Now, there is still a problem because the NAS Job Scheduler can't work when the User Job Scheduler is on. Therefore, you add code to the Job Scheduler to keep on checking the flag to see if another user is trying to use the NAS. If there is, you stop the NAS version of the job.
Sign In or Register to comment.