How to call "Action Result" ASP.NET MVC at specific time (Daily) - c#

I have a system for sending E-mails to users by a specific time .
built in ASP.NET MVC4 and has an action result "function" for checking the time of messages and send it if the day of the message is today .
how can I call this action result (daily) -like a scheduler- in efficient way ?
Thanks.

Whilst a separate service / application would be better, you could use wget.
GNU Wget is a free software package for retrieving files using HTTP,
HTTPS and FTP, the most widely-used Internet protocols. It is a
non-interactive commandline tool, so it may easily be called from
scripts, cron jobs, terminals without X-Windows support, etc.
You would then do something like:
"C:\Program Files (x86)\GnuWin32\bin\wget.exe" --no-check-certificate https://www.exammple.com/YouController/YourAction -O NUL
in a .bat file and set that to run via a windows Scheduled task at the time you require (assuming you don't need to run it less than every 60 seconds - if you do, let me know as I have another way around this using a windows service to call the bat file instead).
Omitting the -O NUL part would also save the output so you could see if everything ran successfully by doing:
public ActionResult YourAction()
{
//Do your code, get some stats that show it ran properly.
return Content("Return your stats here.");
}
from your controller action.

More efficient will be when you create new application as Windows Service. There u can easy set code to start at specific time. in this solution you will have more flexibility and independent. You can start hire : Windows Service to run a function at specified time

You could create a small console application that just calls the API do send out the emails. You can then schedule the console app to run at a specific time using the Windows Scheduler; you can even have it run without showing the console window. See here or here for details on how to schedule a task.

Use Azure Functions, that's exactly what it was built for. It's really good.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-overview

Related

why Control M OS Job that executes an exe gets stuck mid execution?

I have a c# exe that reads some log lines from a remote unix server using sed. when I run this exe several times on my windows server or even my dev pc, it executes fine. However, when I try to run it as a cyclic OS job in Control M eventually (sometimes at the first execution, seems to happen at random) it gets stuck but doesn't throw an exception or anything.
The command of the job runs a .bat file, and the cyclic is configured to 0 minutes with pause time being controlled dynamically by the exe with a thread.sleep.
after searching the web and seeking recommendation from other areas of my workplace that use control M, I have so far tried changing my agent to use local user with ctmwincfg, I also tried with changing the agent service to the same user (had to reverse this one as the agent stopped working properly), I also changed from directly executing my exe on the job to using a .bat file.
one of my hostgroup agents is windows server 2016 and 3 more are windows server 2012
I wasn't able of finding a solution within control M itself, but rather in the code of the .exe. I used a task, it allows the program to terminate the stuck method after a timeout, which in turn allows the control M job to finish normally.
var task = Task.Run(() => MyStuckMethod(arg));
if (task.Wait(TimeSpan.FromSeconds(30)))
return task.Result;
else
throw new Exception("Timed out");
Alternatively, there are a few workarounds within control M which involve sending alerts or creating shouts and then automating the kill of the job, but this is not useful for my case.
Example in BMC communities

Starting an existing Mainframe job with FTP and C#

We have a lot of jobs(jcl) running on the mainframe.
I was asked to try and start a restore job. I have to do this with a C# application.
What i got now is i am able to connect to the mainframe with an ftp library and I can call raw FTP commands that the mainframe understands.
I kind of know how to submit a .jcl file, which will be processed as a job if I use the command "quote site filetype=jes".
My questions are:
- Is there a way to start an existing job?
- Does it matter what "directory"/partition(?) I have navigated to before submitting a job?
You could submit the below JCL through FTP and it would run the JCL in JOB.LIBRARY(JOB)
//JS010 EXEC PGM=IEBGENER
//SYSUT1 DD DSN=JOB.LIBRARY(JOB),DISP=SHR
//SYSUT2 DD SYSOUT=(,INTRDR)
//SYSPRINT DD SYSOUT=*
//SYSIN DD DUMMY
Expanding on Bills comments, is this a Production, Development, Test, QA etc job.
In production on mainframe's, jobs are normally run using a Scheduler (e.g. Ca7 or Workload Scheduler for z/OS (formerly OPC) + several others). Some sites also use Schedulers in QA / Development as well, but this is rare.
Submitting a job via a scheduler
To submit via a scheduler you MUST talk to the Operations / Production Control / Mainframe Support department. They should know what is possible and have preferred ways of doing something like this. They should also know what access is required !!!
Possible options would include:
Most schedulers have the option of submitting a job / schedule when a Dataset (File for non-mainframer's) is created. If available this will probably be the easiest to implement.
All schedulers provide programs that can submit schedules.
You may be able to run a job that submits the appropriate job
Run a program in the foreground to submit the appropriate job
These days most of the schedulers would have a Web interface, they may also have interfaces on other platform. This option is probably not going to be available though.
Submitting a job
If you are just going submit the job, options include:
Copying the job to the INTRDR as #Deuian has done (either foreground or background).
Running TSO background
Submitting via job (from Deuian's answer):
//JOBNAME JOB ...
//JS010 EXEC PGM=IEBGENER
//SYSUT1 DD DSN=JOB.LIBRARY(JOB),DISP=SHR
//SYSUT2 DD SYSOUT=(,INTRDR)
//SYSPRINT DD SYSOUT=*
//SYSIN DD DUMMY
Finally to do anything on the Mainframe, you will need the appropriate security access !!!
I have tried to provide a background information + a basic guide of the option available. Basically you need to talk to Mainframe-Operation / Mainframe-programmers !!!.
Yes, you can - just execute the RETR 'DATASET.NAME' FTP command. Remember that the quotes here are important - without quotes the command would be interpreted as "read spool files by JOBID". And with quotes it would be interpreted as "submit an existing JCL data set, wait for job to complete and retrieve it's spool". And it doesn't matter what directory you have navigated to before submitting a job.
You can refer to my Java implementation of JES client which works through FTP - https://github.com/vadimshchukin/jesclient. It has code which does exactly what you want:
public JESJob execute(String datasetName) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
retrieveFile(String.format("'%s'", datasetName), outputStream);
JESJob job = new JESJob(this);
job.setSpool(outputStream.toString());
return job;
}

Calling website through Windows Service

Looking at creating a windows service that will run on an IIS Server. Said service will access an url (mysite.com/mailflow.ashx) every 5 minutes. This is done to get the mailflow going, which is done by accessing the mailflow.ashx file.
Is: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
what I'm looking for in my scenario or is there another class that will do the job?
Coming from a non-coding background but I want to do this on my own as a learning experience.
Small hints are more than enough.
Cheers!
Unless you really want to learn coding, I'd recommend simply creating a scheduled task in windows. Make it call the following js script (called wget in my case)
var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
WinHttpReq.Open("GET", WScript.Arguments(0), /*async=*/false);
WinHttpReq.Send();
WScript.Echo(WinHttpReq.ResponseText);
and set the task to start the following program cscript with the following arguments /nologo C:\Path\To\wget.js http://my.url.com
There are plenty of ways to ping an remote url, so you may decide to use another option (perhaps simply launching then killing a browser) but coding a service really is overkill if this is the only task you need
Yes, the HttpWebRequest class is what you need.

c# console application interacting with PHP page

I have a c# console application that I invoke with a server call in PHP
chdir($filePath);
exec($filePath.$fileName);
This works great, the application is run. The data it is designed to collect is collected and everyone is happy.
Currently I have plans on storing the one time use information on a server or a flat file, but then I noticed that while the console application is running and doing it's magic the page hangs waiting for the application to stop. This intrigued me, and now i'm wondering if there is a way for the application to pass it's data back to the page directly?
Note: I'm running Apache2 on Windows 7
Update:
Ended up using
$runCommand = "D:\\ScanBoyConsole\\ScanBoy_Console.exe COM1 9600 8 1 0 1";
$WshShell = new COM("WScript.Shell");
$output = $WshShell->Exec($runCommand)->StdOut->ReadAll;
json_decode($output);
If you mean by "directly" that you want the application's output sent to the client while it's still running you might be interested in passthru().
If you're also the author of the C# application you could skip the console application and expose the functionality in a way accessible via php's COM and .Net module.
The console app should be able to print (using Console.WriteLine), and PHP can take the results of that...
Back in my PHP days, we called scripts all the time (that are nothing but console apps basically) and had the results spit out to the page.
"shell_exec — Execute command via shell and return the complete output
as a string"
http://php.net/manual/en/function.shell-exec.php
[So, the only difference is that you should use shell_exec instead of a regular exec]

Best way to run a tool from ASP.Net page

I have a developer tool that I want to run from an internal site. It scans source code of a project and stores the information in a DB. I want user to be able to go to the site, chose their project, and hit run.
I don't want the code to be uploaded to the site because the projects can be large. I want to be able to run my assembly locally on their machine. Is there an easy way to do this?
EDIT: I should note, for the time being, this needs to be accomplished in VS2005.
EDIT 2: I am looking for similar functionality to TrendMicro's Housecall. I want the scan to run locally, but the result to be displayed in the web page
You could use a ClickOnce project (winform/wpf) - essentially a regular client app, deployed via a web-server. At the client, it can do whatever it needs. VS2005/VS2008 have this (for winform/wpf) as "Publish" - and results in a ".application" file that is recognised by the browser (or at least, some browsers ;-p).
You might be able to do the same with Silverlight, but that has a stricter sandbox, etc. It would also need to ask the web-server to do all the db work on its behalf.
I want to be able to run my assembly
locally on their machine
Sounds like you want them to download the tool and run it from their local machine, does that work for you?
Any code can scan files given the location and permissions. For a website to open an exe on a different machine and permit that to run and get access to the files contained on the web server would require a horrifically low level of security that would mean the entire system is practically completely open to attack. If your system is completely behind a firewall and hence protected from outside intererance then you want to look more at the permissions and less at the code.
To run an exe on a machine try following notepad example, though you may have to use a specified directory as well
ProcessStartInfo psi = new ProcessStartInfo("notepad.exe");
psi.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(ExitHandlerToKillProcess);
p.StartInfo = psi;
p.Start();
and when done dont forget to kill the Process. Alternately use javascript. Either way watch the security permissions and remember the risks of doing this.
I would probably write some sort of command line tool or service that does the processing and extraction of project data. Then I would use a page to update/register projects that the web server and the command line tool both have common access to. then at specified times either manually or via cron or similar mechanisms extract the data to your database. once you have this, you just use the website to display last extraction times and the extracted data.
if the projects/end users are on a different subnet etc, then you will need the end users to run the tool and then have it post the data into the database.

Categories

Resources